简体   繁体   English

如何在Java中对JSON数组进行排序

[英]How do i sort JSON Array in java

I have a JSON Array: 我有一个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);

    }

Result in "datas" : 结果为“数据”:

    [{"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"}]

How can I have an ascending order by "dateDebut" ? 如何通过“ dateDebut”获得升序?

Thx 谢谢

JSONArray s are not sortable. JSONArray不可排序。 You can extract the data into an ArrayList and then sort with a comparator and then put back into the JSONArray . 您可以将数据提取到ArrayList ,然后使用comparator进行排序,然后放回JSONArray

Boon (a Java open source lib) has what is currently the fastest JSON parser on the JVM (circa March 2014) and it provides support for sorting, filtering, searching (with indexes), JSON. Boon(Java开源库)具有JVM(大约2014年3月)上目前最快的JSON解析器,它支持对JSON进行排序,过滤,搜索(带有索引)。

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

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

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

http://www.dzone.com/links/java_boon_filtering_for_java_beans_json_and_java.html (Using the Index Search to do JSON ETL and searching and sorting) 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 (Using Boon to filter JSON) http://www.dzone.com/links/java_boon_filtering_for_java_beans_json_and_java.html (使用Boon过滤JSON)

Here is a sorting example: 这是一个排序示例:

    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

Oh yeah... one more thing.. It is screaming fast. 哦,是的...还有一件事..它尖叫得很快。 :) :)

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

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