0

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 Answer 1

3

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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.