我首先在json
中存储localstorage
,然后再将其存储在最终json
中,但我在最终backslash
中获得额外json
。我的代码是:
<!DOCTYPE html>
<html>
<body>
<script>
var mi = [{
"name": "Alex",
"address": "abc"
},
{
"name": "George",
"address": "efg"
}
]
localStorage.setItem("myData", JSON.stringify(mi))
var FinalJson = {
"Collected values" : localStorage.getItem("myData"),
"Place" : "washington"
};
document.write(JSON.stringify(FinalJson));
</script>
</body>
</html>
我得到的输出是:
{"Collected values":"[{\"name\":\"Alex\",\"address\":\"abc\"} {\"name\":\"George\",\"address\":\"efg\"}]","Place":"washington"}
我错了,我得到了额外的backslash
。如何删除额外的backslashes
?附:我不想直接将变量mi
存储在我的FinalJson
中,因为它包含一些更复杂的值,为简单起见,我已将其删除。
答案 0 :(得分:3)
它被串联两次
var FinalJson = {
"Collected values" : JSON.parse(localStorage.getItem("myData")),
"Place" : "washington"
};
答案 1 :(得分:2)
var mi = [{
"name": "Alex",
"address": "abc"
},
{
"name": "George",
"address": "efg"
}
];
localStorage.setItem("myData", JSON.stringify(mi))
var tes = JSON.parse(localStorage.getItem("myData")); //It was "work". I thougt you missed that
var FinalJson = {
"Collected values" : tes,
"Place" : "washington"
};
document.write(JSON.stringify(FinalJson));
document.write(tes);