IOS中的简单嵌套JSON解析

时间:2014-04-11 15:38:08

标签: ios objective-c json

我从API接收此JSON:

{"MatchedResult":{"CurrentStatus":47}}

使用此代码:

NSString *responseString = [request responseString];
NSDictionary *responseDict = [responseString JSONValue];
NSString *currentStatus = [responseDict objectForKey:@"CurrentStatus"];

返回Null。

使用此代码:

NSString *responseString = [request responseString];
NSDictionary *responseDict = [responseString JSONValue];
NSString *currentStatus = [responseDict objectForKey:@"MatchedResult"];

返回:

{
    CurrentStatus = 47;
}

我如何才能获得47的状态?

3 个答案:

答案 0 :(得分:0)

你需要深入了解结构:

NSString *responseString = [request responseString];
NSDictionary *responseDict = [responseString JSONValue];
NSString *currentStatus = responseDict[@"MatchedResult"][@"CurrentStatus"];

答案 1 :(得分:0)

您的Response字典包含Matched Result字典,其中包含您的CurrentStatus

你想......

NSDictionary *matchedResultDict = [responseDict objectForKey:@"MatchedResult"];
NSString *currentStatus = [matchedResultDict objectForKey:@"CurrentStatus"];

答案 2 :(得分:0)

获取您需要的值,您必须分析json以获得必要的密钥

{
     "MatchedResult": {
        "CurrentStatus": 47
      }
}

因为需要CurrentStatus键必须通过MatchedResult

NSString *responseString = [request responseString]; 
NSDictionary *responseDict = [responseString JSONValue];
NSString *currentStatus = [[responseDict objectForKey:@"MatchedResult"] objectForKey:@"CurrentStatus"];
相关问题