如何从嵌套JSON获取值-目标C [英] How to get the values from nested JSON - Objective c

查看:483
本文介绍了如何从嵌套JSON获取值-目标C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从嵌套的JSON响应下面获取一些键和值.下面我已经提到了我的JSON响应结构,我需要从下面的响应中获取所有keys(Red, Green)和键values(Color and ID),并将其加载到Array中以获取tableview单元格值.

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.

仅供参考::我尝试使用NSDictionary进行操作,但是我一直都在获取无序值.我还需要获取有序值.请帮帮我!

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"
}

我的代码:

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

推荐答案

我已经通过编码创建了JSON数据,所以不要认为只是检查以下答案

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!

这篇关于如何从嵌套JSON获取值-目标C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆