get values of particular key in nsdictionary

65,645

Solution 1

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"];

Solution 2

Check like this:

NSString *value=[[[getcate valueForKey:@"abcd"] objectAtIndex:0] valueForKey:@"category"];
Share:
65,645
Minkle Garg
Author by

Minkle Garg

Updated on April 19, 2020

Comments

  • Minkle Garg
    Minkle Garg about 4 years

    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.

  • Minkle Garg
    Minkle Garg about 11 years
    Yes I done this already.. NSMutableArray *firstarray=[getcat objectAtIndex:0]; "abcd":[ { "category":"a", "title":"b", "id":"24" }, { "category":"c", "title":"crd", "id":"65" }, { "category":"ds", "title":"sd", "id":"18" } ] But now how to get value inside the abcd i.e. category, title, id
  • borrrden
    borrrden about 11 years
    That's another Dictionary. I don't understand why this gives so many people so much trouble. You already did the exact same thing in this line -> getcat=[results objectForKey:@"data"]; so just do it again. JSON is not rocket science. You can only get about 5 or 6 different types out, and only two collection types. So collections are either arrays (get by number) or dictionaries (get by string).
  • Minkle Garg
    Minkle Garg about 11 years
    I am unable to get the value inside the dictionary i.e. category, id, title.
  • Anoop Vaidya
    Anoop Vaidya about 11 years
    nslog dict, as shown in my answer, and tell what gets printed.
  • Minkle Garg
    Minkle Garg about 11 years
    I got the inside key values by NSString *cat=[dict valueForKeyPath:@"id"][0]; It means to get all the id for a particular keys for example all the ids under "abcd" I have to use for loop.
  • MD.
    MD. over 9 years
    How to get the "abcd" key name from data? Because i want to show "abcd" as my tableview section title. in my case "abcd" and "efgh" is dynamic.