【问题标题】:Java Android - How to sort a JSONArray based on keysJava Android - 如何根据键对 JSONArray 进行排序
【发布时间】:2015-10-27 23:55:35
【问题描述】:

我正在将这个字符串(来自网络服务)放入这样的 JSONArray,

[
 {
  "lat": "-16.408545",
  "lon": "-71.539105",
  "type": "0",
  "distance": "0.54"
 },
 {
  "lat": "-16.4244317845",
  "lon": "-71.52562186",
  "type": "1",
  "distance": "1.87"
 },
 {
  "lat": "-16.4244317845",
  "lon": "-71.52562186",
  "type": "1",
  "distance": "0.22"
 }
]

我需要按 distance 键对其进行排序,以显示最近的第一个和最远的最后一个。我没有尝试任何代码,因为我真的没有任何想法。我没有使用 GSON 库,我使用的是org.json.JSONArray

【问题讨论】:

  • 使用 undercore.js 库,你有各种排序解决方案。
  • 请看看我的question ..same issue

标签: java android arrays json sorting


【解决方案1】:

将您的 json 对象解析为一个模型,比如 array-List 并使用比较器对其进行排序。

ArrayList<ClassObject> dataList = new ArrayList<String>();
JSONArray array = new JSONArray(json);     
for(Object obj : jsonArray){
   dataList.add(//your data model);
}

请参考此链接对数组列表进行排序 http://java2novice.com/java-collections-and-util/arraylist/sort-comparator/

【讨论】:

    【解决方案2】:

    首先在列表中解析你的数组

    JSONArray sortedJsonArray = new JSONArray();
    List<JSONObject> jsonList = new ArrayList<JSONObject>();
    for (int i = 0; i < jsonArray.length(); i++) {
        jsonList.add(jsonArray.getJSONObject(i));
    }
    

    然后使用collection.sort对新创建的列表进行排序

    Collections.sort( jsonList, new Comparator<JSONObject>() {
    
        public int compare(JSONObject a, JSONObject b) {
            String valA = new String();
            String valB = new String();
    
            try {
                valA = (String) a.get("distance");
                valB = (String) b.get("distance");
            } 
            catch (JSONException e) {
                //do something
            }
    
            return valA.compareTo(valB);
        }
    });
    

    在数组中插入排序值

    for (int i = 0; i < jsonArray.length(); i++) {
        sortedJsonArray.put(jsonList.get(i));
    }
    

    【讨论】:

    • 请参阅this 如何按名称对列表视图进行排序
    • 非常感谢,它帮了我很多时间
    • 顺序切换 --> valA.compareTo(valB) vs valB.compareTo(valA)
    【解决方案3】:

    试试这个。它应该工作

    ArrayList<JSONObject> array = new ArrayList<JSONObject>();
    JSONArray jsonArray = new JSONArray();
    for (int i = 0; i < jsonArray.length(); i++) {
       try {
           array.add(jsonArray.getJSONObject(i));
       } catch (JSONException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
       }
    }   
    
    Collections.sort(array, new Comparator<JSONObject>() {
    
    @Override
    public int compare(JSONObject lhs, JSONObject rhs) {
        // TODO Auto-generated method stub
    
        try {
            return (lhs.getDouble("distance").compareTo(rhs.getDouble("distance")));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
    }
    });
    

    在此之后,您可以将已排序的ArrayList array 转换为JSONArray

    JSONArray jsonArray = new JSONArray(array);
    String jsonArrayStr = jsonArray.toString();
    

    【讨论】:

      猜你喜欢
      • 2018-05-19
      • 2012-10-05
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多