我一直在寻找一段时间,想要一种方法来排序这样的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",
}
]}
Run Code Online (Sandbox Code Playgroud)
并按"COMMERCIALNAME_E"的值按字母顺序排序:
{"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"
}
]}
Run Code Online (Sandbox Code Playgroud)
我找不到任何可以执行此操作的代码.谁能给我一些帮助?
Sar*_*aur 13
我使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
将这些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);
Run Code Online (Sandbox Code Playgroud)
如果要从Object创建JSON.
Person p = new Person();
p.setName("Jigar");
p.setAge(22);
String jsonStr = new com.google.gson.Gson().toJson(obj);
Run Code Online (Sandbox Code Playgroud)
小智 5
我用杰克逊来做这件事。下面是排序方法的实现,您可以向其添加更多检查并添加返回类型
public void sort(String data) throws IOException {
JsonNode node = new ObjectMapper().readTree(data);
ArrayNode array = (ArrayNode) node.get("results");
Iterator<JsonNode> i =array.elements();
List<JsonNode> list = new ArrayList<>();
while(i.hasNext()){
list.add(i.next());
}
list.sort(Comparator.comparing(o -> o.get("attributes").get("COMMERCIALNAME_E").asText()));
}
Run Code Online (Sandbox Code Playgroud)