【问题标题】:Reorder the JSON object in java [duplicate]在java中重新排序JSON对象[重复]
【发布时间】:2018-02-08 06:42:28
【问题描述】:

在我的 Android 应用程序中,我必须根据“id”按升序对下面的 JSON 对象进行排序。将有大约 2000 个对象。请告诉我如何对这些 JSON 对象进行排序。

[
    {
        "id": "bitcoin", 
        "name": "Bitcoin", 
        "symbol": "BTC", 
        "rank": "1", 
        "price_usd": "8001.02", 
        "price_btc": "1.0", 
        "24h_volume_usd": "9892040000.0", 
        "market_cap_usd": "134838389703", 
        "available_supply": "16852650.0", 
        "total_supply": "16852650.0", 
        "max_supply": "21000000.0", 
        "percent_change_1h": "-1.27", 
        "percent_change_24h": "7.28", 
        "percent_change_7d": "-20.22", 
        "last_updated": "1518071364"
    }, 
    {
        "id": "ethereum", 
        "name": "Ethereum", 
        "symbol": "ETH", 
        "rank": "2", 
        "price_usd": "803.952", 
        "price_btc": "0.100246", 
        "24h_volume_usd": "4216090000.0", 
        "market_cap_usd": "78370675159.0", 
        "available_supply": "97481784.0", 
        "total_supply": "97481784.0", 
        "max_supply": null, 
        "percent_change_1h": "-1.22", 
        "percent_change_24h": "7.16", 
        "percent_change_7d": "-29.01", 
        "last_updated": "1518071354"
    }
]

【问题讨论】:

    标签: java android json


    【解决方案1】:

    我建议在服务器端做,在从数据库中查询和获取数据时,他们可以在查询本身中排序,这将减少一堆处理时间(比较2000或更多的值,处理时间可能会增加并导致性能问题)。

    如果以上都做不到,那么你有一个解决办法

    1. 在一个Pojo类(Serializable类)中设置每个json数组数据
    2. 用你制作的 pojo 类制作一个数组列表(假设它是stockList)。
    3. 假设您的 pojo 类名 StockInfo 并在您的 java 文件中使用以下代码。

      Collections.sort(stockList, new Comparator<StockInfo>() {
          @Override
          public int compare(StockInfo s1, StockInfo s2) {
              return s1.getId().compareToIgnoreCase(s2.getId());
          }
      });
      

    我希望,这个解决方案会帮助你。

    【讨论】:

      【解决方案2】:

      这是 JSON 对象排序列表的示例示例

      1.基于学生年龄属性的ArrayList。可以这样做——首先,实现 Comparable 接口,然后重写 compareTo 方法。

      public class Student implements Comparable {
          private String studentname;
          private int rollno;
          private int studentage;
      
          public Student(int rollno, String studentname, int studentage) {
              this.rollno = rollno;
              this.studentname = studentname;
              this.studentage = studentage;
          }
          ...
          //getter and setter methods same as the above example 
          ...
          @Override
          public int compareTo(Student comparestu) {
              int compareage=((Student)comparestu).getStudentage();
              /* For Ascending order*/
              return this.studentage-compareage;
      
              /* For Descending order do like this */
              //return compareage-this.studentage;
          }
      
          @Override
          public String toString() {
              return "[ rollno=" + rollno + ", name=" + studentname + ", age=" + studentage + "]";
          }
      
      }
      

      2.然后在ArrayList上调用Collections.sort

      import java.util.*;
      public class ArrayListSorting  {
      
          public static void main(String args[]){
             ArrayList<Student> arraylist = new ArrayList<Student>();
             arraylist.add(new Student(223, "Chaitanya", 26));
             arraylist.add(new Student(245, "Rahul", 24));
             arraylist.add(new Student(209, "Ajeet", 32));
      
             Collections.sort(arraylist);
      
             for(Student str: arraylist){
                  System.out.println(str);
             }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-21
        • 1970-01-01
        • 1970-01-01
        • 2016-09-23
        • 2017-02-25
        • 1970-01-01
        • 2014-12-26
        • 2016-10-14
        相关资源
        最近更新 更多