-5

I am trying to get some keys and values from below nested JSON response. Below I have mentioned my JSON response structure, I need to get the all keys(Red, Green) and key values(Color and ID) from the below response and load into the Array for tableview cell value.

FYI: I have tried by using NSDictionary but I am getting all the time unordered values. I need to get ordered values also. Please help me!

{
response: {

      RED: {

        Color: "red",
        color_id: "01",

             },

      GREEN: {

        Color: "green",
        color_id: "02",

              }
},

Colorcode: { },
totalcolor: "122"
}

My Code:

    NSError *error;
    NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSArray *responsData = [jsonDictionary objectForKey:@"response"];
    NSLog("%@",[responsData objectAtIndex:0]); // here I am getting bad exception 

    NSDictionary *d1 = responsData.firstObject;
    NSEnumerator *enum1 = d1.keyEnumerator;
    NSArray *firstObject = [enum1 allObjects];
Apple_Ajay
  • 227
  • 4
  • 17
  • Dictionaries are unordered, so if you want the order preserved, you have to change the original JSON so that the data within `response` is an array, not a dictionary. – Rob Jul 23 '15 at 12:21
  • Yes I agree. I am asking how can I get keynames and Keyvalues without mentioning 'keyname (like: Red)'. how to load that values into seperate array for tableview. If you have sample code share here! Thanks @Rob – Apple_Ajay Jul 23 '15 at 12:25
  • @Apple_Ajay Can you post the original JSON response ? The JSON in the question is not a valid JSON. – GoodSp33d Jul 23 '15 at 12:29
  • response = [responsData objectAtIndex:0]; response it is dictionary format in your json – Pravin Tate Jul 23 '15 at 12:30
  • Yes. Its not a valid because we should give quotes for all keys and key values. @GoodSp33d – Apple_Ajay Jul 23 '15 at 12:32
  • please check my answer I hope it will help you in that – Pravin Tate Jul 23 '15 at 12:37

2 Answers2

1

I have create JSON data through coding so don't consider it just check the following answer

 /// Create dictionary from following code
        /// it just for input as like your code
        NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
        NSMutableDictionary * innr = [[NSMutableDictionary alloc] init];
        [innr setObject:@"red" forKey:@"Color"];
        [innr setObject:@"01" forKey:@"color_id"];
        NSMutableDictionary * outer = [[NSMutableDictionary alloc] init];
        [outer setObject:innr forKey:@"RED"];

        innr = [[NSMutableDictionary alloc] init];
        [innr setObject:@"green" forKey:@"Color"];
        [innr setObject:@"02" forKey:@"color_id"];

        [outer setObject:innr forKey:@"GREEN"];

        [dict setObject:outer forKey:@"response"];

       // ANS ------ as follow
        // get keys from response dictionary
        NSMutableArray * key = [[NSMutableArray alloc] initWithArray:[dict[@"response"] allKeys]];

        // sort as asending order
        NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: YES];
        key =  (NSMutableArray *)[key sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];


        // access inner data from dictonary
        for (NSString * obj in key) {
            NSLog(@"%@",dict[@"response"][obj][@"Color"]);
            NSLog(@"%@",dict[@"response"][obj][@"color_id"]);
        }

I think you want same and it will help you!

Pravin Tate
  • 1,145
  • 8
  • 18
  • Awesome Answer. How to load that color and color_ID into seperate Array for tableview. I know its simple any way post here. It will helping others. Thank you. Great work @Pravin – Apple_Ajay Jul 23 '15 at 12:47
  • Create two array and fill those in that for loop. And if it helpfull for you please upvote or accept so other can refer it – Pravin Tate Jul 23 '15 at 12:50
  • @Apple_Ajay Do not load `color` and `color_id` into separate arrays. You want a single array of dictionaries (that way if you sort them, by color name or by color id, they'll stay together). – Rob Jul 23 '15 at 12:52
  • dict[@"response"][obj] fill it in array – Pravin Tate Jul 23 '15 at 12:53
  • NSArray *menuArray = jsonDictionary[@"response"][obj][@"Color"]; It's making bad exception into tableview delegate. @PravinTate – Apple_Ajay Jul 23 '15 at 13:02
  • What are you doing man as per your question you need access content from json with ascending format so as per your question I have give you correct answer. Now you are asking for something else. Which is not good. So please accept it and vote up. And edit your question so I will update my answer accordingly – Pravin Tate Jul 23 '15 at 13:08
  • See you should read my question fully. I have mentioned want to load into array for tableview. Please comment politely Stackoverflow its good for beginners.@PravinTate – Apple_Ajay Jul 23 '15 at 13:13
  • @Ajay just check your question already got 3 down vote means you are not asking question perfect but I know stack overflow is good for beginners. So for helping you I create new project and write code and post. stack not gave me any money for that, I just do it for you when people gaving down vote for your question and you are not accept or up vote my answer so my dear if you want to use my answer use it and just leave it. – Pravin Tate Jul 23 '15 at 13:17
  • Thank you @PravinTate. your answer good one. keep helping. Cool – Apple_Ajay Jul 23 '15 at 13:26
  • Help me how to load the value into tableview array @PravinTate – Apple_Ajay Jul 24 '15 at 05:52
0

If you're stuck with this JSON, if you want an array of the values, you can do the following:

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSDictionary *response = json[@"response"];
NSArray *colors = [response allValues];

If you need that array of colors sorted by color_id, for example, you can sort that yourself:

NSArray *sortedColors = [colors sortedArrayUsingDescriptors:@[[[NSSortDescriptor alloc] initWithKey:@"color_id" ascending:TRUE]]];
Rob
  • 415,655
  • 72
  • 787
  • 1,044