iOS JSON解析成NSDictionary,然后用SBJson解析NSArray
问题描述:
它应该是如此简单,但我无法得到它的工作。 JSON响应为 ([{“id”:“1”,“x”:“1”,“y”:“2”},{“id”:2,“x”:“2” , “Y”: “4”}])iOS JSON解析成NSDictionary,然后用SBJson解析NSArray
NSString *response = [request responseString];
//response is ([{"id":"1", "x":"1", "y":"2"},{"id":2, "x":"2", "y":"4"}])
SBJSON *parser = [[[SBJSON alloc] init] autorelease];
NSDictionary *jsonObject = [parser objectWithString:response error:NULL];
// jsonObject doesn't have any value here..Am I doing something wrong?
NSMutableArray Conversion = [jsonObject valueForKey:NULL];
//Even if I get the value of jsonObject. I don't know what to put for valueForKey here
转换768,16有两个NSObjects..and它们中的每应具有像
ID:1 X:1 Y:2
和
id:2 x :2 y:4
答
您的JSON解析器将从您的响应字符串中产生一个NSArray,而不是NSDictionary。请注意,JSON解析器(包括SBJSON)将根据正在解析的json的内容返回数组对象或字典对象。
NSArray *jsonObject = [parser objectWithString:response error:nil];
然后,您可以访问您的阵列中的各个项目(数组元素将是类型NSDictionary中的),并使用valueForKey:
获得每个项目的属性。
NSDictionary *firstItem = [jsonObject objectAtIndex:0];
NSString *theID = [firstItem objectForKey:@"id"];
尝试,并获得无效CFArrayRef为jsonObject..It似乎JSONObject的是没有得到任何东西。我用的断点检查,并能看到解析器得到了JSON值.. – MomentH 2012-04-23 22:23:27
我知道了..你的答案是完美的: )但Json返回我得到了(和)所以解析器无法处理它..我已经删除与响应= [响应stringByReplacingOccurrencesOfString:@“(”withString:@“”];和响应= [响应stringByReplacingOccurrencesOfString:@“)”withString :@ “”]; **然后它的工作完美..谢谢.. :)是否有一些简单的方法来删除'('和')'在同一时间? – MomentH 2012-04-23 22:43:45