Jump to content

Json


omgRawr
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...