[英]NSJSONSerialization parse error - unrecognized selector
This is the code: 这是代码:
NSString *jsonString = @"[
{\"sn\": \"E\", \"t\": \"K\", \"d\": \"Tue 3-Mar\"},
{\"sn\": \"F\", \"t\": \"Y 1\", \"d\": \"Tue 3-Mar\"}
]";
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"jsArray: %@", jsArray);
for (id job in jsArray) {
NSLog(@"job: %@", job);
NSLog(@"%@", [job sn]);
}
In the console, I get this: 在控制台中,我得到以下信息:
2014-04-22 15:40:46.464 test[2442:60b] jsArray: (
{
d = "Tue 3-Mar";
sn = E;
t = K;
},
{
d = "Tue 3-Mar";
sn = F;
t = "Y 1";
}
)
2014-04-22 15:40:46.466 test[2442:60b] job: {
d = "Tue 3-Mar";
sn = E;
t = K;
}
-[__NSCFDictionary sn]: unrecognized selector sent to instance 0x8fa53b0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSCFDictionary sn]: unrecognized selector sent to instance 0x8fa53b0'
It seems to recognise the array of objects and the individual object. 似乎可以识别对象数组和单个对象。 Why does it object to the property
sn
? 为什么它反对属性
sn
?
The jsArray
contains dictionaries. jsArray
包含字典。 So job
is an NSDictionary
. 所以
job
是一个NSDictionary
。 NSDictionary
doesn't have a method named sn
. NSDictionary
没有名为sn
的方法。 If you want the value for the key @"sn"
then you need: 如果需要键
@"sn"
的值,则需要:
for (NSDictionary *job in jsArray) {
NSLog(@"job: %@", job);
NSLog(@"%@", job[@"sn"]);
}
Your jsArray
contains NSDictionaries, So the type of job will be NSDictionary
. 您的
jsArray
包含NSDictionaries,因此作业的类型将为NSDictionary
。 You can't retrieve value from NSDictionary
like that. 您无法像这样从
NSDictionary
检索值。
Use: 采用:
NSLog(@"%@", [job objectForKey:@"sn"]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.