displaying JSON data with the help of dictionaries and array

不想你离开。 提交于 2019-12-02 23:19:04

问题


I get the following error

    [__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x75a8e20
    2013-04-20 08:56:14.90 MyApp[407:c07] *** Terminating app due to uncaught 
    exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: 
    unrecognized selector sent to instance 0x75a8e20'

This is my first hands on working with JSON. I get the above mentioned error when I try to run the first piece of code where URL is a flickr url. When I use the photos as key it print the array and app abruptly quits.

#define flickrPhotoURL [NSURL URLWithString: @"http://api.flickr.com/services/rest/?format=json&sort=random&method=flickr.photos.search&tags=rocket&tag_mode=all&api_key=12345&nojsoncallback=1"]

- (void)viewDidLoad
{
   [super viewDidLoad];
   //this line of code will be executed in the background to download the contents of the flickr URL
   dispatch_async(flickrBgQueue, ^{
   NSData* flickrData = [NSData dataWithContentsOfURL:flickrPhotoURL]; //NOTE: synchronous method...But we actually need to implement asynchronous method
   [self performSelectorOnMainThread:@selector(appFetchedData:) withObject:flickrData waitUntilDone:YES]; //when data is available "appFetchedData" method will be called
});

}

- (void)appFetchedData: (NSData *)responseData 
{
 //parsing JSON data
 NSError *error_parsing;
 NSDictionary *flickr_json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error_parsing];
 NSArray* photo_information = [flickr_json objectForKey:@"photos"];

 NSLog(@"Photo Information: %@",photo_information);

 NSDictionary* photo = (NSDictionary*)[photo_information objectAtIndex:0];

 humanReadable.text = [NSString stringWithFormat:@"Owner is %@",[photo objectForKey:@"Owner"]];
}

However when I run the same piece of code by replacing the key "photos" with "loans" and use the following URL and code

#define flickrPhotoURL [NSURL URLWithString: @"http://api.kivaws.org/v1/loans/search.json?status=fundraising"]


- (void)appFetchedData: (NSData *)responseData 
{
 //parsing JSON data
 NSError *error_parsing;
 NSDictionary *flickr_json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error_parsing];
 NSArray* photo_information = [flickr_json objectForKey:@"loans"];

 NSLog(@"Photo Information: %@",photo_information);

 NSDictionary* photo = (NSDictionary*)[photo_information objectAtIndex:0];

 humanReadable.text = [NSString stringWithFormat:@"loan amount is %@",[photo objectForKey:@"loan_amount"]];

}

, the app sets the correct information on the humanredable.text property. Am I using the wrong key for the first JSON ?


回答1:


Firstly, thanks for publishing your Flickr API key as-is! It will be super useful for me to perform identity theft some day.

Second, another big thanks for not having read the data you got back. It starts like this:

{"photos":{"page":1, "pages":1792, "perpage":100,
 ^^^^^^^^^^

So the object for the key photos is a dictionary, not an array, thus,

NSArray* photo_information = [flickr_json objectForKey:@"photos"];

is wrong. Did you mean this instead:

NSArray* photo_information = [[flickr_json objectForKey:@"photos"]
                               objectForKey:@"photo"];

? Also, below when you construct the human readable description,

[photo objectForKey:@"Owner"]

is wrong, it should be

[photo objectForKey:@"owner"]

instead.



来源:https://stackoverflow.com/questions/16121359/displaying-json-data-with-the-help-of-dictionaries-and-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!