iOS JSON Parse into NSDictionary,然后使用 SBJson 解析 NSArray



它应该很简单,但我无法让它工作。

JSON 响应是([{"id":"1"

, "x":"1", "y":"2"},{"id":2, "x":"2", "y":"4"}])
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

转换应该有两个 NSObject..他们每个人都应该有喜欢

编号:1X:1Y:2

编号:2X:2Y: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"];

最新更新