2017-04-26 71 views
0

我有一個帶有更多empty_slots元素的JSON源(在下面的示例中只存在一個,但實際上存在更多帶有empty_slots的站)。我怎樣才能將empty_slots的值加起來並作爲double來返回?謝謝!json中的幾個項目的總和

JAVA

public static double getValueFromJSONString(String jString) throws JSONException 
{ 
    JSONObject json = new JSONObject(jString); 


    return json.getJSONObject("empty_bikes").getDouble(jString); 
}  

JSON

{"network": { 

"company": [ 
"Gewista Werbegesellschaft m.b.H" 
], 
"id": "citybike-wien", 
"location": { 
"city": "Wien", 
}, 
"stations": [ 
{ 
"empty_slots": 3, 
"extra": { 
"slots": "26", 
}, 
"free_bikes": 23 
    }]} 
+0

解析json,得到數組'station'循環這個數組獲得'empty_slots'的整數,並將它們加在一起循環。完成。 https://github.com/square/moshi – Blundell

回答

0

結帳JsonPath。下面的代碼沒有進行測試,但應與RestAssured依賴工作 -

JsonPath jsonPath = new JsonPath(jsonString); 

List<Integer> companyEntityIds = jsonPath.getList("network.stations.empty_slots"); 

一旦你的數組,你可以只使用一個流或任何添加這些了。