【问题标题】:Objective C - NSDictionary parsing nested JSONObjective C - NSDictionary 解析嵌套的 JSON
【发布时间】:2017-09-18 14:42:25
【问题描述】:

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

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

这是在做什么

        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"] 的第二次迭代中 - 崩溃了

'NSInvalidArgumentException',原因:'-[__NSArrayM objectForKeyedSubscript:]:无法识别的选择器发送到实例

问题似乎在于将字典分配给“子”字典并且它似乎确实喜欢它。

虽然在调试器中,我可以看到子值。

任何关于如何处理事情或做错什么的想法都将不胜感激。谢谢。

【问题讨论】:

  • 用代码而不是图像编辑问题。

标签: objective-c json nsdictionary


【解决方案1】:

改变

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

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

这应该可以完成工作。

key jsonTree 的值是jSON 而不是JSONArray,正如您所期望的那样。 JSONArray 是“jsonTree”中键“child”的值。

我希望您知道,如果 JSONArray 中存在第二个子对象,您的代码将无法解析第二个子对象。查看每个子密钥代码只有一个 JSON 的 JSON 看起来不错。但是,如果每个键“子”有多个 JSON,则需要更好的代码来解析。

编辑:

我能想到的更简洁的方法

- (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"]];

【讨论】:

  • 感谢您的意见。我没有注意到它是一个数组。让我哑口无言。再次感谢您使用递归的简洁方式。
  • @kamyfc : 很高兴你解决了这个问题 :) 编码愉快
【解决方案2】:

您的孩子似乎由充满一本字典的数组组成。你需要访问这个数组的第一个对象来获取字典。

[children addObject:categoryChild];
if ([dictTree[@"child"] count] > 0) {
    dictTree = [dictTree[@"child"][0] mutableCopy];
}

调试器还显示 dictTree 由键 0 处的一个元素组成。该元素就是您的字典。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多