Jump to content

Json


omgRawr

Recommended Posts

I know that JSON syntax looks something like this:

var Person = { "name" : "John", "age" : 25, "male" : true };

But is there more to it than just that? Or is JSON basically just a "shorthand" method of creating objects in JavaScript? What I mean is that if you didn't use JSON, you would probably create objects like this:

var Person = new Object(); Person.name = "John"; Person.age = 25; Person.male = true;

So is there more to JSON? Is JavaScript all it's used for, or can it do more than that?

Link to comment
Share on other sites

You are right, JSON is just a shorthand method of describing arrays and objects. It is useful because you can translate a JSON-encoded object between languages and still have it understood (with the help of JSON-parsing functions).

Link to comment
Share on other sites

I use JSON almost exclusively in AJAX communications. My PHP server will extract data from mysql and turn it into a string in JSON format. It sends the string to the browser as the response to a query (post or get). When the browser gets it, it's still just a string. But when I put the string through Javascript's eval() function, it turns the string into Javascript objects.One reason for using JSON this way is that it does turn into Javascript objects with one statement. I don't need to parse the data all over again. The other is that it is efficient. With AJAX, the faster the transfer, the better the user experience. Faster = smaller. JSON notation keeps things small.FWIW, you can send JSON data both ways. I don't know how other languages do it, but PHP has JSON parsing functions built-in. You have to construct the strings with less flexibility than you can for Javascript. But it works.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...