目标C-NSDictionary解析嵌套的JSON [英] Objective C - NSDictionary parsing nested JSON

查看:79
本文介绍了目标C-NSDictionary解析嵌套的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一个深度嵌套的JSON,我正在尝试将其解析并创建为模型对象.

Here is a deeply nested JSON that i'm trying to parse and create into model Objects.

基本上,我需要创建模型对象,直到'Child'数组具有0个元素.

Basically i need to create model objects until the 'Child' array has 0 elements.

这是在做什么

        dictTree = [dict[@"jsonTree"] mutableCopy];
        NSMutableArray *children = [[NSMutableArray alloc] init];
        if (dictTree.count > 0)
        {
            while (true)
            {
                CategoryChild *categoryChild = [[CategoryChild alloc]init];
                NSString *str = dictTree[@"id"];
                categoryChild.childId = str;
                categoryChild.name = dictTree[@"name"];
                categoryChild.type = dictTree[@"type"];
                categoryChild.parent = dictTree[@"parent"];
                categoryChild.symbol = dictTree[@"symbol"];
                categoryChild.multiple = dictTree[@"multiple"];
                categoryChild.metricUnit = dictTree[@"metricUnit"];

                [children addObject:categoryChild];
                dictTree = [dictTree[@"child"] mutableCopy];

                if (dictTree.count == 0)
                {
                    break;
                }
            }

            categoryItem.children = children;
            [categoryList addObject:categoryItem];
        }

不幸的是,在第二次迭代中,当我访问dictTree [@"id"]时-崩溃

Unfortunately during the second iteration when i access dictTree[@"id"] - get a crash

"NSInvalidArgumentException",原因:-[__ NSArrayM objectForKeyedSubscript:]:无法识别的选择器已发送到实例

'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKeyedSubscript:]: unrecognized selector sent to instance

问题似乎是这样的事实,即将字典分配给子"字典,它确实喜欢它.

The issue seems to be the fact that am assigning a dictionary to the 'child' dictionary and it does seem to like it.

尽管在调试器中,我仍可以看到子值.

Although in the debugger, i can see the child values.

任何关于如何回事或做错事情的想法都将受到赞赏.谢谢.

Any ideas on how to go bout things or what am doing wrong would be appreciated. thank you.

推荐答案

更改

dictTree = [dict[@"jsonTree"] mutableCopy];

 dictTree = [dict[@"jsonTree"][@"child"] mutableCopy];

那应该做的.

键jsonTree的值是jSON,而不是您期望的JSONArray. JSONArray是"jsonTree"中键"child"的值.

Value for key jsonTree is a jSON and not JSONArray as you are expecting. JSONArray is the value for key "child" inside "jsonTree".

我希望您知道以下事实:如果代码存在于JSONArray中,您的代码将无法解析第二个子对象.查看您的jSON,每个子键代码只有一个JSON,看起来不错.但是,如果每个键子级"有多个jSON,则需要更好的代码来解析.

I hope you are aware of the fact that your code will not be able to parse the second child object if present in JSONArray. Looking at your jSON where there is only one JSON per child key code looks fine. But in case if there is more than one jSON per key "child" you need a better code to parse.

我能想到的一种更清洁的方法

A little cleaner approach I can think of

- (void) parseChildrenArray : (NSArray *) dict {
    for(NSDictionary *child in dict) {
        CategoryChild *createdChild = [self createCategory:child];
        [self.childrenArray addObject:createdChild];
        if ([child[@"child"] count] > 0) {
            [self parseChildrenArray:child[@"child"]];
        }
    }
}

-(CategoryChild *)createCategory: (NSDictionary *)child {
    CategoryChild *ch = [[CategoryChild alloc] init];
    ch.id = child[@"id"];
    //parse other property
    return ch;
}

声明属性

@property (nonatomic,strong) NSMutableArray *childrenArray;

最终致电

NSDictionary *tree = json[@"jsonTree"];
self.childrenArray = [[NSMutableArray alloc] init];
[self parseChildrenArray:tree[@"child"]];

这篇关于目标C-NSDictionary解析嵌套的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆