How to sort JSON object in java?

后端 未结 5 854
一个人的身影
一个人的身影 2020-12-06 01:35

I\'ve been looking for a while and want a way to sort a JSON object like this:

{\"results\": [
  {
    \"layerId\": 5,
    \"layerName\": \"Pharmaceutical En         


        
5条回答
  •  有刺的猬
    2020-12-06 02:04

    Parse these JSON to Collection of Objects and use comparator to sort it using your preferred field.

    • Use GSON to parse it to collection of objects

    Example:

    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);  
    

    If you want to create JSON from Object.

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

提交回复
热议问题