用Objective-C解析JSON
对于我的项目,我使用PHP构建了自己的api。json编码的结果基本上给了我一个如下所示的条目数组用Objective-C解析JSON,objective-c,json,api,Objective C,Json,Api,对于我的项目,我使用PHP构建了自己的api。json编码的结果基本上给了我一个如下所示的条目数组 {"terms":[ {"term0": {"content":"test id", "userid":"100","translateto":null, "hastranslation":"0", "created":"2011-10-19 16:5
{"terms":[
{"term0":
{"content":"test id",
"userid":"100","translateto":null,
"hastranslation":"0",
"created":"2011-10-19 16:54:57",
"updated":"2011-10-19 16:55:58"}
},
{"term1":
{"content":"Initial content",
"userid":"3","translateto":null,
"hastranslation":"0",
"created":"2011-10-19 16:51:33",
"updated":"2011-10-19 16:51:33"
}
}
]
}
然而,我在使用NSMutableDictionary和提取Objective-C中的“内容”时遇到了问题
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSMutableDictionary *JSONval = [responseString JSONValue];
[responseString release];
if (JSONval != nil) {
NSMutableDictionary *responseDataDict = [JSONval objectForKey:@"terms"];
if (responseDataDict!= nil) {
for (id key in responseDataDict) {
NSString *content = [[responseDataDict objectForKey:key]objectForKey:@"content"];
[terms addObject:content];
textView.text = [textView.text stringByAppendingFormat:@"\n%@", content];
}
button.enabled = YES;
}
}
}
其中,当我将objectForKey发送给ResponseDataict时,NSLog会抛出错误,根据日志,这是一个_nsarray
我在这里做错了什么?请参考此.
.
请参考此
.
.
NSMutableDictionary*ResponseDataICT=[JSONval objectForKey:@“术语”] 但是
“术语”
的价值不是字典;这是一个数组。注意JSON字符串中的方括号。你应使用:
NSArray *terms = [JSONval objectForKey:@"terms"];
相反
请注意,数组中的每个术语都是一个对象(字典),其中包含一个名称(键),其对应的值(对象)依次是另一个对象(字典)。您应该将它们解析为:
// JSONval is a dictionary containing a single key called 'terms'
NSArray *terms = [JSONval objectForKey:@"terms"];
// Each element in the array is a dictionary with a single key
// representing a term identifier
for (NSDictionary *termId in terms) {
// Get the single dictionary in each termId dictionary
NSArray *values = [termId allValues];
// Make sure there's exactly one dictionary inside termId
if ([values count] == 1) {
// Get the single dictionary inside termId
NSDictionary *term = [values objectAtIndex:0];
NSString *content = [term objectForKey:@"content"]
…
}
}
根据需要添加进一步的验证
NSMutableDictionary*ResponseDataICT=[JSONval objectForKey:@“术语”]
但是“术语”
的价值不是字典;这是一个数组。注意JSON字符串中的方括号。你应使用:
NSArray *terms = [JSONval objectForKey:@"terms"];
相反
请注意,数组中的每个术语都是一个对象(字典),其中包含一个名称(键),其对应的值(对象)依次是另一个对象(字典)。您应该将它们解析为:
// JSONval is a dictionary containing a single key called 'terms'
NSArray *terms = [JSONval objectForKey:@"terms"];
// Each element in the array is a dictionary with a single key
// representing a term identifier
for (NSDictionary *termId in terms) {
// Get the single dictionary in each termId dictionary
NSArray *values = [termId allValues];
// Make sure there's exactly one dictionary inside termId
if ([values count] == 1) {
// Get the single dictionary inside termId
NSDictionary *term = [values objectAtIndex:0];
NSString *content = [term objectForKey:@"content"]
…
}
}
根据需要添加进一步的验证。您确定您使用的JSON解析器返回可变集合吗?您确定您使用的JSON解析器返回可变集合吗?不建议只提供链接的答案;最好是简要总结一下这是如何应用的,那些只是链接的答案是不被鼓励的;最好是简单总结一下这是如何实现的!我不清楚应该如何分解JSON字符串,但你的帖子真的很有帮助!!谢谢!这就是诀窍!我不清楚应该如何分解JSON字符串,但你的帖子真的很有帮助!!谢谢!