[英]Java Parse JSON with Jackson
I try to parse JSON below with java Jackson library but I not able to get the value at level "ChannelType", "Address" and etc. It is because of value "endpoint123" & "endppoint456", which cause some problem for me. 我尝试用谷歌搜索,但在其他地方找不到。 希望有 1 位专家在这里提供帮助。 提前致谢。
{
"Message": {},
"ApplicationId": "aaa",
"CampaignId": "bbb",
"TreatmentId": "0",
"ActivityId": "ccc",
"ScheduledTime": "2021-04-07T08:43:16.406Z",
"Endpoints": {
"endpointa123": {
"ChannelType": "CUSTOM",
"Address": "bobby@abc.com.my",
"EndpointStatus": "ACTIVE",
"OptOut": "NONE",
"EffectiveDate": "2021-04-07T08:35:48.796Z",
"User": {
"UserAttributes": {
"Custom4": [
"data4"
],
"Custom3": [
"data3"
],
"Custom2": [
"data2"
],
"Custom1": [
"data1"
],
"Custom5": [
"data5"
]
}
},
"CreationDate": "2021-04-07T08:35:48.796Z"
},
"endpoint456": {
"ChannelType": "CUSTOM",
"Address": "fifis@abc.com.my",
"EndpointStatus": "ACTIVE",
"OptOut": "NONE",
"EffectiveDate": "2021-04-07T08:35:48.792Z",
"User": {
"UserAttributes": {
"Custom4": [
"data4"
],
"Custom3": [
"data3"
],
"Custom2": [
"data2"
],
"Custom1": [
"data1"
],
"Custom5": [
"data5"
]
}
},
"CreationDate": "2021-04-07T08:35:48.792Z"
}
}
}
我的代码如下所示,Campaign 是我自己定义的 class。
Campaign campaign = mapper.readValue(json, Campaign.class);
String valueOut = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(campaign);
System.out.println(valueOut);
我的 Java Object Model 如下所示。
Campaign
|--EndPoints
|-- EndPointsContainer (Array)
|--User
|--UserAttributes
以下是我得到的结果。
"Message" : { },
"ApplicationId" : "aaa",
"CampaignId" : "bbb",
"TreatmentId" : "0",
"ActivityId" : "ccc",
"ScheduledTime" : "2021-04-07T10:44:27.598Z",
"EndPoints" : {
"endPointsContainer" : [ ],
"creationDate" : null,
"CreationDate" : null
感谢@罗伯特。 它至少与原始 JSON 非常相似。 POJO 是按如下方式重构的。
Campaign
|--EndPoints
|--User
|--UserAttributes
但是,CreationDate 位于 object“endpoint123”和“endpoint456”内部,而不是外部。 因为它很长。 我贴了一部分。
"ScheduledTime": "2021-04-07T08:43:16.406Z",
"Endpoints": {
"endpointa123": {
"CreationDate": "2021-04-07T08:35:48.796Z"
"ChannelType": "CUSTOM",
这里有两个问题,
enpoints
是一个数组并且具有变量名称endPointsContainer
,则您需要具有相同的名称。 endpointa123
和endpoint456
将被视为不同的对象,不会成为单个列表的一部分CreationDate
是端点的一部分,因此它将在endPointsContainer
中可用因此,按照您的期望,正确的 JSON 将是,
{
"Message": {},
"ApplicationId": "aaa",
"CampaignId": "bbb",
"TreatmentId": "0",
"ActivityId": "ccc",
"ScheduledTime": "2021-04-07T08:43:16.406Z",
"Endpoints": {
"endPointsContainer": [
{
"endpoint": {
"ChannelType": "CUSTOM",
"Address": "bobby@abc.com.my",
"EndpointStatus": "ACTIVE",
"OptOut": "NONE",
"EffectiveDate": "2021-04-07T08:35:48.796Z",
"User": {
"UserAttributes": {
"Custom4": [
"data4"
],
"Custom3": [
"data3"
],
"Custom2": [
"data2"
],
"Custom1": [
"data1"
],
"Custom5": [
"data5"
]
}
},
"CreationDate": "2021-04-07T08:35:48.796Z"
}
},
{
"endpoint": {
"ChannelType": "CUSTOM",
"Address": "fifis@abc.com.my",
"EndpointStatus": "ACTIVE",
"OptOut": "NONE",
"EffectiveDate": "2021-04-07T08:35:48.792Z",
"User": {
"UserAttributes": {
"Custom4": [
"data4"
],
"Custom3": [
"data3"
],
"Custom2": [
"data2"
],
"Custom1": [
"data1"
],
"Custom5": [
"data5"
]
}
},
"CreationDate": "2021-04-07T08:35:48.792Z"
}
}
]
}
}
当我将其反序列化为 MAP class 时,
ObjectMapper jsonMapper = new ObjectMapper();
Map<String,Object> compaign = jsonMapper.readValue(json, Map.class);
这就是它的样子,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.