jQuery JSON.stringify不会将整个JSON字符串存储到cookie [英] jQuery JSON.stringify not getting the entire JSON string to store to cookie

查看:644
本文介绍了jQuery JSON.stringify不会将整个JSON字符串存储到cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个cookie中设置JSON字符串的分数...

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

警报正在说:


{s:{score:2000,name:Michael}}

{"s":"{score":2000,"name":"Michael"}}

...而不是整个对象。如何使整个对象成为JSON字符串?

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

推荐答案

这是因为在你的json中,对于所有值,您需要一个数组

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

这篇关于jQuery JSON.stringify不会将整个JSON字符串存储到cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆