我需要在POJO中反序列化具有多个类的复杂JSON
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我需要在POJO中反序列化具有多个类的复杂JSON相关的知识,希望对你有一定的参考价值。
我知道我做错了,但是,要努力找出答案。请帮忙。
[Jackson Json Conf骆驼蓝图春季:
<bean class="com.sage.entities.MyPojo" id="myPojo"/>
<dataFormats>
<json disableFeatures="FAIL_ON_UNKNOWN_PROPERTIES"
enableFeatures="ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT,ACCEPT_EMPTY_STRING_AS_NULL_OBJECT"
id="AJson2Pojo4"
library="Jackson" prettyPrint="true" unmarshalTypeName="com.sage.entities.MyPojo"/>
</dataFormats>
<unmarshal><custom ref="AJson2Pojo4"/></unmarshal>
我的故事:我成功地反序列化了一个复杂的JSON。在这一点上,我无法弄清楚自己在做什么。先感谢您。
我的POJO。这可行!但是我需要更深入地研究内部类。
public class MyPojo
private static final long serialVersionUID = 202003211122L;
public MyPojo()
private String httpStatus;
Payload PayloadObject;
ArrayList<Object> errorMessages = new ArrayList<Object>();
// Getter Methods
public String getHttpStatus()
return httpStatus;
public Payload getPayload()
return PayloadObject;
// Setter Methods
public void setHttpStatus( String httpStatus )
this.httpStatus = httpStatus;
public void setPayload( Payload payloadObject )
this.PayloadObject = payloadObject;
public class Payload
ArrayList<Object> internalRecords = new ArrayList<Object>();
ArrayList<Object> records = new ArrayList<Object>();
ArrayList<Object> timeSeriesRecords = new ArrayList<Object>();
public ArrayList<Object> getTimeSeriesRecords()
return timeSeriesRecords;
public void setTimeSeriesRecords(ArrayList<Object> timeSeriesRecords)
this.timeSeriesRecords = timeSeriesRecords;
public ArrayList<Object> getInternalRecords()
return internalRecords;
public ArrayList<Object> getRecords()
return records;
public void setInternalRecords(ArrayList<Object> internalRecords)
this.internalRecords = internalRecords;
public void setRecords(ArrayList<Object> records)
this.records = records;
这有效,将JSON反序列化为此。这只是一个对象。
timeSeriesRecords: name=706247, timestamps=[2020-06-05T08:28:59, 2020-06-05T08:53:17, 2020-06-05T10:55:34, 2020-06-05T12:57:43, 2020-06-05T14:57:44, 2020-06-04T22:37:16, 2020-06-04T23:05:51, 2020-06-05T01:07:29, 2020-06-05T03:07:31], data=[name=flight_phase, data=[TAKEOFF, CRUISE, CRUISE, CRUISE, CRUISE, TAKEOFF, CRUISE, CRUISE, CRUISE], arraySizes=[9], name=DPOIL, data=[null, -15.4604, -13.8422, -13.2137, -13.5186, null, -16.8422, -14.7853, -13.5696], arraySizes=[9], name=POIL, data=[null, 72.4604, 69.8422, 69.2137, 69.5186, null, 69.8422, 67.7853, 67.5696], arraySizes=[9], name=GN2MC, data=[null, 1.66469, 2.19799, 2.05127, 2.29911, null, 2.20363, 2.40703, 2.38929], arraySizes=[9], name=DELN1, data=[6.23079, null, null, null, null, -0.370548], arraySizes=[6], name=message_date_time, data=[2020-06-05T08:28:59, 2020-06-05T08:53:17, 2020-06-05T10:55:34, 2020-06-05T12:57:43, 2020-06-05T14:57:44, 2020-06-04T22:37:16, 2020-06-04T23:05:51, 2020-06-05T01:07:29, 2020-06-05T03:07:31], arraySizes=[9], name=EGTHDM, data=[11.9738, null, null, null, null, -0.844012], arraySizes=[6], name=SLOATL, data=[33.4591, null, null, null, null, 29.7589], arraySizes=[6], name=IAIE, data=[0, 0, 0, 0, 0, 0, 0, 0, 0], arraySizes=[9], name=GWFM, data=[null, 4.94427, 4.93224, 7.15636, 5.84145, null, 5.81955, 5.8618, 5.92097], arraySizes=[9], name=IB, data=[0, 1, 1, 1, 1, 0, 1, 1, 1], arraySizes=[9], name=SATSLF, data=[53.6306, null, null, null, null, 73.9836], arraySizes=[6], name=PCN12, data=[103.357, 96.3, 93.3, 92.6, 92.8, 111.38, 93, 90.4, 89.9], arraySizes=[9], name=BRAT, data=[0, 1, 1, 1, 1, 0, 1, 1, 1], arraySizes=[9], name=TATSL, data=[16.6171, null, null, null, null, 28.2083], arraySizes=[6], name=IAIWG, data=[0, 0, 0, 0, 0, 0, 0, 0, 0], arraySizes=[9], name=DTAMB, data=[-2.98298, null, null, null, null, 8.32425], arraySizes=[6], name=YN1MOD, data=[4, 4, 4, 4, 4, 4, 4, 4, 4], arraySizes=[9], name=WBNOM, data=[0, 0, 0, 0, 0, 0, 0, 0, 0], arraySizes=[9], name=VSVNOM, data=[null, 0, 0, 0, 0, null, 0, 0, 0], arraySizes=[9], name=flight_report_id, data=[556612329, 556614998, 556624200, 556636413, 556647711, 556563236, 556565278, 556574200, 556586659], arraySizes=[9], name=DELFN, data=[10.1342, null, null, null, null, -0.525884], arraySizes=[6], name=SATSL, data=[12.017, null, null, null, null, 23.3242], arraySizes=[6], name=DEGT, data=[null, -15.2332, -14.9595, -4.57673, -5.50067, null, -17.8259, -9.75815, -8.17455], arraySizes=[9], name=PCN1K, data=[103.526, 101.35, 99.541, 99.5841, 99.0076, 109.264, 99.0255, 97.4138, 96.875], arraySizes=[9], name=TATSLF, data=[61.9108, null, null, null, null, 82.7749], arraySizes=[6], name=GEGTMC, data=[null, 42.2332, 41.9595, 31.5767, 32.5007, null, 44.8259, 36.7582, 35.1745], arraySizes=[9], name=PCN12I, data=[104.3, 96.3, 93.3, 92.6, 92.8, 112.5, 93, 90.4, 89.9], arraySizes=[9], name=GPCN25, data=[null, 2.63531, 2.10201, 2.24873, 2.00089, null, 2.09637, 1.89297, 1.91071], arraySizes=[9], name=processed_date_time, data=[2020-06-05T08:29:22, 2020-06-05T08:53:54, 2020-06-05T10:56:41, 2020-06-05T12:59:12, 2020-06-05T14:58:32, 2020-06-04T22:37:38, 2020-06-04T23:06:44, 2020-06-05T01:08:04, 2020-06-05T03:10:15], arraySizes=[9]]
因此,timeSeriesRecords存储为格式错误的JSON-如上所示。我需要访问内部记录,名称,数据等,所有这些现在都为格式错误的JSON。我本打算添加另一个POJO,然后将其放入其中,但是我不能,因为它是格式错误的JSON。我也需要把它放在POJO中。
因此,我在JSON POJO中创建了其他类。因此,我将POJO更改为此...添加了内部类以捕获所需的香料。
public class MyPojo
private static final long serialVersionUID = 202003211122L;
public MyPojo()
private String httpStatus;
Payload PayloadObject;
ArrayList<Object> errorMessages = new ArrayList<Object>();
// Getter Methods
public String getHttpStatus()
return httpStatus;
public Payload getPayload()
return PayloadObject;
// Setter Methods
public void setHttpStatus( String httpStatus )
this.httpStatus = httpStatus;
public void setPayload( Payload payloadObject )
this.PayloadObject = payloadObject;
public class Data
@JsonProperty("name")
private String name;
@JsonProperty("arraySizes")
private ArrayList<Integer> arraySizes = new ArrayList<Integer>();
@JsonProperty("data")
private ArrayList<String> data = new ArrayList<String>();
public Data()
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public Data(@JsonProperty("name") String name,
@JsonProperty("arraySizes") ArrayList<Integer> arraySizes,
@JsonProperty("data") ArrayList<String> data)
this.name = name;
this.arraySizes = arraySizes;
this.data = data;
public void setName(String name)
this.name = name;
public String getName()
return this.name;
public void setArraySizes(ArrayList<Integer> arraySizes)
this.arraySizes = arraySizes;
public List<Integer> getArraySizes()
return this.arraySizes;
public void setData(ArrayList<String> data)
this.data = data;
public List<String> getData()
return this.data;
public class TimeSeriesRecords
@JsonProperty("name")
private String name;
@JsonProperty("timestamps")
private ArrayList<String> timestamps = new ArrayList<String>();
@JsonProperty("data")
private ArrayList<Data> data = new ArrayList<Data>();
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public TimeSeriesRecords(@JsonProperty("name") String name,
@JsonProperty("timestamps") ArrayList<String> timestamps,
@JsonProperty("data") ArrayList<Data> data)
this.name = name;
this.timestamps = timestamps;
this.data = data;
public void setName(String name)
this.name = name;
public String getName()
return this.name;
public void setTimestamps(ArrayList<String> timestamps)
this.timestamps = timestamps;
public List<String> getTimestamps()
return this.timestamps;
public void setData(ArrayList<Data> data)
this.data = data;
public List<Data> getData()
return this.data;
public class Payload
ArrayList<Object> internalRecords = new ArrayList<Object>();
ArrayList<Object> records = new ArrayList<Object>();
ArrayList<TimeSeriesRecords> timeSeriesRecords = new ArrayList<TimeSeriesRecords>();
public ArrayList<TimeSeriesRecords> getTimeSeriesRecords()
return timeSeriesRecords;
public void setTimeSeriesRecords(ArrayList<TimeSeriesRecords> timeSeriesRecords)
this.timeSeriesRecords = timeSeriesRecords;
public ArrayList<Object> getInternalRecords()
return internalRecords;
public ArrayList<Object> getRecords()
return records;
public void setInternalRecords(ArrayList<Object> internalRecords)
this.internalRecords = internalRecords;
public void setRecords(ArrayList<Object> records)
this.records = records;
但是现在我得到了错误
Caused by: java.lang.IllegalArgumentException: Unrecognized Type: [null]
at com.fasterxml.jackson.databind.type.TypeFactory._constructType(TypeFactory.java:517)[123:com.fasterxml.jackson.core.jackson-databind:2.6.3]
at com.fasterxml.jackson.databind.type.TypeFactory.constructType(TypeFactory.java:470)[123:com.fasterxml.jackson.core.jackson-databind:2.6.3]
at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory.constructCreatorProperty(BasicDeserializerFactory.java:842)[123:com.fasterxml.jackson.core.jackson-databind:2.6.3]
我已经尝试了几个较小的变化,但是,我无法克服此错误。
请告诉我我在做什么错。谢谢!
TEST JSON
"httpStatus": "OK",
"payload":
"internalRecords": [],
"records": [
"name": "aircraft_id",
"arraySizes": [
412
],
"data": [
"3848",
]
,
"name": "engine_family",
"arraySizes": [
412
],
"data": [
"CFM56-7",
]
,
"name": "engine_type",
"arraySizes": [
412
],
"data": [
"CFM56-7B27E/B1F",
]
,
"name": "aircraft_type",
"arraySizes": [
412
],
"data": [
"B737-900ER",
]
],
"timeSeriesRecords": [
"name": "31340D",
"timestamps": [
"2018-12-17T19:37:18",
"2018-12-17T19:37:18",
"2018-12-17T20:13:18",
"2018-12-18T14:38:43"
],
"data": [
"name": "flight_phase",
"arraySizes": [
15
],
"data": [
"TAKEOFF",
"TAKEOFF",
"CRUISE",
"CRUISE",
"TAKEOFF",
"TAKEOFF",
"CRUISE",
"CRUISE",
"TAKEOFF",
"TAKEOFF",
"CRUISE",
"CRUISE",
"TAKEOFF",
"TAKEOFF",
"CRUISE"
]
,
"name": "message_date_time",
"arraySizes": [
15
],
"data": [
"2018-12-17T19:37:18",
"2018-12-17T19:37:18",
"2018-12-17T20:13:18",
"2018-12-17T22:30:36"
]
,
"name": "IAIE",
"arraySizes": [
15
],
"data": [
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0"
]
,
"name": "IB",
"arraySizes": [
15
],
"data": [
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1"
]
,
"name": "eposition",
"arraySizes": [
15
],
"data": [
1,
2,
1,
2,
1,
2,
1,
2,
1,
2,
1,
2,
1,
2,
2
]
,
"name": "IAIWG",
"arraySizes": [
15
],
"data": [
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0"
]
,
"name": "flight_report_id",
"arraySizes": [
15
],
"data": [
"389660813",
"389660814",
"389667535",
"389668530",
"389700831"
]
,
"name": "engine_serial_number",
"arraySizes": [
15
],
"data": [
"802222",
"802333",
]
,
"name": "processed_date_time",
"arraySizes": [
15
],
"data": [
"2018-12-17T19:38:04",
"2018-12-17T19:38:49",
"2018-12-17T20:15:22",
"2018-12-17T20:16:17",
"2018-12-17T22:03:29",
"2018-12-18T14:51:31"
]
]
]
,
"errorMessages": []
你好,好的,我找到了此链接
http://www.cowtowncoder.com/blog/archives/2010/08/entry_411.html
我将内部类更改为静态,现在不再出现错误,造成原因:java.lang.IllegalArgumentException:无法识别的类型:[null]
但是现在我感觉更是个白痴,我要重新打开书了:)-我不知道如何从另一个类访问静态类?我无法访问有效载荷类
到目前为止已经尝试过,
AviationEntities4 ae =
exchange.getIn().getBody(AviationEntities4.class);
Payload pl = ae.getPayload();
String ok = ae.getHttpStatus();
log.info("HttpStatus: " + ok);
// AviationEntities4 payload = ae.getPayload();
// AviationEntities4.Payload payload = ae.getPayload();
// Payload pl = ae.getPayload();
非常感谢您抽出宝贵时间解决我的问题。
此链接,文章,信息已解决了我的问题,并对内部类和Jackson有了更好的理解。
http://www.cowtowncoder.com/blog/archives/2010/08/entry_411.html
2种方法,
- 使内部类变为静态
- 将内部类放入自己的类中
它使用静态方法工作,并且将内部类作为自己的类工作,然后进行了扩展。所以我选择了#2。它按预期工作。
谢谢你COWTOWNCODER !!!
以上是关于我需要在POJO中反序列化具有多个类的复杂JSON的主要内容,如果未能解决你的问题,请参考以下文章
使用 Newtonsoft Json 从流中反序列化多个 json 对象