我已经编写了这个函数,用于使用openweather API获取JSON天气数据。我有错误org.json.JSONException:使用OWM_TEMPARATURE时没有临时值但是当我根本不使用temparature字段时,我正确地得到了日期和描述。
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的:
没有临时值
org.json.JSONException:没有临时值 at org.json.JSONObject.get(JSONObject.java:389) at org.json.JSONObject.getJSONObject(JSONObject.java:609) 在 com.example.abhishekkumar.sunshine.app.ForecastFragment $ 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) 在java.util.concurrent.FutureTask.run(FutureTask.java:237) 在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:234) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 在 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 :(得分:2)
我遇到了同样的问题。
"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_min
,temp_max
位于格式化JSON数据中的main
对象内,而代码正在尝试访问temp_min
和{{1内部temp_max
对象。
只需进行以下更改:
temp
替换为OWM_TEMPERATURE
OWM_DESCRIPTION
JSONObject temperatureObject = dayForecast.getJSONObject(OWM_TEMPERATURE);
(而不是OWM_MAX = "temp_max";
)OWM_MAX = "max"
(而不是OWM_MIN = "temp_min";
)您的代码将会运行。
答案 1 :(得分:1)
用户选择而不是获取JSON解析。 例如而不是getString(),使用optString()。 实际上当密钥不可用时,将通过opt自动处理默认值。
答案 2 :(得分:0)
您的JSON没有临时对象,而是三个浮点数,
"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
},
我猜这些是你想要提取的价值。