如何解析此特定JSON响应

时间:2015-01-11 14:29:24

标签: ios objective-c json xcode

我有一个JSON响应可以找到here

我过去使用NSArrayNSDictionary解析了JSON响应,但JSON响应编码的文档(找到here)令人困惑。

例如,在此示例响应中:

{
    "response": {
        "success": 1,
        "current_time": 1392754263,
        "raw_usd_value": 0.31,
        "items": {
            "Kritzkrieg": {
                "defindex": {
                    "0": 35
                },
                "prices": {
                    "11": {
                        "Tradable": {
                            "Craftable": {
                                "0": {
                                    "currency": "keys",
                                    "value": 32,
                                    "value_high": 34,
                                    "last_update": 1388253643,
                                    "difference": 21
                                }
                            }
                        }
                    },
                    "3": {
                        "Tradable": {
                            "Craftable": {
                                "0": {
                                    "currency": "metal",
                                    "value": 0.33,
                                    "last_update": 1379423777,
                                    "difference": -0.11
                                }
                            }
                        }
                    },
                    "6": {
                        "Tradable": {
                            "Craftable": {
                                "0": {
                                    "currency": "metal",
                                    "value": 0.05,
                                    "last_update": 1336410088,
                                    "difference": 0
                                }
                            },
                            "Non-Craftable": {
                                "0": {
                                    "currency": "metal",
                                    "value": 0.05,
                                    "last_update": 1362791812,
                                    "difference": 0.03
                                }
                            }
                        }
                    }
                }
            },
        }
    }
}

只有某些NSDictionaries包含密钥“Craftable”,“Non-Craftable”等。

当我解析我的回答时,我怎么能够完成这个?

到目前为止我的代码:

+ (NSArray *)groupsFromJSON:(NSData *)objectNotation error:(NSError **)error
{
    NSError *localError = nil;
    NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError];

    if (localError != nil) {
        *error = localError;
        return nil;
    }

    NSMutableArray *groups = [[NSMutableArray alloc] init];
    NSDictionary *results = [parsedObject objectForKey:@"response"];
    NSDictionary *items = [results objectForKey:@"items"];

    for (NSDictionary *groupDic in items) {
        TF2 *tf2 = [[TF2 alloc] init];
        NSDictionary *groupDick = [items objectForKey:groupDic];
        NSDictionary *prices = [groupDick valueForKey:@"prices"];
        for(NSDictionary *groupDicky in prices) {
            NSDictionary *groupDic1 = [groupDicky objectForKey:prices];
            for(NSDictionary *groupDic2 in groupDic1) {
                NSDictionary *tradable = [groupDic2 valueForKey:@"Tradable"];
                for(NSDictionary *groupDic3 in tradable) {
                    NSDictionary *craftable = [groupDic3 valueForKey:@"Craftable"];
                    NSDictionary *uncraftable = [groupDic3 valueForKey:@"Non-Craftable"];

                NSDictionary *untradable = [groupDic2 valueForKey:@"Non-Tradable"];
                for(NSDictionary *groupDick4 in tradable) {
                    NSDictionary *craftable2 = [groupDick4 valueForKey:@"Craftable"];
                    NSDictionary *uncraftable2 = [groupDick4 valueForKey:@"Non-Craftable"];
                }
            }

        }
        //[groups addObject:list];
    }

    return groups;
}

1 个答案:

答案 0 :(得分:1)

NSDictionary会返回nil以获取此词典中不存在的密钥:

NSDictionary *craftable = [groupDic3 valueForKey:@"Craftable"];
// craftable is nil here

这意味着以下任何调用都会返回nil

NSDictionary* dict = [craftable valueForKey:@"0"];  // nil
NSString* currency = [dict valueForKey:@"currency"];  // nil

也就是说,如果"Craftable""Non-Craftable"包含currency,则您获得此值,否则您将获得nil。这还不足以理解您需要的数据是否可用?