jQ:解析本地存储&字符串化值 [英] jQ: Parse localstorage & stringify values

查看:96
本文介绍了jQ:解析本地存储&字符串化值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 stringify localStorage 中存储一些值,我正在尝试解析它们但它没有'为我工作。

I am storing some values in localStorage using stringify and I'm trying to parse them but it doesn't work for me.

这是我添加价值的方式:

This is how I add the values:

localStorage.setItem('a', JSON.stringify({ userid : '4361', value : '23' }));

这就是我解析它们的方式:

And this is how I parse them:

$('p').text(JSON.parse(localStorage.getItem('a')));

这是小提琴: http://jsfiddle.net/hrHfG/

另外,我想知道如何分别解析每个值。例如,只有'a'的用户ID,或者只有值,如果可能的话。

Also, I'd like to know how can I parse each value separately. For example, only the userid of 'a', or only the value, if its possible.

非常感谢

推荐答案

这是因为 JSON.parse(localStorage.getItem('a'))返回一个对象。你不能在 .text()中使用这样的对象作为参数。

That is because JSON.parse(localStorage.getItem('a')) returns an object. You cannot use an object like that as an argument in .text().

这虽然有效:

$('p').text(JSON.parse(localStorage.getItem('a')).value);

这篇关于jQ:解析本地存储&字符串化值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆