JSON Introduction

JSON Introduction

JSON - Evaluates to JavaScript Objects

The JSON format is syntactically identical to the code for creating JavaScript objects.

Because of this similarity, instead of using a parser (like XML does), a JavaScript program can use standard JavaScript functions to convert JSON data into native JavaScript objects.

Run it Yourself

With our editor, you can edit JavaScript code online and click on a button to view the result:

JSON Example:

<!DOCTYPE html>
<html>
<body>

<h2>JSON Object Creation in JavaScript</h2>

<p id="demo"></p>

<script>
var text = '{"name":"John Johnson","street":"Oslo West 16","phone":"555 1234567"}';

var obj = JSON.parse(text);

document.getElementById("demo").innerHTML =
obj.name + "<br>" +
obj.street + "<br>" +
obj.phone;
</script>

</body>
</html>

Try it Yourself

Much Like XML Because

  • JSON and XML is "self describing" (human readable)
  • JSON and XML is hierarchical (values within values)
  • JSON and XML can be parsed and used by lots of programming languages
  • JSON and XML can be fetched with an XMLHttpRequest

Much Unlike XML Because

  • JSON doesn't use end tag
  • JSON is quicker to read and write
  • JSON is shorter
  • JSON can use arrays
The biggest difference is:

 XML has to be parsed with an XML parser, JSON can be parsed by a standard JavaScript function.

Why goto JSON ?

JSON is faster and easier than XML,AJAX applications:

XML
  • Fetch an XML document
  • Use the XML DOM to loop through the document
  • Extract values and store in variables
 JSON
  • Fetch a JSON string
  • JSON.Parse the JSON string

Share on Google Plus

About Blogger

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Thank You...!