gpt4 book ai didi

java - 将 Json 解析为包含对象的对象

转载 作者:行者123 更新时间:2023-11-29 04:07:45 25 4
gpt4 key购买 nike

我有以下来自 accuweather 的 json 代码

{  
"Headline":{
"EffectiveDate":"2019-07-29T07:00:00+06:00",
"EffectiveEpochDate":1564362000,
"Severity":3,
"Text":"The heat wave will continue through Friday",
"Category":"heat",
"EndDate":"2019-08-02T19:00:00+06:00",
"EndEpochDate":1564750800
},
"DailyForecasts":[
{
"Date":"2019-07-29T07:00:00+06:00",
"EpochDate":1564362000,
"Temperature":{
"Minimum":{
"Value":19.1,
"Unit":"C",
"UnitType":17
},
"Maximum":{
"Value":36.7,
"Unit":"C",
"UnitType":17
}
},
"Day":{
"Icon":30,
"IconPhrase":"Hot",
"HasPrecipitation":false
},
"Night":{
"Icon":35,
"IconPhrase":"Partly cloudy",
"HasPrecipitation":false
},
"Sources":[
"AccuWeather"
]
}
]
}

我尝试通过 jackson 将这个对象解析为 POJO

public static void main( String[] args )
{
String x = "{\"Headline\":{\"EffectiveDate\":\"2019-07-29T07:00:00+06:00\",\"EffectiveEpochDate\":1564362000,\"Severity\":3,\"Text\":\"The heat wave will continue through Friday\",\"Category\":\"heat\",\"EndDate\":\"2019-08-02T19:00:00+06:00\",\"EndEpochDate\":1564750800},\"DailyForecasts\":[{\"Date\":\"2019-07-29T07:00:00+06:00\",\"EpochDate\":1564362000,\"Temperature\":{\"Minimum\":{\"Value\":19.1,\"Unit\":\"C\",\"UnitType\":17},\"Maximum\":{\"Value\":36.7,\"Unit\":\"C\",\"UnitType\":17}},\"Day\":{\"Icon\":30,\"IconPhrase\":\"Hot\",\"HasPrecipitation\":false},\"Night\":{\"Icon\":35,\"IconPhrase\":\"Partly cloudy\",\"HasPrecipitation\":false},\"Sources\":[\"AccuWeather\"]}]}";
ObjectMapper objectMapper = new ObjectMapper();

try {
Weather weather = objectMapper.readValue(x, Weather.class);

System.out.println(weather);
} catch (IOException e) {
e.printStackTrace();
}
}

我在 json 中指定了所有模型,例如 Headline , DailyForecasts 的数组, TemperatureTemperatureItems 组成(在 json 中命名为 minimum 和 maximum)等等,它们都有私有(private)字段和公共(public)构造函数、getter 和 setter。但是我没有一些字段,因为我想省略它们(日、夜、EpochDate、来源)。

当我运行程序时出现错误

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of test.models.weather.Weather (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

我也试过Gson但它返回具有 Null 值的对象

我做错了什么吗?还有其他方法吗?

编辑:这些是模型,@LazerBass 是对的,因为我首先没有包含默认构造函数,现在错误已经改变:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Headline" (class test.models.weather.Weather), not marked as ignorable (2 known properties: "headline", "dailyForecasts"])

public class TemperatureItem {
public double value;
public String unit;
public String unitType;

public TemperatureItem() {

}

//Getters and setters
}


public class Temperature {
private TemperatureItem maximum;
private TemperatureItem minimum;

public Temperature(TemperatureItem maximum, TemperatureItem minimum) {
this.maximum = maximum;
this.minimum = minimum;
}

public Temperature() {

}
//Getters and setters
}

public class DailyForecasts {
private LocalDateTime date;
private Temperature temperature;

public DailyForecasts(LocalDateTime date, Temperature temperature) {
this.date = date;
this.temperature = temperature;
}

public DailyForecasts() {

}
//Getters and setters
}

public class Headline {
private LocalDateTime effectiveDate;
private int severity;
private String text;
private String category;
private LocalDateTime endDate;

public Headline() {

}

public Headline(LocalDateTime effectiveDate, Integer severity, String text, String category, LocalDateTime endDate) {
this.effectiveDate = effectiveDate;
this.severity = severity;
this.text = text;
this.category = category;
this.endDate = endDate;
}
//Getters and setters
}

public class Weather {
private Headline headline;
private DailyForecasts[] dailyForecasts;

public Weather() {

}
public Weather(Headline headline, DailyForecasts[] dailyForecasts) {
this.headline = headline;
this.dailyForecasts = dailyForecasts;
}
//Getters and setters
}

我发现,如果我将 json 字符串转换为小写,我可以获得一些值,尽管 ArrayLocalDateTime没有被解析

最佳答案

要生成 Weather 类及其对应的类,请使用以下链接并将源类型选择为 json。它将根据 json 字符串生成所需的类。

http://www.jsonschema2pojo.org/

生成类后,可以用@JsonIgnore注解不需要的字段。

关于java - 将 Json 解析为包含对象的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57257951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com