我想使用 Java 来接收 JSON 正文,遍历记录,并将所有其他记录输出到新的 JSON 数组中。每组两条记录都是它自己的数组,我只需要在每个数组中取第一个。我还将在原始请求中提供需要添加到后续展平对象的列名。例如,假设我有以下 JSON 请求正文:
{
"records": [
[
[
"0DFC29E2-700E-4CC1-931E-B61DF4954B6B",
"John Doe",
"Teacher",
"China"
],
[
"B5B9186E-CE65-4911-8516-C510D3CC3ACE",
"Jane Doe",
"Doctor",
"London"
]
],
[
[
"20C4DD07-4E96-47F8-A1E1-B20B4C48120C",
"Jim Doe",
"Lawyer",
"Canada"
],
[
"76718CB1-238F-418E-BD14-5E2867FF3FB4",
"Jack Doe",
"Chef",
"Mexico"
]
]
],
"columns": [
"ID",
"Name",
"Occupation",
"Location"
]
}
然后,我希望将此请求正文扁平化为以下内容:
[{
"ID": "0DFC29E2-700E-4CC1-931E-B61DF4954B6B",
"Name": "John Doe",
"Occupation": "Teacher",
"Location": "China"
},
{
"ID": "20C4DD07-4E96-47F8-A1E1-B20B4C48120C",
"Name": "Jim Doe",
"Occupation": "Lawyer",
"Location": "Canada"
}]
我希望这段代码非常动态,因此它不会显式引用代码中的列名。这样,如果我有不同的 JSON 正文结构,我将来可以传递其他列名,它会相应地工作。我将始终以“记录”为标题传递数据,因此可以进行硬编码。任何帮助是极大的赞赏。