omgRawr Posted May 2, 2009 Share Posted May 2, 2009 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 More sharing options...
Synook Posted May 2, 2009 Share Posted May 2, 2009 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 More sharing options...
jeffman Posted May 2, 2009 Share Posted May 2, 2009 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 More sharing options...
justsomeguy Posted May 4, 2009 Share Posted May 4, 2009 For languages other than PHP, or if your installation of PHP does not include the json_encode and json_decode functions, you can check http://json.org for other implementations. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.