<< Chapter < Page Chapter >> Page >

This JavaScript object contains a property with a key named friends . The value of the friends property is a JavaScript array containing two JavaScript objects. Therefore, Listing 10 uses square bracket ([]) notation to access and display the values in the objects that are elements of the array.

The code in Listing 10 produces the first four lines of output text in Figure 4 .

Transform the object into a JSON string

Listing 11 transforms the JavaScript object into a JSON string and displays the string.

Listing 11 . Transform the object into a JSON string.
document.write( "<br/>Transform the object into a JSON string."); var jsontext = JSON.stringify(obj01);document.write("<br/>Display JSON string."); document.write("<br/>" + jsontext);

This produces the three lines of output text in the group near the center of Figure 4 . Note the similarity of the string shown in Figure 4 and the object literal code used to create the JavaScript object in Listing 10 .

At the risk of becoming boring, I will state once again that even though they may look alike, a JSON string is different from a JavaScript object.A JavaScript object typically has properties and behavior. A JSON string is simply a package of characters and has no properties or behavior other thanthose that may typically be ascribed to any string of characters.

Transform the JSON string into a JavaScript object

Finally, Listing 12 uses the JSON.parse method to transform the JSON string into a JavaScript object and displays the values of the new object's properties.The object named obj02 is a replica of the original object named obj01 that was created in Listing 10 .

Listing 12 . Transform the JSON string into a JavaScript object.
document.write("<br/>Transform the JSON string " + "into a JavaScript object.");var obj02 = JSON.parse(jsontext); document.write("<br/>Display values in object."); document.write("<br/>" + obj02.friends[0].name+ ":" + obj02.friends[0].age);document.write("<br/>" + obj02.friends[1].name+ ":" + obj02.friends[1].age);</script></body>

The code in Listing 12 produces the bottom four lines of output text shown in Figure 4 .

Listing 12 also signals the end of the script.

A word of caution

At this point, I want to alert you to a possible terminology issue that you may encounter while searching the web for information about JSON.

Possible terminology issue

Listing 13 contains a scaled down version of code that I copied from (External Link) plus some code thatI added myself.

Listing 13 . Possible terminology issue.
<!DOCTYPE html><html><body><h2>JSON Object Creation in JavaScript</h2><p>Name:<span id="jname"></span><br />Age:<span id="jage"></span><br /></p><script>var JSONObject= { "name":"John Johnson","age":33}; document.getElementById("jname").innerHTML=JSONObject.namedocument.getElementById("jage").innerHTML=JSONObject.age document.write("Code added by Baldwin");var JSONstring = JSON.stringify(JSONObject); document.write("<br/>" + JSONstring); var JSObject = JSON.parse(JSONstring);document.write("<br/>" + JSObject.name + ", " + JSObject.age);</script></body></html>

Questions & Answers

Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Introduction to xml. OpenStax CNX. Dec 02, 2014 Download for free at https://legacy.cnx.org/content/col11207/1.18
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to xml' conversation and receive update notifications?