【问题标题】:How to sort Json value in NSDictionary Objective c?如何在 NSDictionary Objective c 中对 Json 值进行排序?
【发布时间】:2018-01-04 09:31:53
【问题描述】:

我想按 location_name 对嵌套的 Json 数组进行排序,我的 json 在 nsdictionary 中

Json 数组是

apiResult:{
    Description = "List of Location";
    code = 200;
    locationList =     (
                {
            "location_id" = 481;
            "location_name" = "<null>";
            "pre_fixied" = "$3.00";
            "state_name" = Melbourne;
            status = 0;
            zone = "Zone 2";
            "zone_id" = 30;
        },
                {
            "location_id" = 461;
            "location_name" = "O'Halloran Hill";
            "pre_fixied" = "$5.00";
            "state_name" = Adelaide;
            status = 1;
            zone = "Zone 3";
            "zone_id" = 31;
        },
                {
            "location_id" = 460;
            "location_name" = "Sheidow Park";
            "pre_fixied" = "$5.00";
            "state_name" = Adelaide;
            status = 1;
            zone = "Zone 3";
            "zone_id" = 31;
        },
                {
            "location_id" = 459;
            "location_name" = "Hallett Cove";
            "pre_fixied" = "$5.00";
            "state_name" = Adelaide;
            status = 1;
            zone = "Zone 3";
            "zone_id" = 31;
        },
                {
            "location_id" = 458;
            "location_name" = "Eden Hills";
            "pre_fixied" = "$5.00";
            "state_name" = Adelaide;
            status = 1;
            zone = "Zone 3";
            "zone_id" = 31;
        },
                {
            "location_id" = 457;
            "location_name" = Glengowrie;
            "pre_fixied" = "$5.00";
            "state_name" = Adelaide;
            status = 1;
            zone = "Zone 3";
            "zone_id" = 31;
        }
    );
    message = "List of Location";
    status = Success;
} 

它按位置 id 降序出现我希望这个 json 按位置名称升序排列。 我有单独排序位置列表数组,但我想要整个 json 和 locationlist 按位置名称顺序

【问题讨论】:

  • 对整个 json 进行排序而不是仅对 locationList 进行排序有什么意义吗?因为我认为您只需要对 locationList 列表数组进行排序。
  • 但是我需要整个 json 来传递另一个页面
  • 整个json意味着你需要Description = "List of Location";code = 200;message = "List of Location";status = Success;这些都在另一个页面?
  • 是的,采用 NsMutableDictionary 格式
  • 然后创建另一个字典,对位置数组进行排序后,在该字典中添加DescriptioncodemessagestatussortedArray

标签: ios objective-c arrays json sorting


【解决方案1】:

试试这个

locationDescriptor = [[NSSortDescriptor alloc] initWithKey:@"location_id" ascending:YES];
sortDescriptors = [NSArray arrayWithObject: locationDescriptor];
sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];

现在根据需要将 sortedArray 更改为任何其他格式。

【讨论】:

  • 但是我需要带有排序位置列表的整个 json (nsmutableDictionary),我已经单独排序了 locationlist,但是我需要 nsmutabledictionary 格式的整个 json
  • 我只是排序了json的一部分。
【解决方案2】:

第一个短位置数组。

假设您的 json 结果是 NSDictionary *jsonResult; 并且过滤后的数组是 NSArray *filteredArray;

然后

    NSMutableDictionary *filteredJson = [NSMutableDictionary new];
    [filteredJson setObject:[jsonResult valueForKey:@"Description"] forKey:@"Description"];
    [filteredJson setObject:[jsonResult valueForKey:@"code"] forKey:@"code"];
    [filteredJson setObject:filteredArray forKey:@"locationList"];
    [filteredJson setObject:[jsonResult valueForKey:@"message"] forKey:@"message"];
    [filteredJson setObject:[jsonResult valueForKey:@"status"] forKey:@"status"];

【讨论】:

    猜你喜欢
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    相关资源
    最近更新 更多