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 .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

2 Responses to “Example On Converting JSON Text To JavaScript Object”
  1. siva says:

    its a good tutorial for know about json. keep it up

    Thanks
    siva

  2. Venu says:

    Very new to Ajax/json scripts

    thanks for your tutorials – helps easy to grasp.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.