0

I have an iOS application which downloads and parses a JSON file from the Facebook API.

However, whenever I try to parse it my app crashes and I get this error:

2013-11-06 15:31:41.473 JSONTESTER[10881:70b] -[__NSCFDictionary objectAtIndex:]: unrecognized 
selector sent to instance 0x10d01bd30
2013-11-06 15:31:41.476 JSONTESTER[10881:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x10d01bd30'

Here is the simple JSON file I am trying to parse:

{


"id": "xxxxxxxxxx",
   "name": "Dan Sadjadian",
   "home": {
      "data": [
         {
            "id": "xxxxxxxxxxxxxxxxxxxxxx",
            "from": {
               "name": "Jane Smith",
               "id": "xxxxxxxxxxxxxxx"
            },
            "message": "deadline is only 16 days!!! Where are you guys!!!!!!!!! Please shall we start!!!!!",
            "actions": [
               {
                  "name": "Comment",
                  "link": "https://www.facebook.com/xxxxxxx/posts/xxxxxxxxxxx"
               },
               {
                  "name": "Like",
                  "link": "https://www.facebook.com/xxxxxxxxx/posts/xxxxxxxxxxxx"
               }
            ],
            "privacy": {
               "value": ""
            },
            "type": "status",
            "status_type": "mobile_status_update",
            "created_time": "2013-11-06T14:40:23+0000",
            "updated_time": "2013-11-06T14:40:23+0000"
         },
       ],
     },
}

Here is my code which should work....

cell.username.text = [[[[[facebook_array objectAtIndex:index] valueForKey:@"data"] objectAtIndex:0] valueForKey:@"from"] valueForKey:@"name"];

As you can see I am trying to get access to "name" which is in "from" which is in the "data" array which is in "home".

What am I doing wrong?

Thanks, Dan.

4

3 回答 3

2

它应该是

NSArray *arrData=[MAIN_DICTIONARY objectForKey:@"data"];

NSDictionary *data=[arrData objectForIndex:0];

NSDictionary *from=[data objectForKey:@"from"];

NSLog(@"Name = %@", [from objectForKey:@"name"]);

我希望它可以帮助您了解它的工作原理。

于 2013-11-06T16:03:53.183 回答
1

I think the first step is to select home object not [index].

given your json file above.

You are doing [index].data.[0].from.name

Your data look like home.data.[0].from.name

On linux/gnustep this is how I get the home.data.[0].from.name property, I saved your example as fb.json as above:

NSError * error;

NSData * jsdata = [NSData dataWithContentsOfFile:@"fb.json"];
id jsDict = [NSJSONSerialization JSONObjectWithData:jsdata
                                            options:0
                                              error:&error];
// not: data.from.name
// but: home.data.from.name
NSDictionary * home = [jsDict objectForKey:@"home"];
NSArray * data = [home objectForKey:@"data"];
NSDictionary * firstObj = [data objectAtIndex:0];
NSDictionary * from = [firstObj objectForKey:@"from"];
NSString * name = [from objectForKey:@"name"];

NSLog( @"%@", name);

output:

 Jane Smith

If the whole json object from the example is in array in the actual data from fb then you would start with [index] but still the next needs to be selecting home.

于 2013-11-06T23:26:53.573 回答
0

很明显 NSDictionary/NSMutableDictionary 类没有 objectAtIndex 方法。它来自 NSArray。我猜你只能使用 objectForKey 在 JSON 中移动。

于 2013-11-06T15:52:50.447 回答