简体   繁体   English

如何成功将JSON数据转换为Objective-C对象?

[英]How to successfully convert JSON data to Objective-C objects?

So the jist of what I'm doing is simple, getting some data from a server in JSON format. 所以我正在做的事情很简单,从JSON格式的服务器获取一些数据。 I'm using the default classes in Objective-C (specifically NSJSONSerialization) to convert the responseData gotten into JSON format. 我正在使用Objective-C中的默认类(特别是NSJSONSerialization)将responseData getten转换为JSON格式。

So basically- 所以基本上 -

   NSString* testURL=@"http://ws.audioscrobbler.com/2.0/?method=track.getsimilar&artist=Kendrick+Lamar&track=A.D.H.D&api_key=e3f53f2f2896b44ff158a586b8ee15c7&format=json";

   NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:testURL]];

   NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:responseData //1
                      options:kNilOptions
                      error:&error];

   NSArray* songList = [json objectForKey:@"similartracks"];

Problem is, later when I try to access an indivdual object in the array of JSOn data, like so, 问题是,稍后当我尝试访问JSOn数据数组中的一个单独对象时,就像这样,

   NSDictionary* song1 = [songList objectAtIndex:0];

It's giving me this error, 它给了我这个错误,

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x96e2b60' *由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例0x96e2b60'

Any ideas as to why this is happening? 关于为什么会发生这种情况的任何想法?

I appreciate the help, coffeejay 我很感激帮助,coffeejay

似乎[json objectForKey:@"similartracks"]返回一个NSDictionary,尝试这样的事情:
NSArray* songList = [[json objectForKey:@"similartracks"] objectForKey:@"track"]

When dealing with such things, you should always validate your response before trying to do something with it, you should never trust any input. 在处理这些事情时,你应该总是在尝试使用它之前验证你的响应,你永远不应该相信任何输入。 In your case, you're trying to send objectAtIndex: message to an NSDictionary instance, which raises unrecognised selector exception. 在您的情况下,您尝试将objectAtIndex:消息发送到NSDictionary实例,这会引发无法识别的选择器异常。

A simple way to avoid such crashes is to check the class of the returned object, for instance: 避免此类崩溃的一种简单方法是检查返回对象的类,例如:

id song1 = json[@"similartracks"]
if([song1 isKindOfClass:[NSArray class]])
{
    //Now we're sure it's an array
}

您声明返回的数据是data ,但是您正在向NSJSONSerialization方法传递一个名为responseData的变量。

Your json probably doesn't return an array at yhat point but a dictionary. 你的json可能不会在yhat点返回一个数组,而是一个字典。 Show the json content so we can help further. 显示json内容,以便我们进一步提供帮助。

(NsJSonSerialization returns id and can be either an array or dictionary. The contents again will be arrays or dictionaries) (NsJSonSerialization返回id,可以是数组或字典。内容也将是数组或字典)

Your error message says that songList is a dictionary and not an array. 您的错误消息表明songList是字典而不是数组。 Check the data structure. 检查数据结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM