
JSON is a data exchange format that is a subset of the object literal notation in JavaScript. It has been gaining a lot of attention lately as a lightweight alternative to XML, especially in Ajax applications. Why is this? Because of the ability in JavaScript to parse information quickly using the eval( ) function. JSON does not require JavaScript, however, and you can use it as a simple exchange format for any scripting language.
Lets take a look what JSON looks like.
function parseResponse( ) { /* Is the /readyState/ for the /request/ a 4 (complete)? */ if (request.readyState == 4) { /* Is the /status/ from the server 200? */ if (request.status == 200) { var jsonString = request.responseText; var response = eval('(' + jsonString + ')'); // here is where the parsing would begin. } else alert('There was a problem retrieving the data: \n' + request.statusText); request = null; } }
This is the equivalent in XML:
<details id="1" type="book"> <author>Insic2.0</author> <title>JSON: Getting started with JSON</title> <detail> <pages extra="15">>783</pages> <isbn>>1234567< /span></isbn> <price us="19.99" ca="19.99" /> </detail> </details>
JSON is more elegant in describing your data because of its simplicity and more lightweight. In the next post of this tutorial I will explain JSON Requests and Responses. So stay tunned with this website
Related posts:
- Ajax Validation with Jquery This is a very informative tutorial for developers who want...
- Importance of noscript tag let’s check out that what the “noscript” tag can do....
- tips to improve webpage load time Patience is a virtue, but for many, it is often...
- Tip to open flash file in full screen in browser A quick tip for designers and developers to open a...
Related posts brought to you by Yet Another Related Posts Plugin.
























6 Responses
Thanks for this. All web developers should follow these steps.
Most web developers know that JS libraries like jQuery and Prototype (to name two) take care of this work for you by wrapping it with their own methods. I still think its nice to know what those libraries are doing under the hood. I look forward to seeing your next post go in depth about responses and requests. As a suggestion and request, I would ask if you could could go over the dangers of using the “eval” function. Unfortunately for parsing JSON this is the only way to do it. However, with recent developments with ECMAScript and browsers being more standards compliant, we are beginning to see native JSON parsers becoming available and JS libraries utilizing them under the hood.
Social comments and analytics for this post…
This post was mentioned on Twitter by exclusivetuts: New blog posting, An Introduction to JSON – Part 1 – http://exclusivetutorials.com/a5...
meager information
@SMiGL: Thanks for commenting. Well, this is part 1 for json, I will post more detailed tutorial in part 2. So stay Tuned.
[...] Introduction to JSON – Part 1 [...]