Kotanet
Kotanet

Reputation: 573

Jquery JSON array from string

I have such array in Jquery code:

var marr = new Array();

marr[0] = new Object();
marr[0]["name"] = "Spot 1";
marr[0]["value"] = 20;

marr[1] = new Object();
marr[1]["name"] = "Spot 2";
marr[1]["value"] = 70;

How can I represent this array as a (more or less) user friendly string (to give users an ability to send some values as plugin options). An than parse it back for jquery?

Upvotes: 0

Views: 1024

Answers (3)

Ankush Jain
Ankush Jain

Reputation: 1527

this will be json representation of array

var marr=[
         {"name":"spot 1","value":20}
         {"name":"spot 2","value":70}
         ]

Upvotes: 1

Alexander
Alexander

Reputation: 23537

You could have generated a JSON string from the marr array. It may had given you what you are looking for.

> console.log(JSON.stringify(marr));
[
    {
        "name": "Spot 1",
        "value": 20
    },
    {
        "name": "Spot 2",
        "value": 70
    }
]

Upvotes: 3

Wolf
Wolf

Reputation: 2150

Something like this?

var marr = [ {"name": "Spot 1", "value":20}, {"name": "Spot 2", "value":70} ];

This is same as the code you mentioned above.

Upvotes: 2

Related Questions