John Z
John Z

Reputation: 125

Populating NSMutableArray from JSON Array

I have a JSON POST Request and it is returning the below description to me from this..

 NSDictionary *dicData = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

This is the NSLog...

I am trying to populate an additional NSMutableArray with just the thumbnail. But when I print out all keys the only key I get is 'mediaList'

Aren't 'full size and thumbnail other Keys?

2016-10-31 13:56:16.551008 MuzikLive MuzikLive[1444:794721] The dicData has this in it {
    mediaList =     (
                {
            fullsize = "/getMedia?file=/LivePic-19700101_014950899.jpg";
            thumbnail = "/getMedia?file=/thumbs/LivePic-19700101_014950899.jpg";
        },
                {
            fullsize = "/getMedia?file=/LivePic-19700101_014952658.jpg";
            thumbnail = "/getMedia?file=/thumbs/LivePic-19700101_014952658.jpg";
        },
                {
            fullsize = "/getMedia?file=/LivePic-19700101_014954431.jpg";
            thumbnail = "/getMedia?file=/thumbs/LivePic-19700101_014954431.jpg";
        },
 {
            fullsize = "/getMedia?file=/LiveVid-19700101_025111.mp4";
            thumbnail = "/getMedia?file=/thumbs/LiveVid-19700101_025111.jpg";
        }
    );
}

Upvotes: 2

Views: 84

Answers (2)

Syed Qamar Abbas
Syed Qamar Abbas

Reputation: 3677

According to this JSON view you should parse the dictionary according to this..

In Your Scenario parse your JSON dictionary like this.

@property(strong,nonatomic) NSMutableArray *arrayOfThumbnail;

-(void)parseJSONDict:(NSDictionary *)dicData{

    //Alloc new memory to your mutable Array
    self.arrayOfThumbnail = [[NSMutableArray alloc]init]; 
    // Get the array from json who key is "mediaList"
    NSArray = [dicData objectForKey:@"mediaList"];

    for(int index=0;index < array.count ; index++){
        /*In Loop this Array has a dictionary at every index
        {} curly braces in JSON indicates a Dictionary*/

            NSDictionary *dict = [array objectAtIndex:index];

         // Now Fetch the Thumbnail String by providing the keyword to the dictionary at that index

            NSString *thumbnailString = [dict objectForKey:@"thumbnail"];

            [self.arrayOfThumbnail addObject:thumbnailString];
        }


    }

Upvotes: 1

luk2302
luk2302

Reputation: 57114

Aren't 'full size and thumbnail other Keys - they are, but nested.
The top level dictionary dicData contains only data for the key mediaList.
The associated data is:

(
    {
        fullsize = "/getMedia?file=/LivePic-19700101_014950899.jpg";
        thumbnail = "/getMedia?file=/thumbs/LivePic-19700101_014950899.jpg";
    },
            {
        fullsize = "/getMedia?file=/LivePic-19700101_014952658.jpg";
        thumbnail = "/getMedia?file=/thumbs/LivePic-19700101_014952658.jpg";
    },
            {
        fullsize = "/getMedia?file=/LivePic-19700101_014954431.jpg";
        thumbnail = "/getMedia?file=/thumbs/LivePic-19700101_014954431.jpg";
    },
    {
        fullsize = "/getMedia?file=/LiveVid-19700101_025111.mp4";
        thumbnail = "/getMedia?file=/thumbs/LiveVid-19700101_025111.jpg";
    }
)

which is an array of other nested dictionaries which in turn each contain two keys fullsize and thumbnail.

You therefore have to

  • get the value for mediaList out of dicData
  • treat that data as an array
  • get each of the array entries
  • treat them as dictionaries
  • extract fullsize and thumbnail out of them

Upvotes: 2

Related Questions