我在从Android应用中的JSon字符串中检索数据时遇到问题。 这是通过世界天气在线API获取天气的应用程序。 所以,这是JSON响应:
{
"data": {
"current_condition": [
{
"cloudcover": "25",
"humidity": "100",
"observation_time": "10:46 PM",
"precipMM": "0.0",
"pressure": "1006",
"temp_C": "-5",
"temp_F": "23",
"visibility": "0",
"weatherCode": "323",
"weatherDesc": [
{
"value": "Patchy light snow"
}
],
"weatherIconUrl": [
{
"value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0027_light_snow_showers_night.png"
}
],
"winddir16Point": "E",
"winddirDegree": "85",
"windspeedKmph": "0",
"windspeedMiles": "0"
}
],
"request": [
{
"query": "Bruxelles, Belgium",
"type": "City"
}
],
"weather": [
{
"date": "2013-01-22",
"precipMM": "0.3",
"tempMaxC": "1",
"tempMaxF": "33",
"tempMinC": "-6",
"tempMinF": "22",
"weatherCode": "113",
"weatherDesc": [
{
"value": "Sunny"
}
],
"weatherIconUrl": [
{
"value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
}
],
"winddir16Point": "E",
"winddirDegree": "95",
"winddirection": "E",
"windspeedKmph": "13",
"windspeedMiles": "8"
},
{
"date": "2013-01-23",
"precipMM": "0.3",
"tempMaxC": "-3",
"tempMaxF": "26",
"tempMinC": "-8",
"tempMinF": "19",
"weatherCode": "116",
"weatherDesc": [
{
"value": "Partly Cloudy"
}
],
"weatherIconUrl": [
{
"value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png"
}
],
"winddir16Point": "ENE",
"winddirDegree": "65",
"winddirection": "ENE",
"windspeedKmph": "15",
"windspeedMiles": "9"
}
]
}
}
除了从“current_condition”中检索int之外,一切正常。 我可以把双重视为“cloudcover”,我可以从“天气”中正确地获得“tempMaxC”(int)。
这是代码:
public WeatherResponse getWeatherResponse(){
w_res = new WeatherResponse();
try{
JSONObject data = new JSONObject(json_obj.getString("data"));
JSONArray cur_cond = data.getJSONArray("current_condition");
JSONArray weather = data.getJSONArray("weather");
w_res.setCloudCover(cur_cond.getJSONObject(0).getDouble("cloudcover"));
w_res.setHumidity(cur_cond.getJSONObject(0).getDouble("humidity"));
w_res.setObservationTime(cur_cond.getJSONObject(0).getString("observation_time"));
w_res.setTemp_C(cur_cond.getJSONObject(0).getInt("temp_C"));
w_res.setDate(weather.getJSONObject(0).getString("date"));
w_res.setTempMinC(weather.getJSONObject(0).getInt("tempMinC"));
w_res.setTempMaxC(weather.getJSONObject(0).getInt("tempMaxC"));/**/
}
catch(Exception e){
e.getMessage().toString();
}
return w_res;
}
其中WeatherResponse是我为此目的编写的可序列化类。
有什么想法吗?
我认为我的问题有点令人困惑......
我没有遇到任何异常......这将是...我可以检索除了之外的所有内容 来自“current_condition”的int .... 在这种情况下,我无法获得temp_C
这是我的WeatherResponse类
package com.example.wdemoapp.utils;
import java.io.*;
import java.util.*;
public class WeatherResponse implements Serializable{
/**
* date
*/
public String date;
/**
* time
*/
private String observationTime;
/**
* "%"
*/
private double humidity;
private double cloudCover;
/**
* temperature
*/
private int tempMaxC;
private int temp_C;
private int tempMinC;
/**
* Wind speed
*/
private int windSpeedKmph;
/**
* Weather condition code
*/
private int weatherCode;
/**
* Weather icon url
*/
private List<String> weatherIconUrl;
/**
* Weather description text
*/
private List<String> weatherDesc;
/**
* Precipitation amount in millimetre
*/
private double precipMm;
/**
* methods
*/
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getObservationTime() {
return observationTime;
}
public void setObservationTime(String observationTime) {
this.observationTime = observationTime;
}
public double getCloudCover() {
return cloudCover;
}
public void setCloudCover(double cloudCover) {
this.cloudCover = cloudCover;
}
public double getHumidity() {
return humidity;
}
public void setHumidity(double humidity) {
this.humidity = humidity;
}
public int getTempMaxC() {
return tempMaxC;
}
public void setTempMaxC(int tempMaxC) {
this.tempMaxC = tempMaxC;
}
public int getTemp_C() {
return temp_C;
}
public void setTemp_C(int tempMaxC) {
this.temp_C = temp_C;
}
public int getTempMinC() {
return tempMinC;
}
public void setTempMinC(int tempMinC) {
this.tempMinC = tempMinC;
}
public int getWindSpeedKmph() {
return windSpeedKmph;
}
public void setWindSpeedKmph(int windSpeedKmph) {
this.windSpeedKmph = windSpeedKmph;
}
public int getWeatherCode() {
return weatherCode;
}
public void setWeatherCode(int weatherCode) {
this.weatherCode = weatherCode;
}
public List<String> getWeatherIconUrl() {
return weatherIconUrl;
}
public void setWeatherIconUrl(List<String> weatherIconUrl) {
this.weatherIconUrl = weatherIconUrl;
}
public List<String> getWeatherDesc() {
return weatherDesc;
}
public void setWeatherDesc(List<String> weatherDesc) {
this.weatherDesc = weatherDesc;
}
public double getPrecipMm() {
return precipMm;
}
public void setPrecipMm(double precipMm) {
this.precipMm = precipMm;
}
}
我尝试将“temp_C”声明为双(我可以获得“cloudcover”,所以我猜它有效)和作为字符串(因为我也能得到“Date”,“observation_time”...) 最奇怪的是我也可以从“天气”中得到“tempMaxC”和“tempMinC”所以,一般来说它似乎不是真正的问题,而是从“current_condition”中检索
抱歉......这不是答案可能会有所帮助:
我检索“0”作为“temp_C”的值,所以我检索错误的结果!!!