我必须隐藏特定的 JSON 响应并隐藏为其他格式
对于例如我的输入 JSON 就像
JSON1
[
{
"propertyId":10081,
"roomId":0,
"startDate":"2018-01-29T00:00:00",
"endDate":"2018-01-30T00:00:00",
"priority":1,
"message":"Test it",
"id":158,
"dateModifiedUtc":null
},
{
"propertyId":10081,
"roomId":10021855,
"startDate":"2018-01-29T00:00:00",
"endDate":"2018-01-30T00:00:00",
"priority":2,
"message":"Check how it works",
"id":159,
"dateModifiedUtc":null
},
{
"propertyId":10081,
"roomId":10021855,
"startDate":"2018-01-29T00:00:00",
"endDate":"2018-01-30T00:00:00",
"priority":2,
"message":"Check how it works",
"id":160,
"dateModifiedUtc":null
}
]
我需要替换 roomId 的一些值,然后将 json 转换为以下格式并将其发送到其他端点
JSON2
{
"sitenotification":[
{
"roomid":"10601",
"priority":1,
"startdate":"2017-08-10T15:50:52+03:00",
"enddate":"2017-08-15T15:50:52+03:00",
"sitemessage":"test"
},
{
"roomid":"10601",
"priority":1,
"startdate":"2017-08-10T15:50:52+03:00",
"enddate":"2017-08-15T15:50:52+03:00",
"customermessage":"test 2"
}
]
}
我尝试通过多种方式执行此操作,例如使用 jackson 进行反序列化并将其映射到如下所示的特定类
@JsonIgnoreProperties(ignoreUnknown = true)
public class FatNotificationsAttributesDataModel {
@JsonProperty("propertyId")
public int propertyId;
@JsonProperty("roomId")
public int roomId;
@JsonProperty("startDate")
public String startDate;
@JsonProperty("endDate")
public String endDate;
@JsonProperty("priority")
public int priority;
@JsonProperty("message")
public String message;
@JsonProperty("id")
public int id;
@JsonProperty("dateModifiedUtc")
public String dateModifiedUtc;
}
然后再次使用不同的类对其进行序列化
@JsonSerialize(using = CustomSerializer.class)
public class FinalDataModel {
public int roomId;
public String startDate;
public String endDate;
public int priority;
public String message;
}
但不知何故,转换不起作用。我们有没有更好的方法来实现这一目标?
最佳答案
使用震动转换器。将任何 JSON 转换为任何其他 JSON 格式的最简单、最简单的方法。
关于java - 在java中将JSON从一种格式转换为另一种JSON格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49653934/