JSON - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JSON Object

JSONObject

shape Description

As previously discussed, JSON object is utilized to hold keys and values, the following is an example of the JSON object. [html] { "SPlessons": { "name": "Enni", "ID": 513, "Tutorial": "JAVA", "Registered": true } } [/html] Keys and values are separated with colon and keys are strings and values are the types of JSON, each information will be separated by comma. The "{" indicates the JSON object. Following is the image to understand objects in the code. Following is the syntax to write the JSON objects.
var JSONObj = { };

Objects with Strings

While representing a string value needs to be written in double quotes as follows. [html] { "Product": "Splessons", "Email": "support@splessons.com" } [/html]

Objects with Numbers

JSON underpins numbers in double precision floating-point manner. [html] { "integer": 34, "fraction": .2145, "exponent": 6.61789e+0 } [/html]

Objects with Boolean

JSON underpins boolean values also. [html] { "first": true, "second": false } [/html]

JSON Nested Object

In JSON it is possible to create objects inside the another objects as follows. [html] { "firstName": "Sachin", "lastName": "Tendulkar", "age": 37, "address" : { "streetAddress": "Plot-6, Mohan Nagar", "city": "Bangra", "state": "Mubbai", "postalCode": "201007" } } [/html]

Creation of an object in Java Script

shape Example

Following is the code. [html] <html> <head> <title>Creating Object JSON with JavaScript</title> <script language = "javascript" > var JSONObj = { "name" : "splessons.com", "year" : 2015 }; document.write("<h1>JSON with JavaScript example</h1>"); document.write("<br>"); document.write("<h3>Website Name = "+JSONObj.name+"</h3>"); document.write("<h3>Year = "+JSONObj.year+"</h3>"); </script> </head> <body> </body> </html> [/html] Where document.write() is the method to read the completely and splessons.com and 2015 are the values , these values are going to be retrieved by the JSONObj.name, JSONObj.year. Following is an image description of the code. Output: Now compile the code result will be as follows.

Summary

shape Key Points

  • An object of JSON holds key/value pair.
  • In JSON keys and values will be separated by a comma.
  • An object of JSON will be represented with the curly brace {.