2016-11-05 38 views
0

我使用改造來消費天氣api,之前我不得不處理我自己的後端,所以我可以調整它來很好地解析數據。這一次,我得到的JSON看起來是這樣的:Retrofit 2.0解析複雜的api

{ 
    "city": { 
    "id": 838920, 
    "name": "Ledine", 
    "coord": { 
     "lon": 20.34833, 
     "lat": 44.802502 
    }, 
    "country": "RS", 
    "population": 0, 
    "sys": { 
     "population": 0 
    } 
    }, 
    "cod": "200", 
    "message": 0.2644, 
    "cnt": 35, 
    "list": [ 
    { 
     "dt": 1478271600, 
     "main": { 
     "temp": 281.58, 
     "temp_min": 280.9, 
     "temp_max": 281.58, 
     "pressure": 1024.56, 
     "sea_level": 1035.66, 
     "grnd_level": 1024.56, 
     "humidity": 90, 
     "temp_kf": 0.68 
     }, 
     "weather": [ 
     { 
      "id": 800, 
      "main": "Clear", 
      "description": "clear sky", 
      "icon": "01d" 
     } 
     ], 
     "clouds": { 
     "all": 0 
     }, 
     "wind": { 
     "speed": 3.04, 
     "deg": 106.5 
     }, 
     "rain": {}, 
     "sys": { 
     "pod": "d" 
     }, 
     "dt_txt": "2016-11-04 15:00:00" 
    }, 

    { 
     ...... 
     ...... 
    } 
    ] 
} 

把我所造似乎複雜的模型,我與這個掙扎的最後一個星期左右。任何幫助將非常感激。謝謝!

+1

什麼問題? – Blackbelt

+0

你有沒有考慮過使用http://www.jsonschema2pojo.org? – EpicPandaForce

+0

我做了,它使11個類(我認爲這是不對的)我只是想能夠正確列出一些數據。爲了滿足我需要的東西是city.name,city.country和list [0] .main,weather,clouds,wind,rain – vibetribe93

回答

0

很簡單:
你的模型必須是這樣的:

public class Weather implements Serializable { 


public CityObj city; 
public class CityObj { 
    public String name; 
    public int id; 
    public coordObj coord; 
    public String country; 
    public String population; 
    public sysObj sys; 
} 

public class coordObj { 
    public long lon; 
    public long lat; 

} 
public class sysObj { 
    public int population; 
} 
. 
. 
. 

}