Lee
Lee

Reputation: 149

Retrieving keys from NSDictionary

I'm having a hard time managing to pull anything out of a JSON response when querying TMDb. Despite spending most of the weekend searching these forums and others, I am no nearer a solution.

In my header file I have:

@property (nonatomic,strong) NSDictionary * fetchedData;

The dictionary is populating correctly, since the first part of the JSON response looks like this when I use:

NSLog(@"fetchedData: %@", fetchedData);


fetchedData: {
  page = 1;
  results = (
    {
      adult = 0;
      id = 1245;
      "known_for" = (
        {
          adult = 0;
          "backdroppath" = "/hbn46fQaRmlpBuUrEiFqv0GDL6Y.jpg";
          "genreids" = ( 878, 28, 12 );
          id = 24428;
          "mediatype" = movie;
          "original_language" = en;
          "original_title" = "The Avengers";

I've been trying numerous ways to retrieve all instances of "original_title" (or any of the keys) in the response, but I have no hair left to pull out when every attempt is returning NULL, so any suggestions are welcome!

Upvotes: 1

Views: 84

Answers (2)

user3182143
user3182143

Reputation: 9589

Getting data from Response

I think fetchedData is dictionary.So

NSString *strOriginalTitle = [NSString stringWithFormat:@"%@",[[[[[fetchedData valueForKey:@"results"]objectAtIndex:0]valueForKey:@"known_for"]objectAtIndex:0]valueForKey:@"original_title"];

      //OR By getting data step by step

NSArray *arrayResults = [fetchedData valueForKey:@"results"];
NSDictionary *dict = [[arrayResults objectAtIndex:0]  copy];
NSArray *arrayKnownFor = [dict valueForKey@"known_for"] copy];
NSString *strOriginalTitle = [arrayKnownFor objectAtIndex:0]valueForKey:@"original_title"];
NSLog(@"The original_title is - %@",strOriginalTitle);

Upvotes: 1

kirander
kirander

Reputation: 2256

Try this.

NSString *title = fetchedData[@"results"][0][@"known_for"][0][@"original_title"];

Upvotes: 3

Related Questions