我用的是
ResponseEntity<List<item>> res = restTemplate.exchange(
"http://localhost:8080/page",
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<item>>() {});对于我的一个应用程序,但因为我做了rest服务,它只通过Json返回了2个项目。我的问题是,我正在尝试获取天气和我使用的api的返回值:
{"coord":{"lon":-0.13,"lat":51.51},"weather": [{"id":741,"main":"Fog","description":"fog","icon":"50n"},{"id":701,"main":"Mist","description":"mist","icon":"50n"}],"base":"stations","main":{"temp":278.47,"pressure":1012,"humidity":100,"temp_min":277.15,"temp_max":279.15},"visibility":10000,"wind":{"speed":1},"clouds":{"all":75},"dt":1479864000,"sys":{"type":1,"id":5093,"message":0.0311,"country":"GB","sunrise":1479886350,"sunset":1479916868},"id":2643743,"name":"London","cod":200}这是大量的JSON,所以我想知道我是否必须用setter/getter( coord字段、id、描述、just、name等)来创建一个包含所有这些对象的类,或者我可以只使用coord对象和weather对象来创建一个对象,并且只映射这些字段?
谢谢你的建议。
编辑:
也许这与@JsonIgnoreProperties标签的ignoreUnknown = true属性有关?
发布于 2016-11-23 11:15:45
我认为您只需要两个类来设置它们的getter和setter类。你的“天气”变量只是列表,其他的是静态变量。我会这样做:
public class Weather {
private Long id;
private String name;
private Long cod;
private coordLong string;
private coordLat string;
priva... baseStations, cloudsAll, windSpeed, mainTemp...
private List<WeatherList> weatherList;
...
//getters setters发布于 2016-11-23 11:33:02
如果您想映射所有变量,只需创建一个Model类并映射您的响应,如下所示
public class WeatherData{
private Coordinate coord;
private List<Weather> weather;
//Getters and setters setCoord(),getCoord().setWeather(),getWeather().
public class Coordinate{
private Double lon;
private Double lat;
//Getters and Setters
}
public class Weather{
private Integer id;
private String main;
private String description;
private String icon;
//Getters and setters
}
//Remaining fields
}然后像这样映射您的json
//json is your response in String format
Gson gson = new Gson();
WeatherData weatherData = gson.fromJson(json,WeatherData.class);
//handle exception here然后得到任何像这样的变量值
lat = weatherData.getCoord().getLat();https://stackoverflow.com/questions/40755264
复制相似问题