问题
I’m trying to set scores in a cookie with a JSON string…
var json = JSON.stringify({
s:{score:2000,name:"Michael"},
s:{score:1000,name:"Tito"},
s:{score:500,name:"Jackie"},
s:{score:100,name:"Marlon"},
s:{score:10,name:"Jermain"}
});
alert(json);
$.cookies.set('highScores',json,30*24);
The alert is saying:
{"s":"{score":2000,"name":"Michael"}}
…and not the entire object. How do I get the whole object to be a JSON string?
回答1:
It is because in your json you are using the same key s for all values you need an array
var scores = [
{
score: 2000,
name: "Michael"
},
{
score: 1000,
name: "Tito"
},
{
score: 500,
name: "Jackie"
},
{
score: 100,
name: "Marlon"
},
{
score: 10,
name: "Jermain"
}
];
console.log(JSON.stringify(scores));
来源:https://stackoverflow.com/questions/5483170/jquery-json-stringify-not-getting-the-entire-json-string-to-store-to-cookie