0
votes

I am framing a JSON object in Script tag in HTML screen -

var passingElements = {"options":{"axisY":{"title":"Cups","titleFontSize":15,"labelFontColor":"#000000","labelFontSize":"10"},"axisX":{"labelFontColor":"#000000","labelFontSize":"10","gridColor":"orange"},"toolTip":{"enabled":false}, "data":[{"type":"column","indexLabel":"{x}","indexLabelFontColor":"#000000","dataPoints":[{y: 0.07, label:'3:09 A'},{y: 0.01, label:'1:58 A'},]}]}}

We have saved the JSON object to sessionStorage as sessionStorage.setItem("sessiondata", passingElements);

When we are trying to retrieve the stored data as sessionStorage.getItem("sessiondata"); // Printing as "[object Object]"

Please let me know how can i view the data or use the data which is stored in session storages.

We are working on Titanium Appcelerator tool.

Thanks, Rakesh Kalwa.

3

3 Answers

1
votes

Your JSON

var passingElements = {"options":{"axisY":{"title":"Cups","titleFontSize":15,"labelFontColor":"#000000","labelFontSize":"10"},"axisX":{"labelFontColor":"#000000","labelFontSize":"10","gridColor":"orange"},"toolTip":{"enabled":false}, "data":[{"type":"column","indexLabel":"{x}","indexLabelFontColor":"#000000","dataPoints":[{y: 0.07, label:'3:09 A'},{y: 0.01, label:'1:58 A'},]}]}}

To store a JSON object in local storage you will need to convert it into a JSON-formatted string, using the JSON.stringify() function.

sessionStorage.setItem("sessiondata", JSON.stringify(passingElements));

Because the object was previously converted to a JSON-formatted string, you will have to reverse the effects of the stringify function before you can access the data within the object. This is easily done through use of the JSON.parse() function

var obj = sessionStorage.getItem("sessiondata");      
obj = jQuery.parseJSON(obj); 
console.log(obj)                                                                                                                                                                                                                        
1
votes

Be aware that localStorage or sessionStorage use only strings. Objects are not allowed!

But you can serialize any non circular object with JSON:

sessionStorage.setItem("sessiondata", JSON.stringify(passingElements));
var data = JSON.parse(sessionStorage.getItem("sessiondata"));
0
votes

Try to make a string of the passingElements object you have created.

sessionStorage.setItem('sessiondata', JSON.stringify(passingElements));

When you want to access the data you should parse it back from a string into a Javascript Object.

JSON.parse(sessionStorage.getItem('sessiondata'));