I have got the following json
我有以下json
{"status":1,"value":{"details":{"40404000024769":[{"name":"","email":""}]}}}
Im parsing it as follows,
我解析它如下,
NSString *statusCode = [NSString stringWithFormat:@"%@",jsonDic[@"status"]];
NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
NSArray *details = [valueDict objectForKey:@"details"];
NSLog(@"%@",details);
for (NSDictionary *response in details){
NSLog(@"adsf %@",response);
}
}
Following is my error log
以下是我的错误日志
This can get the only 40404000024769
but not the value of 40404000024769
. I tried using[response valueForKey:@"name"]
and the app crashes. How can I be able to get the value of name, email?
这可以得到唯一的40404000024769但不是40404000024769的值。我尝试使用[response valueForKey:@“name”]并且应用程序崩溃了。我怎样才能获得姓名,电子邮件的价值?
2016-04-27 16:24:02.967 Vaighai Export[311:10625] ***Terminating app due to uncaught exception 'NSUnknownKeyException', reason:'[<__NSCFString 0x14e56870> valueForUndefinedKey:]:this class is not key value coding-compliant for the key name.'
5 个解决方案
#1
3
Your JSON structure is:
您的JSON结构是:
-NSDictionary
--Number
--NSDictionary
---NSDictionary
----NSDictionary
-----NSArray
------NSDictionary
The key details
has a dictionary against it as value, not an array as you have assumed. Change your code to this:
关键细节有一个字典反对它作为值,而不是你假设的数组。将您的代码更改为:
NOTE: This is only sample to show you how you were parsing it wrong. You need to handle cases for your real world json in your app.
注意:这只是向您展示如何解析错误的示例。您需要在应用中处理真实世界json的案例。
NSString *statusCode = [NSString stringWithFormat:@"%i",jsonDic[@"status"]]; //We got status
NSDictionary *valueDict = [jsonDic objectForKey:@"value"]; //We got value dic
NSDictionary *detailDic = [valueDict objectForKey:@"details"]; //We got details dic
NSArray * internalArr = [detailDic objectForKey:@"40404000024769"]; //We got array of dictionaries
//Iterate over this array to log internal dictionaries
for(NSDictionary *nameDic in internalArr)
{
NSLog(@"Name: %@ email: %@",[nameDic objectForKey:@"name"],[nameDic objectForKey:@"email"]);
}
#2
1
Firstly from the details dictionary fetch allkeys after that put that keys in the array then at zero index of that array the key is available find the array inside that key.
首先从详细信息字典中获取allkeys,然后将这些键放入数组中,然后在该数组的零索引处,键可用,找到该键内的数组。
#3
1
use this code
使用此代码
NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
NSDictionary *details = [valueDict objectForKey:@"details"];
NSArray *YourArray= [details objectForKey:@"40404000024769"];
NSString *Name = [YourArray objectAtIndex:0]valueForKey:@"name"];
NSString *Email = [YourArray objectAtIndex:0]valueForKey:@"email"];
#4
1
status
is an integer
status是一个整数
NSInteger statusCode = [jsonDic[@"status"] integerValue];
value
contains a dictionary details
value包含字典详细信息
NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
NSDictionary *details = [valueDict objectForKey:@"details"];
details
contains an array in a dictionary. In item 0 of the array there is a dictionary containing the requested name
and email
keys.
details包含字典中的数组。在数组的第0项中,有一个包含所请求的名称和电子邮件密钥的字典。
for (NSString *key in details){
NSLog(@"key %@",key);
NSDictionary *data = details[key][0];
NSLog(@"name %@ - email %@", data[@"name"], data[@"email"]);
}
#5
1
key details
is a dictionary not an array.
关键细节是字典而不是数组。
Just add one line in to fetch 40404000024769
key value.
只需添加一行即可获取40404000024769键值。
Change your code as bellow:
将代码更改为:
NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
NSDictionary *details = [valueDict objectForKey:@"details"];
NSArray *ar = [details objectForKey:@"40404000024769"];
for (NSDictionary *response in ar){
NSLog(@"name: %@ email: %@",[response objectForKey:@"name"],[response objectForKey:@"email"]);
}
#1
3
Your JSON structure is:
您的JSON结构是:
-NSDictionary
--Number
--NSDictionary
---NSDictionary
----NSDictionary
-----NSArray
------NSDictionary
The key details
has a dictionary against it as value, not an array as you have assumed. Change your code to this:
关键细节有一个字典反对它作为值,而不是你假设的数组。将您的代码更改为:
NOTE: This is only sample to show you how you were parsing it wrong. You need to handle cases for your real world json in your app.
注意:这只是向您展示如何解析错误的示例。您需要在应用中处理真实世界json的案例。
NSString *statusCode = [NSString stringWithFormat:@"%i",jsonDic[@"status"]]; //We got status
NSDictionary *valueDict = [jsonDic objectForKey:@"value"]; //We got value dic
NSDictionary *detailDic = [valueDict objectForKey:@"details"]; //We got details dic
NSArray * internalArr = [detailDic objectForKey:@"40404000024769"]; //We got array of dictionaries
//Iterate over this array to log internal dictionaries
for(NSDictionary *nameDic in internalArr)
{
NSLog(@"Name: %@ email: %@",[nameDic objectForKey:@"name"],[nameDic objectForKey:@"email"]);
}
#2
1
Firstly from the details dictionary fetch allkeys after that put that keys in the array then at zero index of that array the key is available find the array inside that key.
首先从详细信息字典中获取allkeys,然后将这些键放入数组中,然后在该数组的零索引处,键可用,找到该键内的数组。
#3
1
use this code
使用此代码
NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
NSDictionary *details = [valueDict objectForKey:@"details"];
NSArray *YourArray= [details objectForKey:@"40404000024769"];
NSString *Name = [YourArray objectAtIndex:0]valueForKey:@"name"];
NSString *Email = [YourArray objectAtIndex:0]valueForKey:@"email"];
#4
1
status
is an integer
status是一个整数
NSInteger statusCode = [jsonDic[@"status"] integerValue];
value
contains a dictionary details
value包含字典详细信息
NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
NSDictionary *details = [valueDict objectForKey:@"details"];
details
contains an array in a dictionary. In item 0 of the array there is a dictionary containing the requested name
and email
keys.
details包含字典中的数组。在数组的第0项中,有一个包含所请求的名称和电子邮件密钥的字典。
for (NSString *key in details){
NSLog(@"key %@",key);
NSDictionary *data = details[key][0];
NSLog(@"name %@ - email %@", data[@"name"], data[@"email"]);
}
#5
1
key details
is a dictionary not an array.
关键细节是字典而不是数组。
Just add one line in to fetch 40404000024769
key value.
只需添加一行即可获取40404000024769键值。
Change your code as bellow:
将代码更改为:
NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
NSDictionary *details = [valueDict objectForKey:@"details"];
NSArray *ar = [details objectForKey:@"40404000024769"];
for (NSDictionary *response in ar){
NSLog(@"name: %@ email: %@",[response objectForKey:@"name"],[response objectForKey:@"email"]);
}