对于我的项目,我使用PHP构建了自己的api。 JSON编码的结果基本上是给我的条目类似下面使用Objective-C解析JSON
{"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;
}
}
}
哪里的NSLog吐出了错误,当我送objectForKey到responseDataDict,这是根据日志__NSArrayM。
我在这里做错了什么?
你确定你使用的JSON解析器返回可变集合吗? – 2011-10-20 06:49:15