【发布时间】:2013-12-20 17:31:32
【问题描述】:
我正在使用原生 NSURLConnectionDelegate 和 NSJsonSerialization。我收到的是 REST 响应,但不是它包含的所有值。
我在网络浏览器中有这个 JSON 响应:
{
"error" : {
"err_num" : 0,
"err_message" : ""
},
"company" : {
"id" : 1,
"name" : "company_string1"
},
"company" : {
"id" : 7,
"name" : "company_string2"
},
"company" : {
"id" : 19,
"name" : "company_string3"
},
"company" : {
"id" : 13,
"name" : "company_string4"
},
"company" : {
"id" : 14,
"name" : "company_string5"
}
}
我正在异步使用NSURLConnection并实现
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];
} // receivedData is NSMutuableData initialized before
在connectionDidFinishLoading 我愿意:
NSMutableDictionary *array = [NSJSONSerialization JSONObjectWithData:self.receivedData options:NSJSONReadingMutableContainers error:&error];
当我登录时,我得到了
{
"err_message" = "";
"err_num" = 0;
}
{
id = 14;
name = "company_string5";
}
如果我使用NSJSONReadingAllowFragments 我会得到
{
"err_message" = "";
"err_num" = 0;
}
{
id = 1;
name = company_string1;
}
还检查了NSDictionary,但我得到了相同的结果。由于重复键,他们在这里的 JSON 有问题吗,它只返回最后一个/第一个吗?我也碰巧在网上查了一下,几乎都说它是有效的 JSON。它没有给出任何错误!
【问题讨论】:
标签: ios5 nsurlconnection nsjsonserialization