-1

有谁知道仅使用 es5 重新格式化下面的 JSON 代码的最佳方法是什么?我没有做太多的 json 格式化,这就是为什么开放征求建议。

{
  "success": true,
  "results": [
    {
      "id": "12",
      "base": "263422"
 },
    {
      "id": "15",
      "base": "223322"
 }
}

至:

{
    "success": true,
    "results": [
        {
            "id": 29,
            "bill": [
                {
                    "id": 29,
                    "base": 124122,
                }
            ]
        },
        {
            "id": 33,
            "bill": [
                {
                    "id": 33,
                    "base": 12412232
                }
            ]
        }
    }
4

1 回答 1

0

类似的东西应该可以工作(假设您只是想重新格式化结构而不是数据本身):

var json = {
    "success": true,
    "results": [
        {
            "id": "12",
            "base": "263422"
        },
        {
            "id": "15",
            "base": "223322"
        }
    ]
};

for(var i = 0; i < json.results.length; i++) {
    json.results[i].bill = [
        {
            id: json.results[i].id,
            base: json.results[i].base
        }
    ];

    delete json.results[i].base;
}

console.log(JSON.stringify(json, null, 4));

于 2019-01-05T00:22:13.313 回答