How to store and retrieve JSON data into local storage?

后端 未结 5 934
迷失自我
迷失自我 2020-12-28 09:28

I have this code:

var string = \'{\"items\":[{\"Desc\":\"Item1\"},{\"Desc\":\"Item2\"}]}\';
localStorage.setItem(\'added-items\', JSON.stringify(string));
         


        
5条回答
  •  猫巷女王i
    2020-12-28 10:09

    var object = Json.parse(retrievedObject);
    

    Now you can access it just like an array

    https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

    If you need more help i have some previous code where i am reading Json from local storage and making a form from that json. This code will help in understanding how to traverse that array

    Json stored in localstorage

    {"form":[{"element":"input", "type":"text","name":"name","value":"value","min":"2","max":"10"}]}

    JavaScript to read that json

    function readJson(){
        if(!form_created){
            add_form();
        }
        var fetched_json = localStorage.getItem("json");
        var obj=JSON.parse(fetched_json);
        for(var i=0; i

提交回复
热议问题