对JsonObjects Java的JsonArray进行排序

时间:2014-04-16 19:24:53

标签: java json spring spring-mvc gson

我正在使用Gson Library。我需要根据JsonObjects中的键对JsonObjects的JsonArray进行排序。下面给出了JsonArray

[{"AS1000":["5.1","0","false","false",null,null],"AS2000":["-","-","-","-","-","-"],"Date":"2014-03-02 08:07:00.0"},
{"AS1000":["5.0","0","false","false",null,null],"AS2000":["-","-","-","-","-","-"],"Date":"2014-03-02 17:58:00.0"},
{"AS1000":["5.0","0","false","false",null,null],"AS2000":["-","-","-","-","-","-"],"Date":"2014-03-30 14:59:00.0"},
{"AS1000":["-","-","-","-","-","-"],"AS2000":["27.0","0","true","false","30.0 - 20.0",null],"Date":"2014-03-30 06:03:00.0"},
{"AS1000":["-","-","-","-","-","-"],"AS2000":["15.0","0","false","false",null,null],"Date":"2014-03-02 02:00:00.0"},
{"AS1000":["5.0","0","false","false",null,null],"AS2000":["-","-","-","-","-","-"],"Date":"2014-03-14 20:06:00.0"},
{"AS1000":["5.0","0","false","false",null,null],"AS2000":["-","-","-","-","-","-"],"Date":"2014-03-30 07:00:00.0"},
{"AS1000":["-","-","-","-","-","-"],"AS2000":["61.0","0","false","false",null,null],"Date":"2014-03-17 19:07:59.0"}]

是否可以根据对象中的键进行排序,例如AS2000 [0]或Date?

我试过像这样的Boon

        JsonArray listLoggerDataObj=gson.fromJson(listloggerdata, JsonArray.class);
        List<JsonObject> list=new ArrayList<JsonObject>();
         for (JsonElement entry : listLoggerDataObj) {
         list.add(entry.getAsJsonObject());
         }


        sort(list,"Date"); /*list ,key used to sort*/
        System.out.println("printing sorted" +list.toString());

但它打印相同的输入。任何想法?

谢谢,

Gowrish

2 个答案:

答案 0 :(得分:1)

找到一个解决方案。使用Boon的jsonParser从字符串构造Json(而不是Gson的fromJson()),然后将其转换为List,最后应用sort(),Issue Fixed。

  Object jsonObject = fromJson(listloggerdata);
  List<?> jsonDepartments = (List<?>) jsonObject;

  sort(jsonDepartments,sortBy("Date"));

  System.out.println("printing sorted" +jsonDepartments.toString());

排序后的结果是

[{"AS2000":["15.0","0","false","false",null,null],"AS1000":["-","-","-","-","-","-"],"Date":"2014-03-02 02:00:00.0"},
 {"AS2000":["-","-","-","-","-","-"],"AS1000":["5.1","0","false","false",null,null],"Date":"2014-03-02 08:07:00.0"},
 {"AS2000":["-","-","-","-","-","-"],"AS1000":["5.0","0","false","false",null,null],"Date":"2014-03-02 17:58:00.0"},
 {"AS2000":["-","-","-","-","-","-"],"AS1000":["5.0","0","false","false",null,null],"Date":"2014-03-14 20:06:00.0"},
 {"AS2000":["61.0","0","false","false",null,null],"AS1000":["-","-","-","-","-","-"],"Date":"2014-03-17 19:07:59.0"},
 {"AS2000":["27.0","0","true","false","30.0 - 20.0",null],"AS1000":["-","-","-","-","-","-"],"Date":"2014-03-30 06:03:00.0"},
 {"AS2000":["-","-","-","-","-","-"],"AS1000":["5.0","0","false","false",null,null],"Date":"2014-03-30 07:00:00.0"},
 {"AS2000":["-","-","-","-","-","-"],"AS1000":["5.0","0","false","false",null,null],"Date":"2014-03-30 14:59:00.0"}]

您甚至可以根据数组索引进行排序

 sort(jsonDepartments,sortByDesending("AS2000[0]"));

希望这可能有助于某人。

答案 1 :(得分:-4)

我也使用过Boon库,但我收到了错误&#34; java.lang.NoClassDefFoundError org.boon.sort.Sort&#34;

如果我做错了,请告诉我。

            this.list = global.getPartyPartneruserlistwithstatus().getJSONObject(0).getJSONArray("in_list");


            List<JSONObject> list1 = new ArrayList<JSONObject>();
            for (int i = 0; i < global.getPartyPartneruserlistwithstatus().getJSONObject(0).getJSONArray("in_list").length(); i++) {
                    list1.add(list.getJSONObject(i));
            }

                sort(list1,"age");

和list包含

中的JSONARRAY

[     {         &#34;用户名&#34;:&#34; Navdeep Singh Sandhu&#34;,         &#34;家乡&#34;:&#34; chandigarh&#34;,         &#34; request_id&#34;:&#34; 2&#34;,         &#34; social_id&#34;:&#34; 100002878462251&#34;,         &#34; social_provider&#34;:&#34; facebook&#34;,         &#34;年龄&#34;:25     },     {         &#34;用户名&#34;:&#34; HarPreet Kaur&#34;,         &#34;家乡&#34;:&#34; chandigarh&#34;,         &#34; request_id&#34;:&#34; 4&#34;,         &#34; social_id&#34;:&#34; 100002029425217&#34;,         &#34; social_provider&#34;:&#34; facebook&#34;,         &#34;年龄&#34;:23     }]