简体   繁体   中英

How to reformat JSON code using only es5 .

Does anyone know what is the best way to reformat JSON code below using only es5 ?I haven`t done much json formatting, thats why open for suggestions.

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

to:

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

Something akin to this should work (provided you are simply looking to reformat the structure and not the data itself):

 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM