java - 在java中将JSON从一种格式转换为另一种JSON格式

标签 java json objectmapper jackson-databind

我必须隐藏特定的 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 格式的最简单、最简单的方法。

https://jolt-demo.appspot.com/#inception

关于java - 在java中将JSON从一种格式转换为另一种JSON格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49653934/

相关文章:

java - 如何在另一个类中设置按钮属性和操作,并在 android 的 Activity 类中使​​用它们

java - 在 imageView 中调用图像破坏生成的图像

python - 将 JSONL 文件加载为 JSON 对象

php - Android php mysql 的 HTTP post 方法

javascript - 如何在谷歌图表中添加新的Y刻度

ios - Realm + Swift 使用 JSON 字符串更新对象

java - 将所有复选框(已选中)项收集到 Array 或 ArrayList 中

java - 为什么我的对象流被损坏?流损坏异常

java - JavaRDD<Status> 转换为 JavaRDD<String> 时出现问题

java - 以通用方式设置 ObjectMapper 属性?