2016-10-26 68 views
0

我有一個JSONObject,我通過API獲取。 它是這樣的:JAVA按JSONObject(String,Int)降序排序

{ 
    "wash_method": 305, 
    "fragrance": 1156, 
    "sweater_type": 260, 
    "dial_color": 66475, 
    "charger_type": 2581, 
    "pen_drives_interface": 563, 
     "watches_type": 66475, 
     "processor": 3270, 
     "frame_material": 1211, 
     "dial_shape": 66475, 
     "os": 3308 
    } 

我想排序這個值的基礎上,得到這樣一個結果:

{ 
    "dial_shape": 66475, 
    "dial_color": 66475, 
    "watches_type": 66475, 
     "os": 3308, 
     "processor": 3270, 
     "charger_type": 2581, 
     "frame_material": 1211, 
     "fragrance": 1156, 
     "pen_drives_interface": 563, 
    "wash_method": 305, 
    "sweater_type": 260,  
    } 

正如你所看到的前3項有類似的值,這樣就可以以任何方式安排。
我該怎麼做? (有沒有辦法以最小的複雜性做呢?)

+1

JSON對象有,也不是「排序」 – Blackbelt

+0

它必須通過API開發人員給予ü響應 – vishnus

+0

@vishnus它是一個第三方API之前完成的概念。我需要在我身邊做。 –

回答

1

這裏是你的問題的解決方案。我希望這將有所幫助。

import org.json.JSONException; 

import org.json.JSONObject; 

import java.util.*; 

public class sample { 
private static class MyModel { 
    String key; 
    int value; 

    MyModel(String key, int value) { 
     this.key = key; 
     this.value = value; 
    } 
} 

public static void main(String[] args) { 
    List<MyModel> list = new ArrayList<>(); 

    try { 
     //here is you json object 
     JSONObject obj = new JSONObject(); 
     obj.put("name1", 29); 
     obj.put("name2", 26); 
     obj.put("name3", 29); 
     obj.put("name4", 27); 

     //parsing your json 
     Iterator<?> keys = obj.keys(); 
     MyModel objnew; 
     while (keys.hasNext()) { 
      String key = (String) keys.next(); 
      objnew = new MyModel(key, obj.optInt(key)); 
      list.add(objnew); 
     } 

     //sorting the values 
     Collections.sort(list, new Comparator<MyModel>() { 
      @Override 
      public int compare(MyModel o1, MyModel o2) { 
       return Integer.compare(o2.value, o1.value); 
      } 
     }); 
     //print out put 
     for (MyModel m : list) { 
      System.out.println(m.key + " : " + m.value); 
     } 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

}

+1

什麼是MyModel? –

+0

你可以在代碼中看到。這是一個類,它將解析你的json後存儲鍵/值。 – Baba

+1

謝謝!這工作完美。 但有一個問題,但。 Integer.compare(o2.value,o1.value)此方法僅適用於API級別爲19或以上的情況。以下API 19設備的替代選擇是什麼? –

0

嘗試實現這一

public class SomeClass { 

    /** 
    * Sorts the given JSONObject 
    * 
    * @param jsonObject 
    * @return jsonObject sorted in descending order 
    * @throws JSONException 
    */ 
    public JSONObject sortJSONObject(JSONObject jsonObject) throws JSONException { 

     ArrayList<SomeChildClass> alstSomeChildClass = new ArrayList<>(); 

     Iterator<String> keys = jsonObject.keys(); 

     while (keys.hasNext()) { 
      SomeChildClass someChildClass = new SomeChildClass(); 
      someChildClass.key = keys.next(); 
      someChildClass.value = jsonObject.getString(someChildClass.key); 
      alstSomeChildClass.add(someChildClass); 
     } 

     // Sort the Collection 
     Collections.sort(alstSomeChildClass); 

     JSONObject sortedJsonObject = new JSONObject(); 
     for (SomeChildClass someChildClass : alstSomeChildClass) 
      sortedJsonObject.put(someChildClass.key, someChildClass.value); 

     return sortedJsonObject; 
    } 

    private class SomeChildClass implements Comparable<SomeChildClass> { 

     private String key; 
     private String value; 

     @Override 
     public int compareTo(@NonNull SomeChildClass target) { 

      int currentVal = Integer.parseInt(value); 
      int targetVal = Integer.parseInt(target.value); 

      return (currentVal < targetVal) ? 1 : ((currentVal == targetVal) ? 0 : -1); 
     } 
    } 
} 

UPDATE

String json = "{\n" + 
      "  \"wash_method\": 305,\n" + 
      "  \"fragrance\": 1156,\n" + 
      "  \"sweater_type\": 260,\n" + 
      "  \"dial_color\": 66475,\n" + 
      "  \"charger_type\": 2581,\n" + 
      "  \"pen_drives_interface\": 563,\n" + 
      "  \"watches_type\": 66475,\n" + 
      "  \"processor\": 3270,\n" + 
      "  \"frame_material\": 1211,\n" + 
      "  \"dial_shape\": 66475,\n" + 
      "  \"os\": 3308\n" + 
      "  }"; 

    try { 
     JSONObject object = new SomeClass().sortJSONObject(new JSONObject(json)); 
     Log.e("JSON", object.toString()); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
+1

這不是對它進行排序。返回相同的結果。 –

+0

檢查更新。我已經交叉驗證它,它的工作原理。 –

+2

實際上超過KitKat API級別我甚至不需要對JSONObject進行排序。但是當我使用kitkat級別的手機時,我需要進行排序,並且您建議的方法不起作用。 –