在 java 中,我试图将嵌套 JSON 转换为平面 JSON。下面是输入有效负载。
输入有效载荷:
{
"root": {
"data": {
"a": "1",
"b": {
"c": "2"
},
"test": [
{
"e": "3"
},
{
"f": "4"
}
],
"test1": [
{
"g": "5"
}
],
"test2": [
1,
2,
3
]
}
}
}
从上面的有效负载中,我试图转换为以下扁平化的 JSON。
{
"root.data.a":"1",
"root.data.b.c":"2",
"root.data.test[]":[
{
"e": "3"
},
{
"f": "4"
}
],
"root.data.test1[].g":"5",
"root.data.test2":[1,2,3]
}
在上述 JSON 中,转换为平面 JSON 时使用了以下条件。
如果 JSONObject 包含嵌套的 JSONObject 和值作为除 JSONArray 之外的任何数据类型,则平面 JSON 的键和值必须
"root.data.a"为 1,值必须为 1。如果 JSONObject 由 JSONArray 组成,并且 JSONArray 的长度大于 1,则平面 JSON 的键必须是
"root.data.test[]"完整的 JSONArray,值必须是完整的 JSONArray。如果 JSONObject 由 JSONArray 组成,并且 JSONArray 的长度为 1,则平面 JSON 必须 key 必须由 JSON 数组中的所有 JSON object 元素组成,
"root.data.test1[].g"并且 value 为“5”。如果 JSON 键的值为 Array 且没有任何 JSON 对象,则将值分配给平面 JSON 中的该路径,如键 as
"root.data.test2"和值 as[1,2,3]
那么,如何从输入 JSON 转换为平面 JSON 。