Newsletter |
Example On Converting JSON Text To JavaScript Object
Json » on Feb 1, 2012 { 2 Comments } By Sivateja
Let us see this concept before we see an example
- JSON arrays are written inside square brackets
- Array can contain any number of objects
Syntax
{"names": ['+'{"name" : "java4s", "address" : "United States", "admin" : "sivateja", "age" : 25},'+'{"name" : "php4s", "address" : "United States", "admin" : "teja", "age" : 26}'+']}
Here names is an array containing 2 objects. By using JavaScript syntax we can access the values with the help of “.” operator, will show you this in the following example.
Example
<html> <head> <title> How To Create JSON Object Array </title> <script language="javascript" > var JsonObjecttext = '{"names": ['+'{"name" : "java4s", "address" : "United States", "admin" : "sivateja", "age" : 25},'+'{"name" : "php4s", "address" : "United States", "admin" : "teja", "age" : 26}'+']}'; var JsonObjectName = eval ("(" + JsonObjecttext + ")"); document.write("<font color='#148ee3' size='2' face='verdana'>Name - <font color='#000'>" +JsonObjectName.names[0].name+"</font></font> || "); document.write("<font color='#148ee3' size='2' face='verdana'>Age. - <font color='#000'>" +JsonObjectName.names[0].age+"</font></font> || "); document.write("<font color='#148ee3' size='2' face='verdana'>name - <font color='#000'>" +JsonObjectName.names[1].name+"</font></font> || "); document.write("<font color='#148ee3' size='2' face='verdana'>name - <font color='#000'>" +JsonObjectName.names[1].age+"</font></font>"); </script> </head> <body> <br> <font face="verdana" size="4">Example On Createing JSON Object Array</font><br><br> <font face="verdana" color="#ff0024" size="2"> Note:<br> <font color="#000"> var JsonObjectName - Here JsonObjectName is the object name, <br>can give your own name.</font></font> </body> </html>
Output
You Might Also Like
::. About the Author .:: | ||
Comments
2 Responses to “Example On Converting JSON Text To JavaScript Object”
its a good tutorial for know about json. keep it up
Thanks
siva
Very new to Ajax/json scripts
thanks for your tutorials – helps easy to grasp.