Gson 没有反序列化 JSON 数据

Gson not deserializing JSON data

我正在尝试从 Yahoo API 获取一些天气信息。这是我的 JSON:

JSON

这是我的 DTO:

public class forecast implements Serializable {

private static final long serialVersionUID = -520652416977871134L;
private String text;
private String high;
private String day;
private String code;
private String low;
private String date;

public forecast() {
}


public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public String getHigh() {
    return high;
}

public void setHigh(String high) {
    this.high = high;
}

public String getDay() {
    return day;
}

public void setDay(String day) {
    this.day = day;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getLow() {
    return low;
}

public void setLow(String low) {
    this.low = low;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

@Override
public String toString() {
    return "ClassPojo [text = " + text + ", high = " + high + ", day = "
            + day + ", code = " + code + ", low = " + low + ", date = "
            + date + "]";
}
}

我只对 forecast 元素感兴趣。

当我尝试将反序列化的数据读入我的 DTO 时,所有这些数据都是空的。我感觉我没有正确格式化我的 DTO。

此外,将 JSON 映射到 POJO 的正确方法是什么?

编辑:这是我的反序列化代码

    String endpoint = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20"
            + "where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22Rhodes%2C%20Gr%22)&"
            + "format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
    try {
        URL endpointURL = new URL(endpoint);
        HttpURLConnection connection = (HttpURLConnection) endpointURL
                .openConnection();
        connection.connect();
        InputStream input = connection.getInputStream();
        JsonReader reader = new JsonReader(new InputStreamReader(input));
        reader.setLenient(true);
        forecast response = new Gson().fromJson(reader,
                forecast.class);
        Log.d("forecast", response.toString());//override toString() to return all the values of the object
    } catch (IOException e) {
        e.printStackTrace();
    }

您的 JSON(您从 Yahoo 获得)非常复杂。所以它不能轻易映射到简单的 POJO(但你仍然可以编写包含所有相应嵌套 JSON 元素的字段的巨大 POJO)。

但是可以从 JSON.

中解析和提取特定元素

代码:

public static void main(String[] args) {
    String endpoint = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20"
            + "where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22Rhodes%2C%20Gr%22)&"
            + "format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
    try {
        URL endpointURL = new URL(endpoint);
        HttpURLConnection connection = (HttpURLConnection) endpointURL
                .openConnection();
        connection.connect();
        InputStream input = connection.getInputStream();
        JsonReader reader = new JsonReader(new InputStreamReader(input));
        reader.setLenient(true);

        JsonElement forecastSubObject = new JsonParser().parse(reader).
                getAsJsonObject().get("query").
                getAsJsonObject().get("results").
                getAsJsonObject().get("channel").
                getAsJsonObject().get("item").
                getAsJsonObject().get("forecast");  

        System.out.println(forecastSubObject.toString());   

        List<forecast> forecasts = (List<forecast>)new Gson().fromJson(forecastSubObject, List.class);

        System.out.println("forecast : " + forecasts);
        System.out.println("first forecast: " + forecasts.get(0));      
    } catch (IOException e) {
        e.printStackTrace();
    }
}

使用 JsonParser 你可以遍历元素(通过他们的名字)。当到达 'forecast' 元素时,将提取相应的字符串。然后它像往常一样解析对象并映射到您的预测 POJO 列表。

一般来说映射to/fromJSON是一个很广的领域。不同的库提供了不同的方法来实现这一点(从简单和肮脏到复杂但可靠)。