【问题标题】:Convert JSON from one format to another JSON format in java在java中将JSON从一种格式转换为另一种JSON格式
【发布时间】:2018-04-04 14:40:47
【问题描述】:

我必须将特定的 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;

}

但不知何故,转换不起作用。我们有更好的方法来实现这一目标吗?

【问题讨论】:

  • 你能澄清什么不起作用吗?是否有任何异常或输出与您期望得到的不同?
  • 使用gson,它会让你的生活更轻松
  • 您的输入不一定是Json。它是Json 元素的数组。尝试反序列化为ArrayList<FatNotificationsAttributesDataModel>,看看是否可行。
  • 抱歉我的描述不够好。我可以通过使用类似 List attributes = node.findValues("attributes"); 的东西将 JSON1 反序列化为列表。 pojo = mapper.convertValue(属性, FatNotificationsAttributesDataModel[].class); .但我不确定如何从对象列表中获取 JSON2,我不太确定我遵循的方法

标签: java json objectmapper jackson-databind


【解决方案1】:

使用颠簸转换器。将任何 JSON 转换为任何其他 JSON 格式的最简单、最容易的方法。

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

【讨论】:

    【解决方案2】:

    你可以通过使用 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();
    

    【讨论】:

    • 我尝试了这个解决方案,但是当我们拥有更大的数据集时似乎遇到了性能问题
    • 显然是的,对于更大的数据集,可能有更好的解决方案/库,最终您可以使用某种多线程来获得更好的性能,但这取决于您。请记住,我给出的解决方案是基于您的要求。
    • 另外,老实说,一开始我认为这是为了教育目的,所以我在谈到 Java 中的 JSON 处理时给出了基本答案^^
    • 谢谢,我不需要解决方案,可能我只需要最好的方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 2015-04-23
    • 2021-09-22
    • 2018-01-20
    相关资源
    最近更新 更多