如何获取存储在localStorage中的对象的值? [英] How do I get an object's value stored in localStorage?
本文介绍了如何获取存储在localStorage中的对象的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已将具有多个属性的对象存储在localStorage中.它不包含单个值,例如字符串或数字. localStorage密钥的名称为UserData
,其值为以下对象:
I have stored an object with multiple properties in localStorage. It does not contain a single value like a string or a number. The name of the localStorage key is UserData
and its value is the following object:
{
key: "1287C31D714BE16FBD44D093E4173CFF"
logTime: "20191013190439"
operatorDni: "46653980"
}
我需要检索对象属性operatorDni
的值,以便在我的代码中执行一些操作.我尝试使用以下代码来检索它:
I need to retrieve the value of the object property operatorDni
in order to perform some actions in my code. I have tried to retrieve it using this line of code:
operatorDni: string;
this.operatorDni= localStorage.getItem('UserData.operatorDni');
但是我得到了null
.
如何使用键在localStorage
中获取对象的属性?我在做什么错了?
How can I get a property of an object in localStorage
with the key? What am I doing wrong?
非常感谢您.
推荐答案
使用JSON.parse
之类的
var userData= JSON.parse(localStorage.getItem('UserData'))
this.operatorDni=userData.operatorDni;
也喜欢保存
localStorage.setItem('UserData',JSON.stringify(yourObject))
这篇关于如何获取存储在localStorage中的对象的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文