0
我正在使用原生NSURLConnectionDelegate
和NSJsonSerialization
。我得到REST响应,但不是它包含的所有值。NSJSONSerialization解析不返回所有值的响应数组/字典
我在Web浏览器此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。它没有给出任何错误!
这也正是同样的JSON响应该我进入浏览器,我推测它最初是一个无效的JSON,但现在我认为我是正确的。它应该是一个嵌套的JSON,只有一个“公司”的关键字。 – Vacca
@Vacca什么应该嵌套JSON?如果字典中有多个相同的键(JSON“对象”),那么这是无效的JSON。 –