在java中重新排序JSON对象

时间:2018-02-08 06:42:28

标签: java android json

在我的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"
    }
]

2 个答案:

答案 0 :(得分: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());
        }
    });
    
  4. 我希望,这个解决方案可以帮助你。

答案 1 :(得分:0)

  

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

1.基于学生Age属性的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);
       }
    }
}