首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从字典中解析字典数组

从字典中解析字典数组
EN

Stack Overflow用户
提问于 2012-08-28 16:55:17
回答 2查看 105关注 0票数 0

解析XML文件后,输出将以字典中的以下格式出现,现在我想将其转换为字典数组

有没有人能给出一个合适的解决方案

代码语言:javascript
复制
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;
        };
    }
)
}
}

现在输出字典又是上述格式了,我想把它转换成字典数组

代码语言:javascript
复制
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>
EN

回答 2

Stack Overflow用户

发布于 2012-08-28 17:05:33

执行以下操作:

代码语言:javascript
复制
 NSMutableArray *arrData = [[parsedValueDictionary objectForKey:@"Data"]  objectForKey:@"row"];
票数 1
EN

Stack Overflow用户

发布于 2012-08-28 17:02:15

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

代码语言:javascript
复制
NSMutableArray *arrayOfDictionaries = [[NSMutableArray alloc] init];
NSDictionary *rows = [rootDictionary objectForKey:@"Data"];

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

UPDATE:我假设“数据”字典中只有“行”条目。否则,您应该在前面检查si的键是否为"row“。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12155839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档