从JSON读取值 - 帮助编写逻辑

时间:2012-02-19 18:46:54

标签: iphone objective-c

我的格式为

JSON
{"index":"0","name":"jemmy","age":"2"}

我需要提取存储的值并保存它;我做了以下,但它不起作用

  
      
  • 由于未捕获的异常而终止应用   'NSInvalidArgumentException',原因:' - [NSCFString objectForKey:]:   无法识别的选择器发送到实例0x6671a10'
  •   

我的代码

    NSDictionary *dictionary = [request responseHeaders];
    SBJsonParser *parser = [SBJsonParser new];     
    id content = [responseString JSONValue];
    NSDictionary *dealDictionary = content;
    NSArray *array = [dealDictionary allKeys];
    for (NSString *str in array)
    {
        NSDictionary *childDictionary = [dealDictionary objectForKey:str];
        NSLog(@"%@ ",[childDictionary objectForKey:@"name"]);
    }

1 个答案:

答案 0 :(得分:1)

您可能不了解如何使用SBJSON,也许快速阅读documentation会很有用。首先,您需要实际使用您创建的解析器:

SBJsonParser *parser = [SBJsonParser new];
NSDictionary *content = [parser objectWithString:[request responseString]];

其次,您可以更轻松地遍历您的字典:

NSEnumerator *enum = [content keyEnumerator];
id key;
while (key = [enum nextObject]) {
    NSLog(@"key %@ and object %@",key,[content objectForKey:key]);
}