[英]Parse JSON object containing multiple nested objects without making class for each nested object
[英]Parse Json to object containing objects
我有以下代碼與json,我從accuweather得到
{
"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
數組, Temperature
包含TemperatureItems
(在json中命名為minimum和maximum)等等,所有模型都有私有字段和公共構造函數,getter和setter。 但是我沒有一些字段,因為我想省略它們(Day,Night,EpochDate,Source)。
當我運行程序時,我收到錯誤
com.fasterxml.jackson.databind.exc.InvalidDefinitionException:無法構造
test.models.weather.Weather
實例(沒有test.models.weather.Weather
,如默認構造,存在):無法從Object值反序列化(沒有基於委托或屬性的Creator)
我也嘗試過Gson
但它返回Null值的對象
難道我做錯了什么? 還有另一種方法嗎?
編輯 :這些是模型,@ LazerBass是對的,因為我首先沒有包含默認構造函數,現在錯誤已經改變:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:無法識別的字段“標題”(類test.models.weather.Weather),未標記為可忽略(2個已知屬性:“標題”,“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字符串轉換為小寫,我可以得到一些值,雖然沒有解析Array
和LocalDateTime
從異常消息判斷我猜你的Weather
類正在使用無參數構造函數。 嘗試添加一個。 例如
public class Weather {
public Weather() {
// no arg constructor needed for jackson
}
}
要生成Weather類及其對應的類,請使用以下鏈接並選擇源類型為json。 它將根據json字符串生成所需的類。
http://www.jsonschema2pojo.org/
生成類后,可以使用@JsonIgnore注釋不需要的字段。
當Jackson
失敗時會出現“ 沒有創建者,就像默認構造,存在 ”這樣的消息,它需要你模型中每個POJO
類的默認的public
no-argument
構造函數。
當它失敗並顯示“ 無法識別的字段...未標記為可忽略的... ”之類的消息時,您需要禁用FAIL_ON_UNKNOWN_PROPERTIES功能。
也可以看看:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.