2015-02-11 61 views
0

我有以下JSON結果:
這是天氣結果。
我的目標是先獲得城市名稱。
然後根據列表中的某個城市,要求物業
從嵌套的JSON結果獲取JSON對象

{ 
    "response": { 
    "version":"0.1", 
    "termsofService":"http://www.wunderground.com/weather/api/d/terms.html", 
    "features": { 
    "hourly": 1 
    , 
    "lang": 1 
    } 
     , "results": [ 
     { 
     "name": "Al-Arz", 
     "city": "Al-Arz", 
     "state": "", 
     "country": "LB", 
     "country_iso3166":"LB", 
     "country_name":"Lebanon", 
     "zmw": "00000.1.40105", 
     "l": "https://stackoverflow.com/q/zmw:00000.1.40105" 
     } 
     , 
     { 
     "name": "Beirut", 
     "city": "Beirut", 
     "state": "", 
     "country": "LB", 
     "country_iso3166":"LB", 
     "country_name":"Lebanon", 
     "zmw": "00000.1.40100", 
     "l": "https://stackoverflow.com/q/zmw:00000.1.40100" 
     } 
     , 
     { 
     "name": "Dahr Baidar", 
     "city": "Dahr Baidar", 
     "state": "", 
     "country": "LB", 
     "country_iso3166":"LB", 
     "country_name":"Lebanon", 
     "zmw": "00000.1.40110", 
     "l": "https://stackoverflow.com/q/zmw:00000.1.40110" 
     } 
     , 
     { 
     "name": "Houche-Al-Oumara", 
     "city": "Houche-Al-Oumara", 
     "state": "", 
     "country": "LB", 
     "country_iso3166":"LB", 
     "country_name":"Lebanon", 
     "zmw": "00000.1.40101", 
     "l": "https://stackoverflow.com/q/zmw:00000.1.40101" 
     } 
     , 
     { 
     "name": "Merdjayoun", 
     "city": "Merdjayoun", 
     "state": "", 
     "country": "LB", 
     "country_iso3166":"LB", 
     "country_name":"Lebanon", 
     "zmw": "00000.1.40104", 
     "l": "https://stackoverflow.com/q/zmw:00000.1.40104" 
     } 
     , 
     { 
     "name": "Rayack", 
     "city": "Rayack", 
     "state": "", 
     "country": "LB", 
     "country_iso3166":"LB", 
     "country_name":"Lebanon", 
     "zmw": "00000.1.40102", 
     "l": "https://stackoverflow.com/q/zmw:00000.1.40102" 
     } 
     , 
     { 
     "name": "Tripoli", 
     "city": "Tripoli", 
     "state": "", 
     "country": "LB", 
     "country_iso3166":"LB", 
     "country_name":"Lebanon", 
     "zmw": "00000.1.40103", 
     "l": "https://stackoverflow.com/q/zmw:00000.1.40103" 
     } 
     ] 
    } 
} 

我怎樣才能得到所有城市的名字嗎?
在此先感謝。

回答

0
JSONObject rootObject = (JSONObject)new JSONTokener(yourJsonString).nextValue(); 
JSONObject responseObject = rootObject.getJSONObject("response"); 
JSONArray cityArray = responseObject.getJSONArray("results"); 
List<String> listWithCityNames = new ArrayList<String>(); 

for(int i = 0; i< cityArray.lenght();i++){ 
    listWithCityNames.add(cityArray.getJSONObject(i).getString("name")); 

} 

for(String city:listWithCityNames){ 
    System.out.println(city); 
} 

這段代碼解析在var yourJsonString JSON字符串並收集屬性name您的JSON的response對象內部results陣列英寸如果需要,添加Try/Catch塊。

+0

非常感謝! – EFK10003 2015-02-12 15:35:26

+0

很高興幫助;) – vinitius 2015-02-12 15:35:47

0

你可以把它當作json對象。請按照以下步驟操作

  1. 將此項變爲json對象。

  2. 然後使用getString()方法將「結果」的值放入一個數組中。

  3. 然後把這個結果轉換成json數組。
  4. 然後通過使用getString方法逐個遍歷json數組,您可以獲得所有城市的值。