【发布时间】:2012-01-28 20:01:57
【问题描述】:
我创建了一个 WCF 服务,它为我的 POST 操作提供以下响应:
"[{\"Id\":1,\"Name\":\"Michael\"},{\"Id\":2,\"Name\":\"John\"}]"
我对 JSONObjectWithData 的调用没有返回任何错误,但我无法枚举结果,我做错了什么?
NSError *jsonParsingError = nil;
NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&jsonParsingError];
NSLog(@"jsonList: %@", jsonArray);
if(!jsonArray)
{
NSLog(@"Error parsing JSON:%@", jsonParsingError);
}
else
{
// Exception thrown here.
for(NSDictionary *item in jsonArray)
{
NSLog(@"%@", item);
}
}
【问题讨论】:
-
抛出什么类型的异常?
-
您的 NSLog 是否从 jsonArray 输出任何内容?
-
@Jacob-Relkin - 例外是:EXC_BAD_INSTRUCTION
-
@Jeremy - 这是 jsonArray 的输出: jsonList: [{"Id":1,"Name":"Michael"},{"Id":2,"Name":"John" }]
-
你确定你总是返回一个数组,即使响应只包含一个项目?
标签: objective-c ios ios5 nsjsonserialization