2015-04-23 72 views
0

我想創建一個pojo,可以同時保存兩個系列,並且當需要時,系列二。數據按年變化; 1975年有降雨數據,但1976年有雨雪數據。換句話說,我希望我的pojo能夠持有多年只有雨量數據的雨量數據,以及多年有雨/雪數據的雨雪數據。如何利用重載pojo構造函數進行改造?

{ 
    "status": "REQUEST_SUCCEEDED", 
    "responseTime": 313, 
    "message": [ 
     "No Data Available for Series 2 Year: 1975" 
    ], 
    "Results": { 
     "series": [ 
      { 
       "seriesID": "1", 
       "data": [ 
        { 
         "year": "1976", 
         "period": "M12", 
         "periodName": "December", 
         "value": "171.8", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M11", 
         "periodName": "November", 
         "value": "172.1", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M10", 
         "periodName": "October", 
         "value": "172.7", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M09", 
         "periodName": "September", 
         "value": "173.3", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M08", 
         "periodName": "August", 
         "value": "174.2", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M07", 
         "periodName": "July", 
         "value": "174.8", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M06", 
         "periodName": "June", 
         "value": "176.0", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M05", 
         "periodName": "May", 
         "value": "176.9", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M04", 
         "periodName": "April", 
         "value": "178.1", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M03", 
         "periodName": "March", 
         "value": "178.7", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M02", 
         "periodName": "February", 
         "value": "179.0", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M01", 
         "periodName": "January", 
         "value": "179.6", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1975", 
         "period": "M12", 
         "periodName": "December", 
         "value": "179.9", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1975", 
         "period": "M11", 
         "periodName": "November", 
         "value": "180.8", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1975", 
         "period": "M10", 
         "periodName": "October", 
         "value": "182.0", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1975", 
         "period": "M09", 
         "periodName": "September", 
         "value": "182.9", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1975", 
         "period": "M08", 
         "periodName": "August", 
         "value": "183.8", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1975", 
         "period": "M07", 
         "periodName": "July", 
         "value": "184.4", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1975", 
         "period": "M06", 
         "periodName": "June", 
         "value": "186.4", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1975", 
         "period": "M05", 
         "periodName": "May", 
         "value": "187.9", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1975", 
         "period": "M04", 
         "periodName": "April", 
         "value": "188.8", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1975", 
         "period": "M03", 
         "periodName": "March", 
         "value": "189.7", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1975", 
         "period": "M02", 
         "periodName": "February", 
         "value": "190.3", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1975", 
         "period": "M01", 
         "periodName": "January", 
         "value": "191.8", 
         "footnotes": [ 
          {} 
         ] 
        } 
       ] 
      }, 
      { 
       "seriesID": "2", 
       "data": [ 
        { 
         "year": "1976", 
         "period": "M12", 
         "periodName": "December", 
         "value": "47.5", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M11", 
         "periodName": "November", 
         "value": "47.7", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M10", 
         "periodName": "October", 
         "value": "47.8", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M09", 
         "periodName": "September", 
         "value": "47.8", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M08", 
         "periodName": "August", 
         "value": "47.7", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M07", 
         "periodName": "July", 
         "value": "47.3", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M06", 
         "periodName": "June", 
         "value": "46.8", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M05", 
         "periodName": "May", 
         "value": "45.5", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M04", 
         "periodName": "April", 
         "value": "44.9", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M03", 
         "periodName": "March", 
         "value": "45.1", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M02", 
         "periodName": "February", 
         "value": "45.5", 
         "footnotes": [ 
          {} 
         ] 
        }, 
        { 
         "year": "1976", 
         "period": "M01", 
         "periodName": "January", 
         "value": "45.9", 
         "footnotes": [ 
          {} 
         ] 
        } 
       ] 
      } 
     ] 
    } 
} 
+0

創建一個包含雨雪數據的pojo,並且您可以檢查一年的雪數據是否爲空,如果它不爲null,那一年包含下雪和降雨數據 –

+0

我想知道如何導致雪和日子到pojo,因爲它們都被標記爲「價值」 – Pwnies

+0

不重要,因爲值總是具有相同的數據類型,並通過檢查年份,你可以檢查它是否是雪數據或雨天數據 –

回答

0

我創建一個POJO在數據數組的對象

public class WeatherData 
{ 
public String year; 
public String period; 
public String periodName; 
public String Value; 
List<Footnotes> footnotes; 
} 

您可以創建getter和setter由你自己,我不知道腳註,因爲它始終是空的,所以你可以改變的根據您的意願,您可以通過查看會話系列ID來檢查是下雨數據還是下雪數據,因爲通過查看您的JSON,我認爲seriesID 1包含降雨數據,而seriesID 2包含降雪數據,但是我會建議@ frek13也提到它會更好,如果你可以有兩個變量一個用於降雨數據,一個用於降雪數據

+0

如何設置改造來完成這項工作?我已經使用jsonschema2pojo。我把結果,系列和基準作爲我的模型;我擺脫了腳註,因爲我真的沒有看到它的用途。我如何設置我的數據,以便它能在雨和雪中變化? – Pwnies

+0

用您創建的POJO創建回調,回調將存儲您的回覆 –