将JSON数据解析为数组

时间:2022-02-22 15:33:57

I get the following error whenI launch my app

我启动应用程序时出现以下错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
    reason: '-   [COViewController fetchAppNetData]: unrecognized selector 
    sent to instance 0x716d200'

Basically I am unable to find out how to parse the JSON data to my array. The structure of my JSON is as follows

基本上我无法找到如何将JSON数据解析到我的数组。我的JSON结构如下

{

  "meta": {},

  "data": []

}

I know that meta is a dictionary and data is an array. But when I try to use the following piece of code I get the above error

我知道meta是字典,数据是数组。但是当我尝试使用下面的代码时,我得到了上面的错误

- (void)fetchAppNetData:(NSData *)responseData
{
 //parse JSON data
 NSError *error;
 NSDictionary* appNet_json = [NSJSONSerialization 
    JSONObjectWithData:responseData options:kNilOptions error:&error];
 NSArray* appNetTimeline = [[appNet_json objectForKey:@"meta"] 
    objectForKey:@"data"];
 NSLog(@"AppNet Timeline : %@",appNetTimeline);
}

How do I make sure that I can identify the structure of JSON properly next time, so that I can avoid this sort of issue? I am extremely sorry to come up with such kind of doubts

我如何确保下次可以正确识别JSON的结构,以便我可以避免这种问题?我非常抱歉想出这样的疑惑

1 个解决方案

#1


3  

The error has nothing to do with the content of the method -fetchAppNetData:. That method is not even getting called.

该错误与方法-fetchAppNetData:的内容无关。该方法甚至没有被调用。

The error is saying that you tried to invoke a method of that name on an object that doesn't respond to it. You've sent that message to an instance of class COViewController, but that's evidently not the class that implemented the method you posted.

错误是说您尝试在不响应它的对象上调用该名称的方法。您已将该消息发送到类COViewController的实例,但显然不是实现您发布的方法的类。

#1


3  

The error has nothing to do with the content of the method -fetchAppNetData:. That method is not even getting called.

该错误与方法-fetchAppNetData:的内容无关。该方法甚至没有被调用。

The error is saying that you tried to invoke a method of that name on an object that doesn't respond to it. You've sent that message to an instance of class COViewController, but that's evidently not the class that implemented the method you posted.

错误是说您尝试在不响应它的对象上调用该名称的方法。您已将该消息发送到类COViewController的实例,但显然不是实现您发布的方法的类。