How to write data to a JSON file using Javascript

前端 未结 3 668
天命终不由人
天命终不由人 2020-11-28 06:22

For example, I have a .JSON file that has the following:

[{\"honda\": \"accord\", \"color\": \"red\"},{\"ford\": \"focus\", \"color\": \"black\"         


        
3条回答
  •  迷失自我
    2020-11-28 07:02

    JSON can be written into local storage using the JSON.stringify to serialize a JS object. You cannot write to a JSON file using only JS. Only cookies or local storage

        var obj = {"nissan": "sentra", "color": "green"};
    
    localStorage.setItem('myStorage', JSON.stringify(obj));
    

    And to retrieve the object later

    var obj = JSON.parse(localStorage.getItem('myStorage'));
    

提交回复
热议问题