Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to convert json data to demo.json and download files

Shulou Source: shulou.com Published: 2022-06-02 06:36:16 09月27日 Update

This article focuses on "how to convert json data into demo.json and download files". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to convert json data into demo.json and download files.

Json is treated as a string and can be downloaded using DataURL

Text-> DataURL

In addition to using DataURL, you can also convert it to Object URL for download

Text-> Blob-> Object URL

You can paste the following code directly into the console download file

Function download (url, name) {

Const a = document.createElement ('a')

A.download = name

A.rel = 'noopener'

A.href = url

/ / trigger simulation click

A.dispatchEvent (new MouseEvent ('click'))

/ / or a.click ()

}

Const json = {

A: 3

B: 4

C: 5

}

Const str = JSON.stringify (json, null, 2)

/ / solution 1: Text-> DataURL

Const dataUrl = `data:,$ {str} `

Download (dataUrl, 'demo.json')

/ / option 2: Text-> Blob-> ObjectURL

Const url = URL.createObjectURL (new Blob (str.split (')

Download (url, 'demo1.json') summary simulation download, you can create a new tag and set the url and download attributes to download can convert json to dataurl to construct URL can convert json to Blob and then convert to ObjectURL to construct URL to this, I believe that everyone on the "how to convert json data into demo.json and download files" have a deeper understanding, might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Files data can be passed content scheme learning practical deeper code interests characters strings practicality actual properties console simple operation methods more friends Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology OPPO Reno MySQL Huawei vpn