简体   繁体   English

HTML5本地存储JSON多个对象

[英]HTML5 local storage JSON multiple objects

Does anyone know, if it's possible to make a local storage with multiple objects in it when I'm doing a loop in javascript? 有谁知道,当我在javascript中循环时,是否有可能在其中存储多个对象的本地存储?

At the moment my code looks like this: 目前,我的代码如下所示:

var albums = '';
var album_list = '';

$.each(data, function(i,item){
   var name = item.name;
   albums += '<li>'+item.name+'</li>';

   var album_content = {name: item.name, uid: 1};
   var album_content = album_content+',';

   album_list += album_content;


});

var album_list = '['+album_list+']';   
localStorage.setItem("albums", JSON.stringify(album_list));
var albums_l = JSON.parse(localStorage.getItem("albums"));

$.each(albums_l, function(i,item){
   console.log(item.name);
});

But that gives me an error. 但这给了我一个错误。 Anyone have a better solution for this, so I just have one localstorage and all the data in that? 有人对此有更好的解决方案,所以我只有一个本地存储,而其中的所有数据都没有?

You're mixing objects and strings in your code. 您正在代码中混合对象和字符串。 Your album_content is initialized in the loop as an object ( {name: item.name, uid: 1} ). 您的album_content在循环中初始化为一个对象( {name: item.name, uid: 1} )。 In the very next line you treat it as a string by appending a comma. 在下一行中,通过添加逗号将其视为字符串。

Overall use an array to collect all your objects. 总体上使用数组来收集所有对象。 It's (almost) always better to use the native objects instead of trying to emulate them in some way. 使用本机对象(而不是尝试以某种方式模拟它们)总是(几乎)总是更好。

Try this code instead. 请尝试使用此代码。

var albums = '';
var album_list = [];

$.each(data, function(i,item){
   var name = item.name;
   albums += '<li>'+item.name+'</li>';

   var album_content = {name: item.name, uid: 1};
   // remove that line completly
   // var album_content = album_content+',';

   album_list.push( album_content );

});


localStorage.setItem("albums", JSON.stringify( album_list ));
var albums_l = JSON.parse(localStorage.getItem("albums"));

$.each(albums_l, function(i,item){
   console.log(item.name);
});

What's wrong: 怎么了:

var albums = '';
var album_list = '';

$.each(data, function(i,item){
   var name = item.name;
   albums += '<li>'+item.name+'</li>';

   var album_content = {name: item.name, uid: 1};

   // at this point album_content is an object.. so it will be this string afterwards: "[object Object],"
   var album_content = album_content+',';

   album_list += album_content; //you're adding "[object Object],"


});

// album_list will now look like this: '["[object Object],[object Object],"]'
var album_list = '['+album_list+']';
localStorage.setItem("albums", JSON.stringify(album_list));
var albums_l = JSON.parse(localStorage.getItem("albums"));

$.each(albums_l, function(i,item){
   console.log(item.name);
});

How to do it correct: 如何正确执行:

var albums = '';
var album_list = [];

$.each(data, function(i,item){
   var name = item.name;
   albums += '<li>'+item.name+'</li>';

   var album_content = {name: item.name, uid: 1};

   album_list.push(album_content);


});

localStorage.setItem("albums", JSON.stringify(album_list));
var albums_l = $.parseJSON(localStorage.getItem("albums"));

$.each(albums_l, function(i,item){
   console.log(item.name);
});

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

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