我们在服务器中有一个JSON文件。我们能够检索JSON文件,但显然我们无法获取嵌套JSON中的项目。
以下是我们代码的摘要:
- (void)onSuccess:(WLResponse *)response{
NSLog(@"Connection Success: %@",response);
NSString *resultText;
if ([response responseText] != nil) {
resultText = [response responseText];
NSDictionary *allData = [response getResponseJson];
NSDictionary *resultData = allData[@"result"];
...
}
}
以下是我们的JSON文件结构:
{"contacts":[
{
"name":"name 1",
"address":"address 1"
},
{
"name":"name 2",
"address":"address 2"
},
{
"name":"name 3",
"address":"address 3"
}
]}
答案 0 :(得分:2)
在客户端上安装NSDictionary后,您可以使用valueForKeyPath:。
例如,伪代码:
NSArray* contacts = [allData valueForKeyPath:@"result.contacts"];
NSString* firstContactName = contacts[0][@"name"];
我假设allData
引用的NSDictionary在results.contacts
密钥路径中有联系人NSArray。如果情况并非如此,请根据NSDictionary的结构进行修改。