I’ve just started to learn how to use JSON and this is probably a noob question: ¿why do I have to parse JSON?
I’ve a JSON object that comes from a php array (I’ve used it json_encode) and it looks like this:
var jason =
{
"book1": {
"price": 10,
"weight": 30
},
"book2": {
"price": 40,
"weight": 60
}
};
and I can loop through it using the $.each() function, so why would I parse it? After apllying a parse, I cant use $.each() anymore, dunno why. And I dont understand why I cant use something like this: console(jason.book1[1]);
I know that the description in jquery says:
Description: Takes a well-formed JSON string and returns the resulting JavaScript object.
But I dont really get it. Anyway, any help would be appriciated!
The above is an object in JavaScript (not technically JSON, since JSON is just a notation for representing objects rather than a data structure. It’s pedantic to say that, though):
However, say you had a string like this:
You could get that string in a variety of ways, but say you have it.
$.parseJSON(jason)will turn that string into the object you have above.See? http://jsfiddle.net/7Qvd2/