0

我试图了解展平 Json 和取消展平 Json 到底是什么。任何链接或博客都会有所帮助。

另外还有一个问题,我正在尝试将扁平化的 json 属性反序列化为 objectmapper 对象。我正在尝试的 Json 格式如下。

{
  "MyUserID": "45345dfsf4545",
  "attributes": {
    "ArrayAttribute1[0].alertMessage": "You have consumed all of your data allowance",
    "ArrayAttribute1[0].promoName": "MyPromoTest",
    "ArrayAttribute2[0].showmorepromosbutton": "true",
    "ArrayAttribute1[0].promoPrice": "P 149.00",
    "userType": 1,
    "Attribute1": "Jan 28 2016 . 3:09PM",
    "Attribute1": "true",
    "Attribute2.validityColor": "RED",
    "Attribute2.subscriberBal": "P 29.5",
    }
}
4

2 回答 2

0
  • 扁平化意味着将 JSON 对象放入一个类似单层次结构的结构,也称为扁平结构,即没有子对象或父对象,一切都是键值对。嵌套对象可以通过点运算符访问。相反,Un-flattening 意味着将扁平化的结构放入存在父子结构的多层结构中。

  • 您需要将未展平的结构传递给 Object-mapper 以对其进行反序列化。

以下示例将有助于更好地理解它

嵌套 JSON 对象

{ "a":
  { "b": 1,
    "c": null,
    "d": [false, true]
  },
  "e": "f",
  "g": 2.3
}

扁平化的 JSON

{ "a.b": 1,
  "a.c": null,
  "a.d[0]": false,
  "a.d[1]": true,
  "e": "f",
  "g": 2.3
}

Java Map 像扁平化的 JSON

  {a.b=1, a.c=null, a.d[0]=false, a.d[1]=true, e=f, g=2.3}

为什么需要扁平化? 如果您没有 JSON 对象的内部结构,那么访问其中的所有元素将非常麻烦,因此简单的解决方案是将所有内容放在同一级别,并且对象的深度由点运算符描述。所以扁平化可以更容易地访问内部元素。

序列化和反序列化只能发生在未展平的 JSON 上。

于 2019-12-30T05:19:31.287 回答
0

找到这个存储库(https://github.com/wnameless/json-flattener)来展平和取消展平 json。确保在将其提供给 ObjectMapper 之前先将其展开为字符串。不幸的是,没有找到定义差异的规范。

于 2018-08-24T13:45:18.793 回答