2015-09-03 87 views
5

我的JSON從JSONArray對象進行排序是:如何刪除重複和使用Java

[ 
    { 
     "distance":32, 
     "stationCode":"MIG", 
     "name":"Midghat", 
     "platforms":"2" 
    }, 
    { 
     "distance":32, 
     "stationCode":"MIG", 
     "name":"Midghat", 
     "platforms":"2" 
    }, 
    { 
     "distance":69, 
     "stationCode":"MDDP", 
     "name":"Mandideep", 
     "platforms":"2" 
    }, 
    { 
     "distance":69, 
     "stationCode":"MDDP", 
     "name":"Mandideep", 
     "platforms":"2" 
    }, 
    { 
     "distance":18, 
     "stationCode":"HBD", 
     "name":"Hoshangabad", 
     "platforms":"2" 
    }, 
    { 
     "distance":18, 
     "stationCode":"HBD", 
     "name":"Hoshangabad", 
     "platforms":"2" 
    }, 
    { 
     "distance":37, 
     "stationCode":"CHQ", 
     "name":"Choka", 
     "platforms":"2" 
    }, 
    { 
     "distance":37, 
     "stationCode":"CHQ", 
     "name":"Choka", 
     "platforms":"2" 
    }, 
    { 
     "distance":85, 
     "stationCode":"HBJ", 
     "name":"Habibganj", 
     "platforms":"5" 
    }, 
    { 
     "distance":85, 
     "stationCode":"HBJ", 
     "name":"Habibganj", 
     "platforms":"5" 
    }, 
    { 
     "distance":0, 
     "stationCode":"ET", 
     "name":"ItarsiJn", 
     "platforms":"28" 
    }, 
    { 
     "distance":8, 
     "stationCode":"PRKD", 
     "name":"Powerkheda", 
     "platforms":"2" 
    }, 
    { 
     "distance":8, 
     "stationCode":"PRKD", 
     "name":"Powerkheda", 
     "platforms":"2" 
    }, 
    { 
     "distance":55, 
     "stationCode":"ODG", 
     "name":"ObaidullaGanj", 
     "platforms":"2" 
    }, 
    { 
     "distance":55, 
     "stationCode":"ODG", 
     "name":"ObaidullaGanj", 
     "platforms":"2" 
    }, 
    { 
     "distance":44, 
     "stationCode":"BKA", 
     "name":"Barkhera", 
     "platforms":"2" 
    }, 
    { 
     "distance":44, 
     "stationCode":"BKA", 
     "name":"Barkhera", 
     "platforms":"2" 
    }, 
    { 
     "distance":79, 
     "stationCode":"MSO", 
     "name":"Misrod", 
     "platforms":"2" 
    }, 
    { 
     "distance":79, 
     "stationCode":"MSO", 
     "name":"Misrod", 
     "platforms":"2" 
    }, 
    { 
     "distance":25, 
     "stationCode":"BNI", 
     "name":"Budni", 
     "platforms":"2" 
    }, 
    { 
     "distance":25, 
     "stationCode":"BNI", 
     "name":"Budni", 
     "platforms":"2" 
    }, 
    { 
     "distance":91, 
     "stationCode":"BPL", 
     "name":"BhopalJn", 
     "platforms":"6" 
    }, 
    { 
     "distance":63, 
     "stationCode":"ITKL", 
     "name":"ItayaKalan", 
     "platforms":"2" 
    }, 
    { 
     "distance":63, 
     "stationCode":"ITKL", 
     "name":"ItayaKalan", 
     "platforms":"2" 
    } 
] 

我希望它按照距離排序和刪除重複stationCode。我嘗試過使用簡單的,如果其他人,但該過程將太多..專門爲排序相同的任何快捷方式。

+0

發佈你已經試過的代碼,以及你的錯誤是什麼:)這樣,你會更容易學習和幫助我們! –

回答

3

我前一段時間寫了這個工具,它各種JSONObjects 只有條件的JSONArray是您的JSONobjects必須包含keys要基於(它也接受一組鍵sort,如果你想排序基於幾個鍵)

import java.util.Collections; 
import java.util.Comparator; 
import java.util.Random; 
import net.sf.json.JSONArray; 
import net.sf.json.JSONObject; 
public class JSONArraySort { 
    @SuppressWarnings("unchecked") 
    public static void sortASCE(JSONArray array, Object key) { 
     Object[] keys = { key }; 
     Collections.sort(array, new JSONArrayComparator(false, keys)); 
    } 
    @SuppressWarnings("unchecked") 
    public static void sortDESC(JSONArray array, Object key) { 
     Object[] keys = { key }; 
     Collections.sort(array, new JSONArrayComparator(true, keys)); 
    } 
    @SuppressWarnings("unchecked") 
    public static void sortASCE(JSONArray array, Object[] key) { 
     Collections.sort(array, new JSONArrayComparator(false, key)); 
    } 
    @SuppressWarnings("unchecked") 
    public static void sortDESC(JSONArray array, Object[] key) { 
     Collections.sort(array, new JSONArrayComparator(true, key)); 
    } 
    private static class JSONArrayComparator implements Comparator<JSONObject> { 
     private final Object[] KEYS; 
     private final boolean DESC; 
     public JSONArrayComparator(boolean DESC, Object[] KEYS) { 
      this.KEYS = KEYS; 
      this.DESC = DESC; 
     } 
     @Override 
     public int compare(JSONObject object1, JSONObject object2) { 
      int length = KEYS.length; 
      for(int i = 0 ; i < length ; i++){ 
       String KEY = KEYS[i].toString(); 
       Object one = object1.get(KEY); 
       Object two = object2.get(KEY); 
       if(Number.class.isAssignableFrom(one.getClass()) && Number.class.isAssignableFrom(two.getClass())){ 
        Double numOne = Number.class.cast(one).doubleValue(); 
        Double numTwo = Number.class.cast(two).doubleValue(); 
        int compared = 0; 
        if(DESC){ 
         compared = numTwo.compareTo(numOne); 
        }else{ 
         compared = numOne.compareTo(numTwo); 
        } 
        if(i == KEYS.length - 1 || compared != 0){ 
         return compared; 
        } 
       }else{ 
        int compared = 0; 
        if(DESC){ 
         compared = two.toString().compareTo(one.toString()); 
        }else{ 
         compared = one.toString().compareTo(two.toString()); 
        } 
        if(i == KEYS.length - 1 || compared != 0){ 
         return compared; 
        } 
       } 
      } 
      // this shouldn't happen. 
      return 0; 
     } 
    } 
     //testing... 
    public static void main(String... args) { 
     JSONArray array1 = new JSONArray(); 
     for(int i = 0 ; i < 100 ; i++){ 
      Random random = new Random(); 
      int num1 = random.nextInt(10); 
      int num2 = random.nextInt(10); 
      JSONObject object = new JSONObject(); 
      object.put("num1", num1); 
      object.put("num2", num2); 
      array1.add(object); 
     } 
     String[] keys = { "num1", "num2" }; 
     sortASCE(array1, keys); 
     System.out.println(array1.toString()); 
    } 
} 
現在,如果你想通過

他們

Set<String> stationCodes=new HashSet<String>(); 
JSONArray tempArray=new JSONArray(); 
for(int i=0;i<yourJSONArray.size();i++){ 
    String stationCode=yourJSONArray.getJSONObject(i).getString("stationCode"); 
    if(stationsCodes.contains(stationCode){ 
    continue; 
    } 
    else{ 
     stationsCodes.add(stationCode); 
     tempArray.add(yourJSONArray.getJSONObject(i)); 
    } 

} 


yourJSONArray= tempArray; //assign temp to original 

//here how you can sort it using utility above: 
JSONArraySort.sortASCE(yourJSONArray,"distance"); 
刪除重複即可
+0

感謝它的工作排序..過濾重複它與我的其他邏輯:)使用這種排序的東西保存了很多時間:) –

+0

@GovindMantri,酷伴侶,只是你知道你可以使用多個鍵對DECS進行排序。 – nafas

1

沒有直接的辦法到,但你可以遵循的方式波紋管提到:

  1. 使用org.codehaus.jackson.map.ObjectMapper

  2. 變換的JSONObject到Java對象列表中使用Java Map做獨特的(把key = stationCode,javaObject作爲對象)

  3. 根據距離對地圖數據進行排序。

+0

肯定有一個排序的直接方式:) – nafas