开发者

jQuery JSON.stringify not getting the entire JSON string to store to cookie

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,"nam开发者_如何学Goe":"Michael"}}

…and not the entire object. How do I get the whole object to be a JSON string?


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));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜