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

what is defense mechanism
Chinaza Reply
what is defense mechanisms
Chinaza
I'm interested in biological psychology and cognitive psychology
Tanya Reply
what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




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?

Ask