【问题标题】:jQuery JSON.stringify not getting the entire JSON string to store to cookiejQuery JSON.stringify 没有将整个 JSON 字符串存储到 cookie
【发布时间】:2011-03-30 07:21:10
【问题描述】:

我正在尝试使用 JSON 字符串在 cookie 中设置分数……

 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"}}

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

【问题讨论】:

    标签: jquery json cookies stringification


    【解决方案1】:

    这是因为在您的 json 中,您对需要数组的所有值都使用相同的键 s

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-07
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      相关资源
      最近更新 更多