2017-10-17 32 views
0

我想從JSONObject的「氣象」的數據不顯示,但這個錯誤是來了。JSON對象在Java中

org.json.JSONException: JSONObject["weather"] not a string. 
    at org.json.JSONObject.getString(JSONObject.java:639) 
    at GetWeather.main(GetWeather.java:49) 

這是我的代碼

import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.Map; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

public class GetWeather {  

    public static String getWeather(String args){ 
     String result =" "; 
     URL url ; 
     HttpURLConnection urlConnection = null; 
     try{ 
      url = new URL(args); 
      urlConnection = (HttpURLConnection) url.openConnection(); 
      InputStream in = urlConnection.getInputStream(); 
      InputStreamReader reader = new InputStreamReader(in); 
      int data= reader.read(); 
      while(data!=-1){ 
       char current=(char) data; 
       result += current; 
       data= reader.read(); 
      } 
      return result; 
     }catch(MalformedURLException e){ 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    //main 
    public static void main(String[] args){ 
     String s1 = getWeather(args[0]); 
     try { 
      JSONObject jsonObject = new JSONObject(s1); 
      String weather= jsonObject.getString("weather"); 
      System.out.println(weather); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

這是我傳遞

http://api.openweathermap.org/data/2.5/weather?q=Delhi&APPID=04b767167643ea6af521695e7948e0fb 

這個字符串中的數據,我回去

{"coord":{"lon":77.22,"lat":28.67},"weather":[{"id":721,"main":"Haze","description":"haze","icon":"50d"}],"base":"stations","main":{"temp":305.86,"pressure":1007,"humidity":38,"temp_min":304.15,"temp_max":307.15},"visibility":3500,"wind":{"speed":1.5,"deg":320},"clouds":{"all":0},"dt":1508241600,"sys":{"type":1,"id":7808,"message":0.0051,"country":"IN","sunrise":1508201604,"sunset":1508242734},"id":1273294,"name":"Delhi","cod":200} 

其中在格式化版本看起來像

{ 
    "coord": { 
     "lon": 77.22, 
     "lat": 28.67 
    }, 
    "weather": [{ 
      "id": 721, 
      "main": "Haze", 
      "description": "haze", 
      "icon": "50d" 
     } 
    ], 
    "base": "stations", 
    "main": { 
     "temp": 305.86, 
     "pressure": 1007, 
     "humidity": 38, 
     "temp_min": 304.15, 
     "temp_max": 307.15 
    }, 
    "visibility": 3500, 
    "wind": { 
     "speed": 1.5, 
     "deg": 320 
    }, 
    "clouds": { 
     "all": 0 
    }, 
    "dt": 1508241600, 
    "sys": { 
     "type": 1, 
     "id": 7808, 
     "message": 0.0051, 
     "country": "IN", 
     "sunrise": 1508201604, 
     "sunset": 1508242734 
    }, 
    "id": 1273294, 
    "name": "Delhi", 
    "cod": 200 
} 

請告訴我什麼是錯我的代碼,做些什麼。

+4

「天氣」鍵不成立字符串值,但陣列'[....]'所以'的getString(「天氣」)'是不挑陣列正確方法。你嘗試過類似'getJSONArray'的東西嗎? – Pshemo

+0

所以,你不應該使用'的getString()' – Neo

+0

謝謝,@Pshemo它的工作,你能告訴我的,我可以從中提取的溫度和壓力的方式嗎? –

回答

1

「天氣」,你想獲得的價值是不是String,但JSONArray

爲了看它裏面的所有的信息,請嘗試使用getJSONArray()

try { 
    JSONObject jsonObject = new JSONObject(s1); 
    // read the `weather` content 
    JSONArray weatherArray = jsonObject.getJSONArray("weather"); 
    // get only the first element of `weather`, the only one existing 
    JSONObject weatherObject = (JSONObject)weatherArray.get(0); 
    // read all its' properties 
    for (Object key : weatherObject.keySet()) { 
     System.out.println("key:" + key + ", value: " + weatherObject.get((String)key)); 
    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

對於喜歡「臨時」或「壓力」等信息,只需要用getJSONObject()因爲「主」有JSONObject類型:

JSONObject mainObject = jsonObject.getJSONObject("main"); 
System.out.println("pressure value: " + mainObject.get("pressure")); 
System.out.println("temp value: " + mainObject.get("temp"));