How to parse json array in javascript

Parse JSON

A common use of JSON is to read data from a web server, and display the data in a web page:

  1. JavaScript files needed:
    Insert these scripts into <head> tag:

    <head>
       //download this js from "http://jquery.com/download/"
       <script src="js/jquery.min.js"></script>
    </head>
  2. To convert object to string
    Use JSON.stringify to create a string from a object

    var data = [{id: 1, name:'personsName'}, {id: 2, name:'personsName2'}]
    var string = JSON.stringify(data)
  3. To convert string to object
    Use JSON.parse is to create an object from a string

    var data = [{id: 1, name:'personsName'}, {id: 2, name:'personsName2'}]
    var string = JSON.stringify(data)
    var objects = JSON.parse(data)

    Foreach loop

    objects.forEach(function(key, index){
      console.log(key.id);
      console.log(key.name); 
    });

Reference link: http://stackoverflow.com/questions/23068875/parse-json-array-in-jquery-in-foreach-loop

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s