Press "Enter" to skip to content

JSON Examples in JavaScript

Definition :

It is a representation which will be used for holding the data in array and object format.

Program 1 :

<html>

   <head>

      <script language=”javascript”>

                     function display()

                                 {

                                 a='[{“name”:”arif”,”age”:24},{“name”:”ranga”,”age”:30}]’;

                                 b=JSON.parse(a);

                                 for(i=0;i<b.length;i++)

                                 document.write(b[i].name+” “+b[i].age+”</br>”);

                                 }

                  </script>

                </head>

                <body>

                   <input type=”button” value=”click” onclick=”display()”/>

                </body>

</html>

Program 2 :

<html>

   <head>

      <script language=”javascript”>

                      function display()

                                  {

                                  a='[{“name”:”arif”,”age”:24,”marks”:[1,2,3,4,5,6]},{“name”:”ranga”,”age”:30,”marks”:[11,12,13,14,15,16]}]’;

                                  b=JSON.parse(a);

                                  for(i=0; i<b.length; i++)

                                  {

                                  document.write(b[i].name+” “+b[i].age);

                                  temp=b[i].marks;

                                  for(j=0; j<temp.length; j++)

                                  {

                                  document.write(temp[j]+”</br>”);

                                  }

                                  }

                                  }

                  </script>

                </head>

                <body>

                   <input type=”button” value=”click” onclick=”display()”/> </br>

                </body>

</html>

Be First to Comment

    Leave a Reply

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