How would one parse this JSON in jQuery:
{"3":[
{"project_id":27,"name":"Name1"},
{"project_id":28,"name":"Name2"},
{"project_id":29,"name":"Name3"},
{"project_id":32,"name":"Name4"}
]}
How would one parse this JSON in jQuery:
{"3":[
{"project_id":27,"name":"Name1"},
{"project_id":28,"name":"Name2"},
{"project_id":29,"name":"Name3"},
{"project_id":32,"name":"Name4"}
]}
The same way we parse JSON every day, Pinky:
var parsed = JSON.parse(input);
I'm not sure what exactly you need but you can access the elements within the object by core JavaScript. You dont need jQuery for that. For example:
theObject[3][0].product_id
would return 27
JSON data (for example, that returned from an AJAX call) is automatically "parsed" into a structure which is connected to a variable. Or, you can take a string and turn it into a structure using what Adrian showed.
Now, there is one thing about your structure that is problematic: "3" is, as far as I know, not an OK name within an associative array. Change that to "list" or something that meets the requirements of keys like so:
var projects = {"list":[
{"project_id":27,"name":"Name1"},
{"project_id":28,"name":"Name2"},
{"project_id":29,"name":"Name3"},
{"project_id":32,"name":"Name4"}
]};
Then you could access the elements like so.
console.log(projects.list);
console.log(projects.list[0].project_id, projects.list[0].name);