将对象从localstorage转换为文本

时间:2017-03-23 15:06:31

标签: javascript jquery json

我正从localstorage输出对象,我得到:

"[{\"title\":\"q\",\"ingredients\":\"s\",\"instructions\":\"d\",\"moreingredients\":[],\"moreinstruction\":[],\"img\":{\"jQuery3110052397224993886441\":{\"display\":\"\"}}},{\"title\":\"q\",\"ingredients\":\"w\",\"instructions\":\"e\",\"moreingredients\":[],\"moreinstruction\":[],\"img\":{\"jQuery311079670549304635711\":{\"display\":\"\"}}},{\"title\":\"q\",\"ingredients\":\"w\",\"instructions\":\"e\",\"moreingredients\":[],\"moreinstruction\":[],\"img\":{\"jQuery311016424488798697091\":{\"display\":\"\"}},\"salutation\":\"polish\"},{\"title\":\"q\",\"ingredients\":\"q\",\"instructions\":\"a\",\"moreingredients\":[],\"moreinstruction\":[],\"img\":{\"jQuery311067483883379310751\":{\"display\":\"\"}},\"salutation\":\"italian\"},{\"title\":\"q\",\"ingredients\":\"w\",\"instructions\":\"e\",\"moreingredients\":[],\"moreinstruction\":[],\"img\":{\"jQuery3110317360085863979571\":{\"display\":\"\"}},\"salutation\":\"polish\"}]"

但是我想摆脱所有括号,只有实际文本而不是

  

{\"标题\":\" Q \"

我想要页面上的q。

var salt;
$("#salutation").change(function() {
  salt = $(this).children(":selected").attr("id");
});
var existingData = JSON.stringify(localStorage.getItem("key")) || [];
document.getElementById("qq").innerHTML +=(existingData);
$('form').submit(function() {
var newArray = [];
    $(".add_id2").each(function(){
    newArray.push($(this).val());
});
var newArray2 = [];
    $(".add_id").each(function(){
    newArray2.push($(this).val());
});
var existingData = JSON.parse(localStorage.getItem("key")) || [];
var newData = {
  'title': $("#title").val(),
  'ingredients': $("#ingredients").val(),
  'instructions': $("#inst").val(),
  'moreingredients': newArray,
  'moreinstruction': newArray2,
  'img': img,
  'salutation': salt,
    };
existingData.push(newData);
localStorage.setItem("key", JSON.stringify(existingData));

1 个答案:

答案 0 :(得分:3)

localStorage的值始终存储为字符串。您需要使用JSON.parse()将字符串转换为有效对象,然后您可以使用for循环迭代json以使用json[i].title获取标题:



var json = "[{\"title\":\"q\",\"ingredients\":\"s\",\"instructions\":\"d\",\"moreingredients\":[],\"moreinstruction\":[],\"img\":{\"jQuery3110052397224993886441\":{\"display\":\"\"}}},{\"title\":\"q\",\"ingredients\":\"w\",\"instructions\":\"e\",\"moreingredients\":[],\"moreinstruction\":[],\"img\":{\"jQuery311079670549304635711\":{\"display\":\"\"}}},{\"title\":\"q\",\"ingredients\":\"w\",\"instructions\":\"e\",\"moreingredients\":[],\"moreinstruction\":[],\"img\":{\"jQuery311016424488798697091\":{\"display\":\"\"}},\"salutation\":\"polish\"},{\"title\":\"q\",\"ingredients\":\"q\",\"instructions\":\"a\",\"moreingredients\":[],\"moreinstruction\":[],\"img\":{\"jQuery311067483883379310751\":{\"display\":\"\"}},\"salutation\":\"italian\"},{\"title\":\"q\",\"ingredients\":\"w\",\"instructions\":\"e\",\"moreingredients\":[],\"moreinstruction\":[],\"img\":{\"jQuery3110317360085863979571\":{\"display\":\"\"}},\"salutation\":\"polish\"}]";
json = JSON.parse(json);
console.log(json);
for (var i = 0; i < Object.keys(json).length; i++) {
  console.log("Title is: " + json[i].title);
}
&#13;
&#13;
&#13;