2015-06-11 102 views
1

我想从Yahoo API获取一些天气信息。这是我的JSON:Gson不反序列化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(); 
    } 
+2

您使用GSON的代码在哪里? –

+0

在我使用GSON的地方添加了代码 –

+0

你如何调用'Gson'?由于'forecast'是内部元素之一,它是一个对象数组,您不能忽略其他所有内容,并直接从JSON获取一个'forecast'实例。 – m4ktub

回答

1

你的JSON(你从雅虎获得)是非常复杂的代码。所以它不能轻易映射到简单的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列表。

一般来说,映射到/来自JSON的球体非常广泛。不同的库提供了不同的方式来实现这个目标(从简单到肮脏到复杂但可靠)。

+0

感谢您的答案@ flaz14。让我试试这个并回到你身边。 –

+0

嗯,我只需要预测元素,所以我尝试'getAsJsonObject()。get(“forecast”),但仍然是'null'对象。对此有何想法?我也注意到'forecast'是一个数组,但是GSON api不同意我抛出一个异常,说这不是数组。 –

+1

我编辑了代码。因此,预测POJO的所有数组都加载了'List forecast =(List )new Gson()。fromJson(forecastSubObject,List.class);'加载这个数组。然后,可以从“预测”列表中选取任何需要的项目。 – flaz14