我怎样才能使 JSON 数据在 API 上可用?

How could I make this JSON data usable on an API?

JSON 数据当前看起来像这样的字符串:

["ID","姓名","年龄"],["212","大卫","38"]

我希望它看起来像这样:

{"ID":"212","姓名":"大卫","年龄":"38"}

提前感谢您的帮助

我找到了这段代码,它解决了大部分问题

var columns = ["ID", "Name", "Age"];
var rows = ["212", "David", "38"];
var result =  rows.reduce(function(result, field, index) {
  result[columns[index]] = field;
  return result;
}, {})

console.log(result);

您可以按照以下步骤进行操作:

  1. 从数组中提取键和值
  2. 将它们压缩到关键匹配值
  3. 使用Object.fromEntries创建对象键值
let obj = [["ID","Name","Age"],["212","David","38"]]
let [keys, values] = obj;
let zipped = keys.map((key, i)=>[key, values[i]]);
let output = Object.fromEntries(zipped);
console.log(output);

让我们说 让 jsonVal = [["ID","姓名","年龄"],["212","大卫","38"],["212","大卫","38"]] 第 0 个索引将有键,剩下的是数据

let newJsonVal = []  ​
 for (let i =1; i< jsonVal.length-1; i++) {
    ​let newObject ={}
     ​jsonVal[i].map((d,j) => { 
       ​newObject[jsonVal[0][j] = d;
      ​})
    newJsonVal.push(newObject)
 }

newJsonVal 将具有您需要的对象数组