我得到了一个预测形式的JSON响应。所以这棵树是这样的:
{ "data":{
"img":"image":"http:\/\/testingpage.com\/images\/frog_320x480_23.jpg",
"link":"http:\/\/google.com","verbose":"Hi there"},
"figures":[
{ "ID":"16","TYPE":"sailor","COLOR":"1E90FFFA","ICON":"sailor"},
{"ID":"32","TYPE":"pilot","COLOR":"32CD32BC","ICON":"pilot"}
]
}
}我正在使用SBJSON库。
我得到了我的价值观
NSString *jsonString = [NSString alloc initWithData:iData编码:NSUTF8StringEncoding];NSDictionary *results = jsonString JSONValue;
拿到我需要的钥匙:
结果objectForKey:@"keyname"]
到目前一切尚好。
刚才发生的事情是,我从一个不同的提供者那里得到了一个回应,结果在本质上完全不同:
[
{ "TEMPValues": {
"temp1": 13.2,
"temp2":11.1,
"temp3":11.2,
"temp4":13.4
},
"semipath":"pollution",
"value":"AXT"
},
{ "TEMPValues": {
"temp1":19.3,
"temp2":12.1,
"temp3":10.8,
"temp4":13.1},
"semipath":"pollution",
"value":"AUT"
}
]我这里有两个问题:
TEMPValues的第一个对象的TEMPValues。results objectForKey:@"keyname"]获得一个值时,我都会得到一个SIGABRT,因为results被识别为一个数组发布于 2014-06-23 12:36:22
首先创建值数组
NSArray *results = (NSArray *)[jsonString JSONValue];之后,迭代这个数组。
for(int i= 0; i < results.count; i++) {
NSDictionary *dic = (NSDictionary *)[results objectAtIndex:i];
}发布于 2014-06-23 12:43:38
你必须做类似的事情。
-(void) retrieveData{
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"yoururl"]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"TEMPValues"];
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];
variable=[[jsonArray objectAtIndex:0]objectForKey:@"keyname"];
}https://stackoverflow.com/questions/24366059
复制相似问题