简体   繁体   中英

How to print object data in Javascript using sessionStorage concepts in HTML5

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.

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"));

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)                                                                                                                                                                                                                        

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'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM