2016-06-27 65 views
-4

openweather APIを使用してJSON天気データを取得するためにこの関数を記述しました。私はエラーorg.json.JSONExceptionを持っています:OWM_TEMPARATUREを使用している間、tempの値はありません。しかし、私はtemparatureフィールドを全く使わないとき、日付と説明を正しく取得しています。一時的な値がありませんorg.json.JSONException:tempの値がありません

private String[] getWeatherDataFromJson(String forecastJsonStr, int numDays) 
      throws JSONException { 


     final String OWM_LIST = "list"; 
     final String OWM_WEATHER = "weather"; 
     final String OWM_TEMPERATURE = "temp"; 
     final String OWM_MAX = "max"; 
     final String OWM_MIN = "min"; 
     final String OWM_DESCRIPTION = "main"; 

     JSONObject forecastJson = new JSONObject(forecastJsonStr); 
     JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST); 


     Time dayTime = new Time(); 
     dayTime.setToNow(); 


     int julianStartDay = Time.getJulianDay(System.currentTimeMillis(), dayTime.gmtoff); 


     dayTime = new Time(); 

     String[] resultStrs = new String[numDays]; 
     for(int i = 0; i < weatherArray.length(); i++) { 

      String day; 
      String description; 
      String highAndLow; 


      JSONObject dayForecast = weatherArray.getJSONObject(i); 


      long dateTime; 

      dateTime = dayTime.setJulianDay(julianStartDay+i); 
      day = getReadableDateString(dateTime); 


      JSONObject weatherObject = dayForecast.getJSONArray(OWM_WEATHER).getJSONObject(0); 
      description = weatherObject.getString(OWM_DESCRIPTION); 



      JSONObject temperatureObject = dayForecast.getJSONObject(OWM_TEMPERATURE); 
      double high = temperatureObject.getDouble(OWM_MAX); 
      double low = temperatureObject.getDouble(OWM_MIN); 

      highAndLow = formatHighLows(high, low); 



      resultStrs[i] = day + " - " + description + " - " + highAndLow; 
     } 

     for (String s : resultStrs) { 
      Log.v(LOG_TAG, "Forecast entry: " + s); 
     } 
     return resultStrs; 

    } 

LOGCAT:

TEMPなし値

org.json.JSONException:org.json.JSONObject.getで一時 なし値(JSONObject.java:389 ) at org.json.JSONObject.getJSONObject(JSONObject.java:609) at com.example.abhishekkumar.sunshine.app.Foreca stFragment $ FetchWeatherTask.getWeatherDataFromJson(ForecastFragment.java:177) でcom.example.abhishekkumar.sunshine.app.ForecastFragment $ FetchWeatherTask.doInBackground(ForecastFragment.java:279) com.example.abhishekkumar.sunshine.appで。 ForecastFragment $ FetchWeatherTask.doInBackground(ForecastFragment.java:92) android.os.AsyncTask $ 2.call(AsyncTask.java:295) at java.util.concurrent.FutureTask.run(FutureTask.java:237) でandroid .os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:234) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:11) 13) java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:588)java.lang.Thread.runで (Thread.java:818)

そして、私のopenweatherデータでです:

{"city":{"id":1260788,"name":"Pālam","coord":{"lon":77.099998,"lat":28.566669},"country":"IN","population":0,"sys":{"population":0}},"cod":"200","message":0.0155,"cnt":7,"list":[{"dt":1467018000,"main":{"temp":38.42,"temp_min":35.56,"temp_max":38.42,"pressure":986.68,"sea_level":1010.85,"grnd_level":986.68,"humidity":62,"temp_kf":2.85},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":36},"wind":{"speed":4.17,"deg":107.001},"sys":{"pod":"d"},"dt_txt":"2016-06-27 09:00:00"},{"dt":1467028800,"main":{"temp":33.82,"temp_min":31.92,"temp_max":33.82,"pressure":985.98,"sea_level":1009.98,"grnd_level":985.98,"humidity":76,"temp_kf":1.9},"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10d"}],"clouds":{"all":32},"wind":{"speed":3.11,"deg":99.5087},"rain":{"3h":5.54},"sys":{"pod":"d"},"dt_txt":"2016-06-27 12:00:00"},{"dt":1467039600,"main":{"temp":31.34,"temp_min":30.39,"temp_max":31.34,"pressure":987.55,"sea_level":1011.75,"grnd_level":987.55,"humidity":75,"temp_kf":0.95},"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10n"}],"clouds":{"all":36},"wind":{"speed":2.31,"deg":105.002},"rain":{"3h":4.07},"sys":{"pod":"n"},"dt_txt":"2016-06-27 15:00:00"},{"dt":1467050400,"main":{"temp":30.15,"temp_min":30.15,"temp_max":30.15,"pressure":988.78,"sea_level":1013.04,"grnd_level":988.78,"humidity":78,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":32},"wind":{"speed":3.22,"deg":100.501},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-06-27 18:00:00"},{"dt":1467061200,"main":{"temp":29.52,"temp_min":29.52,"temp_max":29.52,"pressure":987.7,"sea_level":1012.15,"grnd_level":987.7,"humidity":81,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":64},"wind":{"speed":2.97,"deg":96.0021},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-06-27 21:00:00"},{"dt":1467072000,"main":{"temp":26.71,"temp_min":26.71,"temp_max":26.71,"pressure":988.06,"sea_level":1012.31,"grnd_level":988.06,"humidity":94,"temp_kf":0},"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10d"}],"clouds":{"all":76},"wind":{"speed":0.71,"deg":189.5},"rain":{"3h":3.205},"sys":{"pod":"d"},"dt_txt":"2016-06-28 00:00:00"},{"dt":1467082800,"main":{"temp":29.3,"temp_min":29.3,"temp_max":29.3,"pressure":990,"sea_level":1014.38,"grnd_level":990,"humidity":91,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":80},"wind":{"speed":2.86,"deg":100.504},"rain":{"3h":0.005},"sys":{"pod":"d"},"dt_txt":"2016-06-28 03:00:00"}]} 
+0

コードを適切な形式で入力してください。各コード行の前に4つのスペースを提供する –

答えて

0

あなたの一時オブジェクトを持っていないJSON、代わりに3台の山車、

"main" : { "grnd_level" : 1009.13, 
     "humidity" : 95, 
     "pressure" : 1009.13, 
     "sea_level" : 1018.03, 
     "temp" : 22.949999999999999, 
     "temp_kf" : 0, 
     "temp_max" : 22.949999999999999, 
     "temp_min" : 22.949999999999999 
     }, 

私はあなたが抽出したい値であると推測しています。

0

JSON解析のためにgetの代わりにユーザーを選択します。例: getString()の代わりにoptString()を使用します。 実際にキーが利用できない場合、デフォルトでoptによって自動的に処理されます。

1

私は同じ問題を抱えています。

"main":{ 
     "temp":26.19, 
     "temp_min":21.45, 
     "temp_max":26.19, 
     "pressure":869.02, 
     "sea_level":1026.9, 
     "grnd_level":869.02, 
     "humidity":43, 
     "temp_kf":4.75 
    } 

コードがtempオブジェクト内temp_mintemp_maxにアクセスしようとしているのに対しtemptemp_mintemp_maxがフォーマットJSONデータにmainオブジェクトの内側にあることに注意してください。

ただ、次のように変更します。

  1. JSONObject temperatureObject = dayForecast.getJSONObject(OWM_TEMPERATURE);OWM_DESCRIPTIONOWM_TEMPERATUREを交換し
  2. OWM_MIN = "temp_min";(代わりにOWM_MIN = "min"の)

を行います(代わりにOWM_MAX = "max"の)OWM_MAX = "temp_max";を作り、あなたのコードが実行されます。

関連する問題