JSON - SPLessons

JSON Array And Comments

Home > Lesson > Chapter 6
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JSON Array And Comments

JSON Array

shape Description

An Array is nothing but collection of similar data types and it can be also known as static data structure why because the size of an array will be given at the time of its declaration. Array starts from zero. Following is the syntax to declare an array. [java]datatype[] identifier;[/java]

shape Conceptual Figure

Advantages and Disadvantages

shape Description

JSON Array

shape Description

Generally an array can be defined as a collection of similar elements, in a JSON array represents a list of values in ordering way and it will store the multiple values also. JSON array has the ability to store the string, boolean, number. JSON array will be represented as []. Following are the examples for JSON array.

Array of Strings

Following is an example to represent the array of strings and each string is separated by a comma. [html] ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] [/html]

Array of Numbers

Following is an example to represent the array of numbers and each number is separated by a comma. [html] [12, 34, 56, 43, 95] [/html]

Array of Booleans

Following is an example to represent the array of booleans and each boolean is separated by a comma. [html] [true, true, false, false, true] [/html]

Array of Objects

Following is an example to represent the array of objects and each object is separated by a comma. [html] { "students": [ {"name":"Sachin", "email":"sachin@gmail.com", "age":23}, {"name":"samules", "email":"samules@gmail.com", "age":28}, {"name":"John", "email":"john@gmail.com", "age":33}, {"name":"Bob", "email":"bob32@gmail.com", "age":41} ] } [/html]

Multidimensional Array

[html] [ [ "s", "a", "i" ], [ "a", "n", "t" ], [ "i", "t", "s" ] ] [/html]

Creation Of JSON Array Objects in JavaScript

shape Example

Following is an example. [html] <html> <head> <title>Creation of array object in javascript using JSON</title> <script language = "javascript" > document.writeln("<h2>JSON array object</h2>"); var books = { "Pascal" : [ { "Name" : "Pascal Made Simple", "price" : 700 }, { "Name" : "Guide to Pascal", "price" : 400 }], "Scala" : [ { "Name" : "Scala for the Impatient", "price" : 1000 }, { "Name" : "Scala in Depth", "price" : 1300 }] } var i = 0 document.writeln("<table border = '2'><tr>"); for(i = 0;i<books.Pascal.length;i++){ document.writeln("<td>"); document.writeln("<table border = '1' width = 100 >"); document.writeln("<tr><td><b>Name</b></td><td width = 50>" + books.Pascal[i].Name+"</td></tr>"); document.writeln("<tr><td><b>Price</b></td><td width = 50>" + books.Pascal[i].price +"</td></tr>"); document.writeln("</table>"); document.writeln("</td>"); } for(i = 0;i<books.Scala.length;i++){ document.writeln("<td>"); document.writeln("<table border = '1' width = 100 >"); document.writeln("<tr><td><b>Name</b></td><td width = 50>" + books.Scala[i].Name+"</td></tr>"); document.writeln("<tr><td><b>Price</b></td><td width = 50>" + books.Scala[i].price+"</td></tr>"); document.writeln("</table>"); document.writeln("</td>"); } document.writeln("</tr></table>"); </script> </head> <body> </body> </html> [/html] Above code is an example to use array objects in javascript code, where pascal and scale both are the array object names, document.writeln() is the method to print the data. Following is the description image for the above code. Output: Now compile the code result will be as follows.

JSON Comments

shape Description

JSON does not underpin any comments, but the developer can use comments inside code as follows. [html] { "student": { "name": "sahin", "salary": 860008, "comments": "He is a great cricketer." } } [/html] In the above example, an attribute comments is treated as a comment.

Summary

shape Key Points

  • The comments are not standard in JSON i.e it does not support it.
  • In one array another array can be also inserted.
  • The document.writeln() is the method to read the data.