Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get values of particular key in nsdictionary

I saved the json parsing result in a dictionary which look like:

{
 "statusCode":"200",
"body":[
  {
     "status":"success",
     "remarks":null
  }
],

"data":[
     "abcd":[
        {
           "category":"a",
           "title":"b",
           "id":"24"
        },
        {
           "category":"c",
           "title":"crd",
           "id":"65"
        },
        {
           "category":"ds",
           "title":"sd",
           "id":"18"
        }

     ]
  },
  {
     "efgh":[
        {
           "category":"ds",
           "title":"sd",
           "id":"18"
        },
        {
           "category":"sd",
           "title":"sd",
           "id":"1"
        }

                             ]
  },
  {
     "ijkl":[
        {
           "category":"ds",
           "title":"sd",
           "id":"18"
        },
        {
           "category":"sd",
           "title":"sd",
           "id":"1"
        }

     ]
  }
 ]
}

The data for key @"data" can be saved into an array by using

NSMutableArray *getdata=[[NSMutableArray alloc]init];
getcat=[results objectForKey:@"data"];

Now what should I do for the values(category, title, id) inside the first index i.e. "abcd".

If anyone has any knowledge please look upon that.

Thanks to all.

like image 694
Minkle Garg Avatar asked Apr 26 '13 09:04

Minkle Garg


2 Answers

Following will give you the desired object

NSDictionary *dict=[results valueForKeyPath:@"data.abcd"][0];

For individual:

NSString *categoryString=[[results valueForKeyPath:@"data.abcd"][0] objectForKey:@"Category"];
NSString *idString=[[results valueForKeyPath:@"data.abcd"][0] objectForKey:@"id"];
NSString *titleString=[[results valueForKeyPath:@"data.abcd"][0] objectForKey:@"title"];

Also,

NSString *categoryString=dict[@"Category"];
NSString *idString=dict[@"id"];
NSString *titleString=dict[@"title"];
like image 91
Anoop Vaidya Avatar answered Oct 21 '22 13:10

Anoop Vaidya


Check like this:

NSString *value=[[[getcate valueForKey:@"abcd"] objectAtIndex:0] valueForKey:@"category"];
like image 3
Balu Avatar answered Oct 21 '22 13:10

Balu