简体   繁体   English

如何使用HTML5中的sessionStorage概念在Javascript中打印对象数据

[英]How to print object data in Javascript using sessionStorage concepts in HTML5

I am framing a JSON object in Script tag in HTML screen - 我正在HTML屏幕的Script标签中构建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'},]}]}} var ThroughElements = {“ 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); 我们已将JSON对象保存为sessionStorage作为sessionStorage.setItem(“ sessiondata”,PassingElements);。

When we are trying to retrieve the stored data as sessionStorage.getItem("sessiondata"); 当我们尝试以sessionStorage.getItem(“ sessiondata”)的形式检索存储的数据时; // Printing as "[object Object]" //打印为“ [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. 我们正在研究Titanium Appcelerator工具。

Thanks, Rakesh Kalwa. 谢谢,Rakesh Kalwa。

Be aware that localStorage or sessionStorage use only strings . 请注意,localStorage或sessionStorage 使用字符串 Objects are not allowed! 禁止对象!

But you can serialize any non circular object with JSON : 但是您可以使用JSON序列化任何非圆形对象:

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

Your JSON 您的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. 要将JSON对象存储在本地存储中,您需要使用JSON.stringify()函数将其转换为JSON格式的字符串。

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. 由于该对象先前已转换为JSON格式的字符串,因此必须先反转stringify函数的效果,然后才能访问该对象中的数据。 This is easily done through use of the JSON.parse() function 通过使用JSON.parse()函数可以轻松完成此操作

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

Try to make a string of the passingElements object you have created. 尝试创建一个已创建的PassingElements对象的字符串。

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. 当您要访问数据时,应将其从字符串解析回Javascript对象。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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