admin管理员组文章数量:1390803
hey there i saw many questions about this topic but none of them fit my question. i'm trying to use localStorage to store a user custom preferences, i try put an json object into a localStorage key and use it later on. the object at the beginning looks like that:
Object {test: "{a:"b",c:"d"}"}
the JSON.parse
method returns an error, what i done is that:
var local_storage = getAll();
$.parseJSON(JSON.stringify(local_storage.test.substring(0,0).substring(0,local_storage.length,-1)));
the output is :
{a:"b",c:"d"}
but i can't use it as local_storage.test.a why is that and what is the solution for that?
thx for the help :)
Edit!
Thanks to @Oli Soproni B, the solution is:
var key = {a:"b",c:"d"};
var l = JSON.stringify(key);
localStorage.setItem('test',l);
var local_storage = $.parseJSON(localStorage.getItem('test'));
console.log(local_storage);
console.log(local_storage.a);
hey there i saw many questions about this topic but none of them fit my question. i'm trying to use localStorage to store a user custom preferences, i try put an json object into a localStorage key and use it later on. the object at the beginning looks like that:
Object {test: "{a:"b",c:"d"}"}
the JSON.parse
method returns an error, what i done is that:
var local_storage = getAll();
$.parseJSON(JSON.stringify(local_storage.test.substring(0,0).substring(0,local_storage.length,-1)));
the output is :
{a:"b",c:"d"}
but i can't use it as local_storage.test.a why is that and what is the solution for that?
thx for the help :)
Edit!
Thanks to @Oli Soproni B, the solution is:
var key = {a:"b",c:"d"};
var l = JSON.stringify(key);
localStorage.setItem('test',l);
var local_storage = $.parseJSON(localStorage.getItem('test'));
console.log(local_storage);
console.log(local_storage.a);
Share
Improve this question
edited Feb 17, 2015 at 9:17
benjah
asked Feb 17, 2015 at 8:15
benjahbenjah
6607 silver badges31 bronze badges
2
-
what is
local_storage.test.substring(0,0).substring(0,local_storage.length,-1)
? – Arun P Johny Commented Feb 17, 2015 at 8:17 - why are you calling stringify and parse – Arun P Johny Commented Feb 17, 2015 at 8:19
3 Answers
Reset to default 3// data
var k = {a:"b", c: "d"};
// stringify json
var l = JSON.stringify(k);
// set item to local storage
localStorage.setItem('test', l);
// get item to local storage and parse data
var local_storage = $.parseJSON(localStorage.getItem('test'));
console.log(local_storage);
Object {a: "b", c: "d"}
console.log(local_storage.a);
prints b
// or use
var local_storage = JSON.parse(localStorage.getItem('test'));
// in parsing the stringify json data
Localstorage stores string, not object. So you need to convert object to string while storing and converting it to object while retrieving.
To store:
localStorage.setItem("key",JSON.stringify(obj));
To retrieve:
obj = JSON.parse(localStorage.getItem(obj));
See DEMO here.
You used Json.stringify
, because you need to store the data into localstorage as a string only.
you need to parse
that again to JSON
in order to use it as a JSON object,
but not like this
JSON.stringify(local_storage.test.substring(0,0).substring(0,local_storage.length,-1))
This tries to get a substring from your previously stored string, and again tries to stringify it.
You can get the stored string directly like this,
var local_storage = getAll();
var test=JSON.parse(local_storage.test);
And then use, as the test object, as test: {a:"b",c:"d"}
本文标签: javascriptConverting string to a valid JSON objectStack Overflow
版权声明:本文标题:javascript - Converting string to a valid JSON object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744734034a2622216.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论