繁体   English   中英

如何在NSDictionary目标c中对Json值进行排序?

[英]How to sort Json value in NSDictionary Objective c?

我想按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

尝试这个

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

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

第一个简短的位置数组。

假设您的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"];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM