1

I'm new in jquery mobile. I'm doing a shopping list application for my school assignment and I'm requested to store objects in local storage.

Each item must include the following information: item Name, item quantity and a Boolean is_bought. I wish to store all items data into one JSON string. Items data is entered by user from an other page.

My problem is 1)How items can be stored in local storage through JSON stringify.

2)How items data can be retrieved from JSON string to be represented into a list.

Stephen
  • 29
  • 4

1 Answers1

2

First: If I understood you right you have an object(json) structure like:

{
  "name": "cheese",
  "quantity": 2,
  "is_bought": false
}

If not (in question you have no key(name) for your variables) your structure must be like I showed for having an access to each variable in the object.

Second: About localStorage. It is limited to handle only string key/value pairs, so you can't just save an object in it. You have to use JSON.stringify() to parse your object into the string and save in localStorage, then, after retrieving, use JSON.parse() to parse it back. The code could look like this:

var item = {"name": "cheese", "quantity": 2, "is_bought": true};

// Store item into localStorage
localStorage.setItem('item', JSON.stringify(item));
// Retrieve item from localStorage
var retrievedItem = localStorage.getItem('item');
var parsedItem = JSON.parse(retrievedItem);

EDIT: TO STORE MULTIPLE ITEMS

So, if your question is about storing multiple items and distinguishing them, and if your item's name is unique and you know what item is bought, you can store them into the localStorage with the key of item's name, e.g.

// You can do this in a for loop
localStorage.setItem('item_' + item.name, JSON.stringify(item));

// And to change (if you already know bought item's name), 'cheese' for example
var retrievedItem = localStorage.getItem('item_cheese'); 
var parsedItem = JSON.parse(retrievedItem);
parsedItem.is_bought = true;
// And save again in localStorage
localStorage.setItem('item_cheese', JSON.stringify(parsedItem)); 
Serob_b
  • 965
  • 12
  • 29
  • What happens with your code if there's more than one item? :) – Alexey Soshin Dec 10 '16 at 14:57
  • Ohh.. Sorry. I misunderstood the question :) – Serob_b Dec 10 '16 at 15:02
  • Thanks very much for your feedback. However how can enter data in item object list dynamically? – Stephen Dec 11 '16 at 10:38
  • Sorry, I can not understand the exact question. Could you please describe it in more details? What I understood is that you have a list of item objects, right? If so, I've answered how to store the list in local storage and change some data (is_bought). But what you mean by saying 'dynamically' ? Do you want to change some data of object which is already in the list, or you want add some new object to the list? Or you want to add some new data do list's object which is not described beforehand (e.g. 4'th variable which is item's description - `item.description = "some description"`)? – Serob_b Dec 11 '16 at 13:42
  • Answer to these questions please and add more details to give a chance to community to answer. **It would be better if you edit your question and add details in it.** After editing write some comment for me to be notified ;) – Serob_b Dec 11 '16 at 13:43