2016-10-17 118 views
0

我有一個字符串符號一堆JSON對象:過濾JSON對象

"{\"address\":{\"street\":\"Steenstraat\",\"housenumber\":\"17A\",\"postalcode\":\"6828 CA\",\"city\":\"ARNHEM\",\"geoLocation\":{\"lat\":\"51.983718\",\"lng\":\"5.915553\"}},\"distance\":0,\"type\":\"ING\"} 

所以每個JSON對象看起來是這樣的:

{ 
     "address" : { 
       "street" : "Steenstraat", 
       "housnumber" : "17A", 
       "postalcode" : "6828 CA", 
       "city" : "ARNHEM", 
       "geolocation" : { 
          "latitude" : "51.983718", 
          "longitude" : "54.983718" 
          } 
        }, 
       "type" : "citi", 
       "distance" : 0 
    } 

現在,我使用谷歌的gson庫從其他API中獲取,並給了我一個上面的JSON對象的字符串。我怎樣才能嘗試過濾掉(或者重新定義JSON的結構)來按特定參數對JSON對象進行排序(比如按城市名排序)?

這是我的Atm類。我試圖將JSON字符串轉換爲Atm對象列表。

public class Atm { 

    private String type; 
    private Long distance; 
    private Map<String, String> address = new HashMap<String, String>(); 

    public Atm() { 
    } 

    public String getType() { 
     return type; 
    } 

    public void setType(String type) { 
     this.type = type; 
    } 

    public Long getDistance() { 
     return distance; 
    } 

    public void setDistance(Long distance) { 
     this.distance = distance; 
    } 

    public Map<String, String> getAddress() { 
     return address; 
    } 

    public void setAddress(Map<String, String> address) { 
     this.address = address; 
    } 

    @Override 
    public String toString() { 
     return "Atm{" + 
       "type='" + type + 
       ", distance=" + distance + 
       ", address=" + address.toString() + 
       '}'; 
    } 
} 

還是有沒有辦法做到這一點,而無需將其轉換爲Java數據結構?

+0

您需要使用哪種語言? –

+1

Java,最好是 –

+0

這是一個類似的[post](http://stackoverflow.com/questions/19543862/how-can-i-sort-a-jsonarray-in-java) –

回答

0

注意:由於您在JSON中使用子元素geoLocation,因此不能使用Map<String, String>。 您應該使用:Map<String, Object>,或者創建一個自定義類來表示您的地址。

要按城市篩選您的Atm列表,您可以執行以下操作。

在Java 8:

Gson gson = new Gson(); 
String atm1 = "{\"address\":{\"street\":\"Steenstraat\",\"housenumber\":\"17A\",\"postalcode\":\"6828 CA\",\"city\":\"ARNHEM\",\"geoLocation\":{\"lat\":\"51.983718\",\"lng\":\"5.915553\"}},\"distance\":0,\"type\":\"ING\"}"; 
String atm2 = "{\"address\":{\"street\":\"Steenstraat\",\"housenumber\":\"17A\",\"postalcode\":\"6828 CA\",\"city\":\"ARNHEM\",\"geoLocation\":{\"lat\":\"51.983718\",\"lng\":\"5.915553\"}},\"distance\":0,\"type\":\"ING\"}"; 
String atm3 = "{\"address\":{\"street\":\"Steenstraat\",\"housenumber\":\"17A\",\"postalcode\":\"6828 CA\",\"city\":\"NEW-YORK\",\"geoLocation\":{\"lat\":\"51.983718\",\"lng\":\"5.915553\"}},\"distance\":0,\"type\":\"ING\"}"; 

List<Atm> atms = new ArrayList<Atm>(); 

atms.add(gson.fromJson(atm1, Atm.class)); 
atms.add(gson.fromJson(atm2, Atm.class)); 
atms.add(gson.fromJson(atm3, Atm.class)); 

List<Atm> filteredOnCity = atms.stream().filter(atm -> atm.getAddress().get("city") 
    .equals("ARNHEM")).collect(Collectors.toList()); 

與Apache公地collections4:

//Build your list with code from above 
Predicate<Atm> filterOnCity = new Predicate<Atm>() { 
    @Override 
    public boolean evaluate(Atm atm) { 
     return atm.getAddress().get("city").equals("ARNHEM"); 
    } 
}; 

CollectionUtils.filter(atms, filterOnCity); 
+0

我可以以某種方式做到這一點沒有用的java?當我嘗試列出列表時,我總是收到這個錯誤:期望的BEGIN_ARRAY,但在第1行第2列的路徑上是STRING 我想我知道爲什麼會出現這個錯誤。它是因爲我想要一個Map 的列表,但地圖中的最後一個元素是另一個地圖(地理定位的那個) –

+0

最簡單的方法是更改​​您的Map '到'Map '。 然後它會直接從盒子中運行。 –

+0

沒有,仍然得到相同的錯誤 –

0

爲什麼不過濾或將它們發送到Java之前在客戶端對它們進行排序?

var arr = JSON.parse(MY_JSON_ARRAY_STRING); 

arr.sort(function(a, b){ 
    if (a.city < b.city) 
     return -1; 
    if (a.city > b.city) 
     return 1; 
    return 0; 
}); 

var arrString = JSON.stringify(arr);