Export a Json object to a text File

前端 未结 3 1543
逝去的感伤
逝去的感伤 2021-02-02 18:09

I\'m trying to write a Json object (JsonExport) and I\'d like to write its content into a text file.

I\'m using max4live to export data from Audio DAW to Json in order t

3条回答
  •  迷失自我
    2021-02-02 18:28

    I know this question already has accepted answer but I think my answer could help someone. So, the problem is to export Json data to a text file. Once you execute the following code, file will be downloaded by the browser.

    const filename = 'data.json';
    const jsonStr = JSON.stringify(JsonExport);
    
    let element = document.createElement('a');
    element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(jsonStr));
    element.setAttribute('download', filename);
    
    element.style.display = 'none';
    document.body.appendChild(element);
    
    element.click();
    
    document.body.removeChild(element);
    

提交回复
热议问题