【问题标题】:How to get the values from nested JSON - Objective c如何从嵌套的 JSON 中获取值 - 目标 c
【发布时间】:2015-10-13 17:24:17
【问题描述】:

我正在尝试从嵌套的 JSON 响应下方获取一些键和值。下面我提到了我的JSON 响应结构,我需要从下面的响应中获取所有keys(Red, Green) 和键values(Color and ID) 并加载到表格视图单元格值的数组中。

仅供参考:我尝试过使用NSDictionary,但我一直得到无序值。我还需要获得有序值。请帮帮我!

{
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,使response中的数据是一个数组,而不是字典。
  • 是的,我同意。我在问如何在不提及“键名(如:红色)”的情况下获取键名和键值。如何将这些值加载到 tableview 的单独数组中。如果你有示例代码分享在这里!谢谢@Rob
  • @Apple_Ajay 你能发布原始的 JSON 响应吗?问题中的 JSON 不是有效的 JSON。
  • 响应 = [responsData objectAtIndex:0];响应它是您的 json 中的字典格式
  • 是的。它无效,因为我们应该为所有键和键值提供引号。 @GoodSp33d

标签: ios objective-c json


【解决方案1】:

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

 /// 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"]);
        }

我想你也想要,它会帮助你!

【讨论】:

  • 很棒的答案。如何将该颜色和 color_ID 加载到 tableview 的单独数组中。我知道在这里发布任何方式都很简单。它会帮助别人。谢谢你。伟大的工作@Pravin
  • 创建两个数组并将它们填充到那个 for 循环中。如果它对您有帮助,请点赞或接受,以便其他人可以参考
  • @Apple_Ajay 不要将colorcolor_id 加载到单独的数组中。您需要一个字典数组(这样,如果您按颜色名称或颜色 ID 对它们进行排序,它们将保持在一起)。
  • dict[@"response"][obj] 填入数组
  • NSArray *menuArray = jsonDictionary[@"response"][obj][@"Color"];它对 tableview 委托产生了严重的异常。 @PravinTate
【解决方案2】:

如果你被这个 JSON 卡住了,如果你想要一个值数组,你可以执行以下操作:

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

如果您需要按color_id 排序的颜色数组,例如,您可以自己排序:

NSArray *sortedColors = [colors sortedArrayUsingDescriptors:@[[[NSSortDescriptor alloc] initWithKey:@"color_id" ascending:TRUE]]];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多