我一直在寻找一段时间,想要一种方法来排序这样的
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,"attributes": { "OBJECTID": "1","COMMERCIALNAME_E": "GAYATHY HOSPITAL PHARMACY" },"attributes": { "OBJECTID": "255","COMMERCIALNAME_E": "AL DEWAN PHARMACY" },} ]}
并按“COMMERCIALNAME_E”的值按字母顺序排序:
{"results": [ { "layerId": 5,"geometryType": "esriGeometryPoint" },"COMMERCIALNAME_E": "GAYATHY HOSPITAL PHARMACY" },"geometryType": "esriGeometryPoint" } ]}
我找不到任何可以执行此操作的代码.谁能给我一些帮助?
解决方法
将这些JSON解析为对象集合,并使用比较器使用首选字段对其进行排序.
> Use GSON to parse it to collection of objects
例:
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);
如果要从Object创建JSON.
Person p = new Person(); p.setName("Jigar"); p.setAge(22); String jsonStr = new com.google.gson.Gson().toJson(obj);