如何从 JSON 属性中将值移至上一级(Java)。

huangapple go评论76阅读模式
英文:

How to move value from json property, one level up (java)

问题

Sure, here's the translated code portion:

我有以下的 JSON 数据

{
    "Id": "357",
    "Start": 76341,
    "long": 0,
    "data": "{bytesIn:120, byteOut:120}"
}

我需要做的是将参数 "data" 中的值({bytesIn:120, byteOut:120}) 移动到与 "long""start" 相同的层级输出应该如下所示的 JSON

{
    "Id": "357",
    "Start": 76341,
    "long": 0,
    "bytesIn": 120,
    "byteOut": 120
}

有没有在 Java 中使用 ObjectMapper 来实现这个操作的 "好方法"
英文:

I have following json.

    {
        "Id": "357",
        "Start": 76341,
        "long": 0,
        "data": "{bytesIn:120, byteOut:120}"
    },

What I need to do is to take the value({bytesIn:120, byteOut:120}) from parametr "data" and move to the same level like "long", "start". The output should looks like this json"

    {
        "Id": "357",
        "Start": 76341,
        "long": 0,
        "bytesIn": 120, 
        "byteOut": 120
    },

Is there any "nice way" to do this in java, using ObjectMapper?

答案1

得分: 3

我想你在使用Jackson。在数据字段上添加@JsonUnwrapped注解。

class YourType {
    @JsonUnwrapped
    private Data data;
}
英文:

I suppose you use Jackson. Add @JsonUnwrapped on data field

class YourType {
    @JsonUnwrapped
    private Data data;
}

huangapple
  • 本文由 发表于 2020年7月28日 19:32:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63133143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定