[英]Convert JSON from one format to another JSON format in java
I have to covert a specific JSON response and covert to some other format 我必须隐瞒特定的JSON响应,并隐瞒其他某种格式
For Eg My input JSON is like 例如,我的输入JSON就像
JSON1 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
}
]
I need to replace some values for roomId and then convert json to below format and send it to some other endpoint 我需要替换roomId的一些值,然后将json转换为以下格式并将其发送到其他端点
JSON2 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"
}
]
}
I tried doing this in couple of ways like Deserialization using jackson and mapping it to specific class like below 我尝试以几种方式执行此操作,例如使用杰克逊进行反序列化并将其映射到如下所示的特定类
@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;
}
and then again serialising it using a different class 然后再次使用不同的类对其进行序列化
@JsonSerialize(using = CustomSerializer.class)
public class FinalDataModel {
public int roomId;
public String startDate;
public String endDate;
public int priority;
public String message;
}
But somehow the conversion is not working. 但是以某种方式,转换无法正常进行。 Do we have any better way to achieve this?
我们是否有更好的方法来实现这一目标?
You can achieve that by using JSONObject and JSONArray 您可以通过使用JSONObject和JSONArray来实现
//Create a JSON array with your json string
JSONArray array = new JSONArray(jsonString);
JSONArray newArray = new JSONArray();
//loop on your JSONArray elements
for (i=0, i<array.size(),i++) {
//Get each separate object into a JSONObject
JSONObject obj = array.getJSONObject(i);
//let say you want to modify id for example
obj.put("id", 12345);
//push the modifyed object into your new array
newArray.put(obj);
}
//transform the array into a string
String json = newArray.toString();
Use Jolt Convertor. 使用震动转换器。 Simplest, and esiest way to convert any JSON to any other JSON format.
将任何JSON转换为任何其他JSON格式的最简单,最简便的方法。
https://jolt-demo.appspot.com/#inception https://jolt-demo.appspot.com/#inception
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.