2015-06-11 46 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