解析JSON并获取值

时间:2013-09-17 11:54:36

标签: objective-c json nsdictionary

我在解析我的JSON时遇到了麻烦。我想知道如何在“sel”对象中获取value1和value2(参见下面的JSON)。我一直这样做:

 SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: url]];
 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
 NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];


 NSMutableDictionary *json = [jsonParser objectWithString: json_string];

    //NSLog(@"%@", json);

    for(NSDictionary *dict in [json valueForKey:@"sel"])
    {
        NSLog(@"%@", [dict objectForKey: @"value1"]);
    }

这给了我一个错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x88314c0'

我如何达到value1和value2? 我的JSON看起来像这样:

{
        content =  {
                details = {
                    desc = "lorem";
                    name = lorem;
                    options = {
                        name = lorem;
                    };
                };
            };
        };
        sel =     {
            other =         (
            );
           content =         {
                id = 95;
                "date" = "2013";
                price = 100;
            };
            value1 = 100;
            value2 = 100;
        };
    }

0 个答案:

没有答案