Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将getforobject重新模板化为java类_Java_Spring - Fatal编程技术网

将getforobject重新模板化为java类

将getforobject重新模板化为java类,java,spring,Java,Spring,我想将以下json解析为java类: { "coord": { "lon": 5, "lat": 51 }, "weather": [ { "id": 211, "main": "Thunderstorm", "description": "onweersbui", "icon": "11d" } ], "base": "stations", "main": { "temp": 20.

我想将以下json解析为java类:

{
  "coord": {
    "lon": 5,
    "lat": 51
  },
  "weather": [
    {
      "id": 211,
      "main": "Thunderstorm",
      "description": "onweersbui",
      "icon": "11d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 20.84,
    "pressure": 995,
    "humidity": 77,
    "temp_min": 18,
    "temp_max": 23
  },
  "visibility": 10000,
  "wind": {
    "speed": 3.6,
    "deg": 90
  },
  "clouds": {
    "all": 75
  },
  "dt": 1494516300,
  "sys": {
    "type": 1,
    "id": 4842,
    "message": 0.0047,
    "country": "BE",
    "sunrise": 1494474907,
    "sunset": 1494530314
  },
  "id": 7668893,
  "name": "Scherpenheuvel-Zichem",
  "cod": 200
}
用于解析的代码为:

Weatherinformation response = restTemplate.getForObject(URL, Weatherinformation.class);
天气信息类:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Weatherinformation {

private Main main;
private List weather;
private String name;

public List getWeather() {
    return weather;
}

public void setWeather(List weather) {
    this.weather = weather;
}

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

public Main getMain() {
    return this.main;
}

public void setMain(Main main) {
    this.main = main;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class Weather {

    private String description;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

@JsonIgnoreProperties(ignoreUnknown = true)
public class Main {

    private double temp;
    private double temp_min;
    private double temp_max;

    public double getTemp() {
        return temp;
    }

    public void setTemp(double temp) {
        this.temp = temp;
    }

    public double getTemp_min() {
        return temp_min;
    }

    public void setTemp_min(double temp_min) {
        this.temp_min = temp_min;
    }

    public double getTemp_max() {
        return temp_max;
    }

    public void setTemp_max(double temp_max) {
        this.temp_max = temp_max;
    }
}

}

到目前为止,除了获取天气描述外,一切都正常。

我认为在WeatherInformation类中,“列表天气”应该是“天气[]”。

我认为“列表天气”应该是“天气[]”在WeatherInformation类中。

从内部移动
WeatherInformation
内部类,使其成为一个单独的类或使内部类静态。

从内部移动
WeatherInformation
内部类,使其成为一个单独的类或使内部类静态。

提供的格式不正确JSON格式错误百出,与Java类不匹配。oops通常不匹配,这只是因为发布了本文。刚刚更正了@ashraful,你为什么不使用泛型呢。您是否尝试过使用
List
而不是Weatherinformation类中的List尝试过但不起作用@pvpkiranjson您提供的数据格式不正确JSON格式错误百出,与Java类不匹配。oops通常不匹配,这只是因为发布此帖子。刚刚更正了@ashraful,你为什么不使用泛型呢。您是否尝试过使用
List
而不是Weatherinformation类中的List尝试过,但在@pvpkiranth下无法工作这给了我一个异常:线程“main”中的异常org.springframework.http.converter.httpMessageNodeTableException:无法读取文档:无法构造view.controller.Weatherinformation$Weather的实例:未找到合适的构造函数,无法从对象值反序列化(缺少默认构造函数或创建者,或者可能需要添加/启用类型信息?)这给了我这个异常:线程“main”中的异常org.springframework.http.converter.httpMessageNodeTableException:无法读取文档:无法构造view.controller.Weatherinformation$Weather的实例:未找到合适的构造函数,无法从对象值反序列化(缺少默认构造函数或创建者,或者可能需要添加/启用类型信息?)