d |
JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging. It is also a subset of JavaScript's Object Notation (the way
objects are built in JavaScript)
An example of where this is used is web services responses. In the 'old' days, web services used XML as their primary data format for transmitting back data, but since JSON appeared (The JSON format is specified in RFC 4627 by Douglas Crockford), it has been the preferred format because it is much more lightweight
You can find a lot more info on one of Crockford's sites here
JSON is built on two structures:
JSON Structure
Here is an example of JSON data:
JSON in JavaScript
When working with JSON and JavaScript, you may be tempted to use the
eval function to evaluate the result returned in the callback, but this is not suggested due to security reasons (malicious data can be sent from the server to the client and then eval ed in the client script with harmful effects).
Therefore, one must always try to use Crockford's script that checks for a valid JSON before evaluating it. Link to the script explanation is found here and here is a direct link to the js file.
Example on how to use the JSON Parser (with the json from the above code snippet):
The JSON Parser also offers another very useful method,
stringify . This method accepts a JavaScript object as a parameter, and outputs back a string with JSON format. This is useful for when you want to send data back to the server:
The above two methods (
parse and stringify ) also take a second parameter, which is a function that will be called for every key and value at every level of the final result, and each value will be replaced by result of your inputted function. (More on this here)
Btw, for all of you out there who think JSON is just for JavaScript, check out this post that explains and confirms otherwise.
JSON Tutorial available
http://www.w3schools.com/json/References
|
Monday, 8 September 2014
What is JSON and why would I use it? Json vs XML
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment