Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

ios - How to get the values from nested JSON - Objective c

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];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...