0

我有一个 JSON 数组:

public JSONObject toJSONObject(DataJSONConverter dataJsonConverter) {
    JSONObject jsonResponse = new JSONObject();

    if (datas != null && dataJsonConverter != null) {
        JSONArray jsonDatas = new JSONArray();
        // for (Iterator<? extends Object> iter = datas.iterator(); iter.hasNext();) {
        // jsonDatas.put(dataJsonConverter.toJSONObject(iter.next()));
        // }

        for (Object iter : datas) {
            jsonDatas.put(dataJsonConverter.toJSONObject(iter));
        }

        jsonResponse.put("datas", jsonDatas);
        jsonResponse.put("count", count);

    }

结果为“数据”:

    [{"id":60685,"libelle":"BILAN3","typeAnomalie":"Trop de points 
invalides","dateDebut":"18\/07\/2013 00:00","numero":"1268"}

    {"id":60628,"libelle":"_fictif","typeAnomalie":"Trop de points 

invalides","dateDebut":"02\/06\/2013 00:00","numero":"1242"}

    {"id":14672,"libelle":"NAVIL 949","typeAnomalie":"D\u00e9passement","dateDebut":"13\/05\/2012 12:00","numero":"263"}]

如何按“dateDebut”进行升序?

谢谢

4

2 回答 2

1

JSONArrays 不可排序。您可以将数据提取到 a 中ArrayList,然后用 a 排序,comparator然后放回JSONArray.

于 2013-07-18T13:45:26.087 回答
0

Boon(一个 Java 开源库)拥有目前 JVM 上最快的 JSON 解析器(大约 2014 年 3 月),它提供对排序、过滤、搜索(带索引)、JSON 的支持。

http://www.dzone.com/links/1123623.html(路径表达式和 JSON)

http://www.dzone.com/links/1123621.html(排序JSON)

http://www.dzone.com/links/1091051.html(使用解析器)

http://www.dzone.com/links/java_boon_filtering_for_java_beans_json_and_java.html(使用索引搜索做JSON ETL和搜索排序)

http://www.dzone.com/links/java_boon_filtering_for_java_beans_json_and_java.html(使用 Boon 过滤 JSON)

这是一个排序示例:

    Object jsonObject = fromJson(json);
    List<?> jsonDepartments = (List<?>) jsonObject;
    List<?> jsonEmployees = (List<Employee>) atIndex(jsonDepartments, "employees");

    sort(employees); //natural sort


    sort( employees, "lastName"); //sort by last name



    sort( departmentList );



    sort( employees, sortBy( "department.name" ),
                     sortByDescending( "lastName" ),
                     sortBy( "firstName" ) ); //well you get the idea



    sort(employees,
            sortBy("contactInfo.phoneNumbers[0]")); //you can even sort by an path expression




    sort( employees,
            sortByDescending("contactInfo.phoneNumbers[0]") ); //forward and backwards

哦,是的……还有一件事……它尖叫得很快。:)

于 2014-03-21T08:20:11.640 回答