2010-11-25 145 views
4

我一直在找了一會兒,想辦法進行排序JSON對象是這樣的:由「COMMERCIALNAME_E」的價值如何在Java中對JSON對象進行排序?

{"results": [ 
    { 
    "layerId": 5, 
    "layerName": "Pharmaceutical Entities", 
    "attributes": { 
     "OBJECTID": "35", 
     "FACILITYTYPE": "Pharmacy", 
     "FACILITYSUBTYPE": "24 Hr Pharmacy", 
     "COMMERCIALNAME_E": "SADD MAARAB PHARMACY" 
     }, 
    "geometryType": "esriGeometryPoint", 
    }, 
    { 
    "layerId": 5, 
    "layerName": "Pharmaceutical Entities", 
    "attributes": { 
     "OBJECTID": "1", 
     "FACILITYTYPE": "Pharmacy", 
     "FACILITYSUBTYPE": "24 Hr Pharmacy", 
     "COMMERCIALNAME_E": "GAYATHY HOSPITAL PHARMACY" 
    }, 
    "geometryType": "esriGeometryPoint", 
    }, 
    { 
    "layerId": 5, 
    "layerName": "Pharmaceutical Entities", 
    "attributes": { 
     "OBJECTID": "255", 
     "FACILITYTYPE": "Pharmacy", 
     "FACILITYSUBTYPE": "24 Hr Pharmacy", 
     "COMMERCIALNAME_E": "AL DEWAN PHARMACY" 
     }, 
    "geometryType": "esriGeometryPoint", 
    } 
]} 

和排序按字母順序獲得:

{"results": [ 
    { 
    "layerId": 5, 
    "layerName": "Pharmaceutical Entities", 
    "attributes": { 
     "OBJECTID": "255", 
     "FACILITYTYPE": "Pharmacy", 
     "FACILITYSUBTYPE": "24 Hr Pharmacy", 
     "COMMERCIALNAME_E": "AL DEWAN PHARMACY" 
     }, 
    "geometryType": "esriGeometryPoint" 
    }, 
    { 
    "layerId": 5, 
    "layerName": "Pharmaceutical Entities", 
    "attributes": { 
     "OBJECTID": "1", 
     "FACILITYTYPE": "Pharmacy", 
     "FACILITYSUBTYPE": "24 Hr Pharmacy", 
     "COMMERCIALNAME_E": "GAYATHY HOSPITAL PHARMACY" 
     }, 
    "geometryType": "esriGeometryPoint" 
    }, 
    { 
    "layerId": 5, 
    "layerName": "Pharmaceutical Entities", 
    "attributes": { 
     "OBJECTID": "35", 
     "FACILITYTYPE": "Pharmacy", 
     "FACILITYSUBTYPE": "24 Hr Pharmacy", 
     "COMMERCIALNAME_E": "SADD MAARAB PHARMACY" 
     }, 
    "geometryType": "esriGeometryPoint" 
    } 
]} 

我找不到任何將執行此操作的代碼。任何人都可以給我一些幫助嗎?

回答

5

將這些JSON解析爲對象集合並使用比較器使用您的首選字段對其進行排序。

例子:

import com.google.gson.Gson; 

class Person { 
    private int age; 
    private String name; 
} 

String json = "{'age':22,'name':'Jigar'}"; 
Gson gson = new Gson(); 
TestJsonFromObject obj = gson.fromJson(json, Person.class); 

如果你想創建對象JSON。

Person p = new Person(); 
p.setName("Jigar"); 
p.setAge(22); 
String jsonStr = new com.google.gson.Gson().toJson(obj); 
+0

謝謝,但我在動態獲取該對象(我稱爲Web服務),它是不可能性,使用toString()方法或任何onther方法需要請幫我 – 2010-11-25 14:03:47

+0

JSON對象轉換爲字符串@venkatesh如果您要求從Object創建JSON,而不是使用`new com.google.gson.Gson()。toJson(obj)';` – 2010-11-25 14:08:38

+0

謝謝,JSONObject obj =/** json object data ** /那麼String str = obj.toString();這是對的嗎 ?請解釋清楚 – 2010-11-25 15:19:57

2

你可以寫的JSON陣列周圍List<JSONObject>包裝,然後用Collections.sort用自定義Comparator<JSONObject>

3

Boon提供了JSON排序,搜索,過濾等功能。

退房:

http://www.dzone.com/links/r/sorting_for_java_instances_maps_java_collections.html(文排序)

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); //natural sort 



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



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




    sort(employees, 
      sortByDescending("contactInfo.phoneNumbers[0]")); //backwards by a path expression 


    max(employees); //gets the max (natural order employee) 


    greatest(employees, 5); //gets the top five 


    min(employees); //gets the lowest 



    least(employees, 5); //gets the lowest five 


    max(employees, "salary"); //gets the top salaried employee 

    greatest(employees, "salary", 5); //gets the top five salaried employees 


    min(employees, "salary"); //the least 

    least(employees, "salary", 5); //the lowest five salaried employees 

寶也是目前的JVM(大約2014年3月)最快的JSON解析器。

3

我用JSON簡單的API來排序。這裏是我的代碼:

import java.io.FileReader; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Comparator; 

import org.json.simple.JSONArray; 
import org.json.simple.JSONObject; 
import org.json.simple.parser.JSONParser; 

public class SortJSON { 

public static void main(String[] args) { 
    JSONParser parser = new JSONParser(); 
    try { 
     JSONObject o = (JSONObject) parser.parse(new FileReader("test3.json")); 
     JSONArray array = (JSONArray) o.get("results"); 
     ArrayList<JSONObject> list = new ArrayList<>(); 

     for (int i = 0; i < array.size(); i++) { 
      list.add((JSONObject) array.get(i)); 
     } 
     Collections.sort(list, new MyJSONComparator()); 

     for (JSONObject obj : list) { 
      System.out.println(((JSONObject) obj.get("attributes")).get("OBJECTID")); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

} 

class MyJSONComparator implements Comparator<JSONObject> { 

@Override 
public int compare(JSONObject o1, JSONObject o2) { 
    String v1 = (String) ((JSONObject) o1.get("attributes")).get("COMMERCIALNAME_E"); 
    String v3 = (String) ((JSONObject) o2.get("attributes")).get("COMMERCIALNAME_E"); 
    return v1.compareTo(v3); 
} 

} 
相關問題