如何将JSON数据保存到文件

时间:2015-01-28 09:45:06

标签: javascript

javascript中存在一个存储在变量中的JSON数据。我必须将它存储在名为.json的其他文件中。我可以这样做.Json数据是这样的:

{
  "method":"get",
  "class":"form-horizontal",
  "html":[{
              "type":"div",
              "class":"form-group",
              "id":"input_Name",
              "html":[{
                       "type":"label",
                       "class":"control-label col-sm-2",
                       "for":"input_Name",
                       "html":"Name"},
                      {
                       "type":"div",
                       "class":"col-sm-10",
                       "html":[{
                               "type":"text",
                               "class":"form-control",
                               "name":"input_Name",
                               "id":"input_Name",
                               "placeholder":"user@example.com"
                              }]
                      }]
        }]
}

1 个答案:

答案 0 :(得分:2)

使用saveAs(blob, filename)

var blob = new Blob([JSON.stringify(data)], {type: "text/plain;charset=utf-8"});
saveAs(blob, "data.json");

JSFiddle:http://jsfiddle.net/2wcew2xs/