如何使用NSJSONSerialization从JSON对象中获取值

4

我可以获取JSON对象并将其显示,但我该如何从“lat”和“lng”中获取值以在Xcode中使用?

我的代码:

NSString *str=[NSString stringWithFormat:@"https://www.<WEBSITE>];
NSURL *url=[NSURL URLWithString:str];
NSData *data=[NSData dataWithContentsOfURL:url];
NSError *error=nil;

NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData:data
                                                           options:kNilOptions
                                                             error:&error];
NSLog(@"Your JSON Object: %@ Or Error is: %@", dictionary, error);

JSON对象:

(
    {
    response =         {

        lat = "52.517681";
        lng = "-115.113995";

    };
}

我似乎无法访问任何数据。我已经尝试过:

NSLog(@"Value : %@",[dictionary objectForKey:@"response"]);

我也尝试了很多变化,比如:

NSLog(@"Value : %@",[[dictionary objectForKey:@"response"] objectForKey:@"lat"]);

但最终总是会崩溃:

-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x15dd8b80

我注意到在调试过程中,“字典”只包含一个对象。如何将我的JSON对象转换为具有键值对的NSDictionary?这个JSON对象是格式错误还是其他什么问题?


有没有response上面的键?如果你在该行设置断点并悬停在NSDictionary对象上,你应该能够看到其内容。 - Kevin DiTraglia
1
你看到了什么?这是一个包含一个元素的数组,该元素是一个名为“response”的条目的字典。该条目又是一个包含两个元素“lat”和“lng”的字典。 - Hot Licks
1
你的问题显然是你没有剥离数组。(你在上面称之为“字典”的东西其实是一个数组。) - Hot Licks
看起来你反序列化的数据实际上不是字典而是数组。 你可以尝试打印字符串而不是对其进行反序列化: 使用NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; - cescofry
如何访问数组元素? - Hot Licks
显示剩余5条评论
3个回答

5
那个特定的JSON对象是一个NSArray,而不是NSDictionary,所以它无法识别选择器,你没有收到警告,因为NSJSONSerialization JSONObjectWithData返回一个id。
尝试:
NSArray *array = [NSJSONSerialization JSONObjectWithData:data
                                                 options:kNilOptions
                                                   error:&error];
NSDictionary *dictionary = array[0];

@Hbombre 还要注意的是,在现代编程中,我们通常喜欢使用方括号表示法来访问字典对象,例如 dictionary[@"response"]。这样通常更易读,但显然这是一种风格选择,你不必遵守。 - JuJoDi

0

看起来返回的JSON不是一个NSDictionnary对象,而是一个NSArray。请将您的代码更改为以下内容:

NSarray *responseArray = [NSJSONSerialization JSONObjectWithData:data
                                                           options:kNilOptions
                                                             error:&error];

responseArray包含一个字典对象(或者多个?),然后你可以像这样访问它们:
For ( NSDictionary *dic in responseArray){
    NSDictionnary *response = [dic objectForKey@"response"];
....
}

-3
这是我编写的应用程序的一部分...基本上获取一个NSData对象,通常来自响应,并使用NSJSONSerialization JSONObjectWithData: Options: Error,会生成一个NSDictionary。
NSError *error = nil;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];

//Make sure response came in
if(response != nil){
    NSError *error = nil;
    id data = [NSJSONSerialization JSONObjectWithData:response options:0 error:&error];

    if([data isKindOfClass:[NSDictionary class]]){
        //Create dictionary from all the wonderful things google provides
        NSDictionary *results = data;

        //Search JSON here
        NSArray *cityAndState = [[[[results objectForKey:@"results"] objectAtIndex:0] objectForKey:@"address_components"] valueForKey:@"short_name"];

如果您认为这篇文章还有改进的空间,请考虑留下评论。 - Jason Renaldo
2
好的,现在它几乎是不可见的。 - Hot Licks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接