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

238 IT屋

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];
解决方案

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响应下面获取一些键和值。下面我提到了我的 JSON 响应结构,我需要获取所有键(红色,绿色)和键< code> values(颜色和ID)来自以下响应,并加载到Array中以获取表格单元格值。



仅供参考:我已经尝试使用 NSDictionary ,但是我一直都在获取无序值。我还需要获取有序值。



  {
响应:{

RED:{

颜色:红色,
color_id: 01,

},

绿色:{

颜色:绿色,
color_id: 02,

}
},

颜色代码:{},
totalcolor: 122
}


我的代码:



  NSError *错误; 
NSDictionary * jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:& error];
NSArray * responsData = [jsonDictionary objectForKey:@ response];
NSLog(%@,[responsData objectAtIndex:0]); //在这里我得到了严重的异常

NSDictionary * d1 = responsData.firstObject;
NSEnumerator * enum1 = d1.keyEnumerator;
NSArray * firstObject = [enum1 allObjects];

解决方案

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



  ///从以下代码
创建字典///仅用于输入就像您的代码
NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
NSMutableDictionary * innr = [[NSMutableDictionary alloc] init];
[innr setObject:@ red forKey:@ Color];
[innr setObject:@ 01 forKey:@ color_id];
NSMutableDictionary * external = [[NSMutableDictionary alloc] init];
[外部setObject:innr forKey:@ RED];

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

[外部setObject:innr forKey:@ GREEN];

[dict setObject:out for Key:@ response];

// ANS ------如下
//从响应字典中获取密钥
NSMutableArray * key = [[NSMutableArray alloc] initWithArray:[dict [@响应] allKeys]];

//升序排序
NSSortDescriptor * sortOrder = [NSSortDescriptor sortDescriptorWithKey:@ self升序:是];
key =(NSMutableArray *)[key sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortOrder]];


//从字典中访问
的内部数据,以获取(NSString *键中的obj){
NSLog(@%@,dict [@ response ] [obj] [@颜色]);
NSLog(@%@,dict [@ response] [obj] [@ color_id]);;
}


我想您也一样,它将为您提供帮助!


本文地址:IT屋 » 如何从嵌套JSON获取值-目标C