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

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

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

问题

Sure, here's the translated code portion:

  1. 我有以下的 JSON 数据
  2. {
  3. "Id": "357",
  4. "Start": 76341,
  5. "long": 0,
  6. "data": "{bytesIn:120, byteOut:120}"
  7. }
  8. 我需要做的是将参数 "data" 中的值({bytesIn:120, byteOut:120}) 移动到与 "long" "start" 相同的层级输出应该如下所示的 JSON
  9. {
  10. "Id": "357",
  11. "Start": 76341,
  12. "long": 0,
  13. "bytesIn": 120,
  14. "byteOut": 120
  15. }
  16. 有没有在 Java 中使用 ObjectMapper 来实现这个操作的 "好方法"
英文:

I have following json.

  1. {
  2. "Id": "357",
  3. "Start": 76341,
  4. "long": 0,
  5. "data": "{bytesIn:120, byteOut:120}"
  6. },

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"

  1. {
  2. "Id": "357",
  3. "Start": 76341,
  4. "long": 0,
  5. "bytesIn": 120,
  6. "byteOut": 120
  7. },

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

答案1

得分: 3

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

  1. class YourType {
  2. @JsonUnwrapped
  3. private Data data;
  4. }
英文:

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

  1. class YourType {
  2. @JsonUnwrapped
  3. private Data data;
  4. }

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:

确定