How to Iterate JSON Object

How To Iterate JSON Object.

What is JSON: JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition – December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

Example of JSON Object

{"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]} 

As you can see 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.

As you can see JSON is very lightweight so it is gaining popularity in web applications over xml. It has following benefits over XML.
1. JSON is more compact and can be easily loaded in JavaScript.
2. XML is stricter and has support for schema and namespaces.
3. JSON parsing is generally faster than XML parsing.
4. JSON is easier to work with in some languages (such as javascript, python, and php)
5. JSON is a more compact format, meaning it weighs far less on the wire than the more verbose XML.
6. JSON specifies how to represent complex datatypes, there is no single best way to represent a data structure in XML.

We often need to iterate JSON object in our program. This is quire easy to do. Let’s take and example of JSON object and we will iterate this JSON Object.

// JSON object
var obj = [{'name':'Deepak','age':'27','city':'jaipur'},{'name':'Jitendra','age':'13','city':'Bhilwara'}];

// iterate JSON Object.
for(var i=0;i <= obj.length; i++) {
    alert(obj[i].name);
    alert(obj[i].age);
    alert(obj[i].city);
}


 

 

 

Deepak Arora

Deepak Arora is a Software Developer and working continuously in this field from 2011. He work mainly with Microsoft Technology. He holds a Master's in Computer Application and he is MCTS certified person.

Leave a Reply

Your email address will not be published. Required fields are marked *