Groovy - 重新格式化 JSON

Groovy - reformatting JSON

我有一个 json 结构,看起来像这样

  {
   "Fields":[
      "FieldName1",
      "FieldName2",
      "FieldName3",
      "FieldName4",
      "FieldName5"
   ],
   "Rows":[
      {
         "Values":[
            "A",
            "B",
            "C",
            "D",
            "E"
         ]
      },
      {
         "Values":[
            "F",
            "G",
            "H",
            "I",
            "J"
         ]
      },
      {
         "Values":[
            "K",
            "L",
            "M",
            "N",
            "O"
         ]
      }
   ]
}

字段名和字段数不固定。 我正在尝试重新格式化它,因为

{
   "values":[
      {
         "FieldName1":"A",
         "FieldName2":"B",
         "FieldName3":"C",
         "FieldName4":"D"
      },
      {
         "FieldName1":"E",
         "FieldName2":"F",
         "FieldName3":"G",
         "FieldName4":"H"
      },
      {
         "FieldName1":"I",
         "FieldName2":"J",
         "FieldName3":"K",
         "FieldName4":"L"
      }
   ]
}

关于如何以重新格式化的方式执行此操作的任何想法,无论字段数量和字段名称如何?

提前致谢

史蒂夫

您可以创建一个包含字段名称和索引的映射,然后按索引为每个值添加字段名称

像这样

        def input = new JsonSlurper().parseText(TEXT)
        def fieldNamesByIndex = input["Fields"].indexed()
        def values = input["Rows"].collect { it["Values"] }.collect { values ->
            // values.withIndex creates a list of Tuple<String, Integer>, where string is the value and integer is the index
            return values.withIndex().collectEntries { [(fieldNamesByIndex[it.second]): it.first] }
        }

        def output = ["values": values]
        println(JsonOutput.toJson(output))

给猫剥皮的较短版本:

import groovy.json.*

def map = [
   "Fields":[
      "FieldName1",
      "FieldName2",
      "FieldName3",
      "FieldName4",
      "FieldName5"
   ],
   "Rows":[
      [
         "Values":[
            "A",
            "B",
            "C",
            "D",
            "E"
         ]
      ],
      [
         "Values":[
            "F",
            "G",
            "H",
            "I",
            "J"
         ]
      ],
      [
         "Values":[
            "K",
            "L",
            "M",
            "N",
            "O"
         ]
      ]
   ]
]

def values = map.Rows*.Values*.withIndex()*.collectEntries{ v, ix -> [ map.Fields[ ix ], v ] }
def res = [ values:values ]

println JsonOutput.prettyPrint( JsonOutput.toJson( res ) )

打印

{
    "values": [
        {
            "FieldName1": "A",
            "FieldName2": "B",
            "FieldName3": "C",
            "FieldName4": "D",
            "FieldName5": "E"
        },
        {
            "FieldName1": "F",
            "FieldName2": "G",
            "FieldName3": "H",
            "FieldName4": "I",
            "FieldName5": "J"
        },
        {
            "FieldName1": "K",
            "FieldName2": "L",
            "FieldName3": "M",
            "FieldName4": "N",
            "FieldName5": "O"
        }
    ]
}

可以肯定地说,您得到了 JSON 的 loading/writing 部分 从您的评论中删除。

从键列表和值列表获取映射的最简单方法 通过 [keys,values].transpose().collectEntries()。所以你会有 迭代 Rows 并执行此操作。对于结果将其放入地图中 在 values.

键下
[values: data.Rows.collect{ [data.Fields, it.Values].transpose().collectEntries() }]