0

解析 XML 文件后,输出在字典中以以下格式出现现在我想将其转换为字典数组
任何人都可以提出正确的解决方案

Printing description of rootDictionary:
<CFBasicHash 0x8866000 [0x1078b38]>{type = mutable dict, count = 1,
entries =>
    0 : <CFString 0x8869490 [0x1078b38]>{contents = "Data"} = <CFBasicHash 0x88695c0 [0x1078b38]>{type = mutable dict, count = 1,
entries =>
    0 : <CFString 0x8869610 [0x1078b38]>{contents = "row"} = (
        {
        Address =         {
            text = "Skt. Clemens Torv 2-6";
        };
        City =         {
            text = "Aarhus C";
        };
        Country =         {
            text = Danmark;
        };
        Description =         {
            text = Ottopiste;
        };
        Lat =         {
            text = "56.156818";
        };
        Lon =         {
            text = "10.2092532";
        };
        Name =         {
            text = Ottopisteet;
        };
        Type =         {
            text = 1;
        };
        Zip =         {
            text = 8000;
        };
    },
        {
        Address =         {
            text = "Kauppatie 66";
        };
        City =         {
            text = Kauhava;
        };
        Country =         {
            text = Finland;
        };
        Description =         {
            text = "(null)";
        };
        Lat =         {
            text = "63.1001586";
        };
        Lon =         {
            text = "23.0463634";
        };
        Name =         {
            text = I m Here;
        };
        Type =         {
            text = 2;
        };
        Zip =         {
            text = 62200;
        };
    }
)
}
}

现在输出字典再次采用上述格式我想将其转换为字典数组

Actual format of XML for reference:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Data>
<row>
  <Lat>56.156818</Lat>
  <Lon>10.2092532</Lon>
  <City>Aarhus C</City>
  <Address>Skt. Clemens Torv 2-6</Address>
  <Zip>8000</Zip>
  <Country>Danmark</Country>
  <Name>Ottopisteet</Name>
  <Description>Ottopiste</Description>
  <Type>1</Type>
</row>
<row>
  <Lat>63.1001586</Lat>
  <Lon>23.0463634</Lon>
  <City>Kauhava</City>
  <Address>Kauppatie 66</Address>
  <Zip>62200</Zip>
  <Country>Finland</Country>
  <Name>I am Here</Name>
  <Description>(null)</Description>
  <Type>2</Type>
</row>
</Data>
4

2 回答 2

1

做这个:

 NSMutableArray *arrData = [[parsedValueDictionary objectForKey:@"Data"]  objectForKey:@"row"];
于 2012-08-28T09:05:32.797 回答
0

您可以只迭代行字典并将每个对象添加到数组中:

NSMutableArray *arrayOfDictionaries = [[NSMutableArray alloc] init];
NSDictionary *rows = [rootDictionary objectForKey:@"Data"];

[rows enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
  [arrayOfDictionaries addObject:obj];
}];

更新:我假设“数据”字典中只有“行”条目。否则,如果之前是“行”,您应该检查 si 的键。

于 2012-08-28T09:02:15.423 回答