Trouble with JSON Data taken from localStorage in JavaScript/HTML5?
-
I am making a completely offline HTML5 web app. In a particular part of my code, I am required to add a row of data to a JavaScript Array Object and save the updated version back to localStorage. Here is the code: var data = localStorage.getItem("cars"); data.goals.push(["name": "merscede", "model": "bmw", "noofdays": "10", "active": "yes"]); //Trouble in this line. localStorage.setItem("goals",data); And this is the value I had stored in the localStorage in a different part of the source code: var data = '{"goals":[{"name":"rocky","model":"old","noofdays":"50","active":"no"}]}'; localStorage.setItem("goals",data); I have trobleshooted extensively and it turns out that only the senctence that I have particularly marked has all the trouble. What's the problem? What should I do?
-
Answer:
Here is the working version: var data = {"goals":[{"name":"rocky","model":"old","noofdays":"50","active":"no"}]}; localStorage.setItem("goals",JSON.stringify(data)); data = JSON.parse(localStorage.getItem("goals")); console.log(data); data.goals.push({"name": "merscede", "model": "bmw", "noofdays": "10", "active": "yes"}); localStorage.setItem("goals",JSON.stringify(data)); data = JSON.parse(localStorage.getItem("goals")); console.log(data);
Ranjan Kumar at Quora Visit the source
Related Q & A:
- How to set value in the dropdown from a JSON data list?Best solution by Stack Overflow
- How do I create an HTML table, in jQuery, with JSON data?Best solution by Stack Overflow
- How to store json data in jquery?Best solution by Stack Overflow
- How to read Json Data from online file?Best solution by mkyong.com
- How to deal with dynamic JSON data using AngularJS?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.