我想在使用javascript获取后更改json格式

时间:2017-10-18 04:31:15

标签: javascript json

  1. 我有一个类似下面的json ..在获取这个json后我存储在一个名为data的变量中。我需要将此数据变量更改为如下所示

      {
    "recipes": [
    {
    "cuisine": "chinese",
    "description": "hjkhdkd",
    "id": 3,
    "min_time": "30-60min",
    "name": "Noodles",
    "nutrition": "",
    "qty": "",
    "quantity_unit_id": "2",
    "rate": "",
    "recipe_type": "vegetarian",
    "serve": [
    "4"
    ],
    "tip": "",
    "image_url": {
    "medium": "/system/recipes/images/000/000/003/medium/dfdfs.jpeg?1501851554"
    }
    }
    ]
    }
    
  2. 如何使用javascript

    更改上面的json数据
    [
    {
    "cuisine": "chinese",
    "description": "hjkhdkd",
    "id": 3,
    "min_time": "30-60min",
    "name": "Noodles",
    "nutrition": "",
    "qty": "",
    "quantity_unit_id": "2",
    "rate": "",
    "recipe_type": "vegetarian",
    "serve": [
    "4"
    ],
    "tip": "",
    "image_url": {
    "medium": "/system/recipes/images/000/000/003/medium/dfdfs.jpeg?1501851554"
    }
    }
    ]
    

1 个答案:

答案 0 :(得分:1)

var data = '{ "recipes": [ { "cuisine": "chinese", "description": "hjkhdkd", "id": 3, "min_time": "30-60min", "name": "Noodles", "nutrition": "", "qty": "", "quantity_unit_id": "2", "rate": "", "recipe_type": "vegetarian", "serve": [ "4" ], "tip": "", "image_url": { "medium": "/system/recipes/images/000/000/003/medium/dfdfs.jpeg?1501851554" } } ] }';
var jsonifieddata = JSON.parse(data);
var newdata = jsonifieddata.recipes;
JSON.stringify(newdata);

在您的控制台中运行此功能。

建议:分享您在再次询问stackoverflow时所做的一些努力。