<< Chapter < Page Chapter >> Page >
Figure 1. The screen output. {"student":true,"name":"Joe","age":21} {"student":false,"name":"Sue","age":32}{"student":true,"name":"Tom","age":19}

The format for each line of text that you see in Figure 1 is the format that you would expect for a JSON string that doesn't include array data. (See Introducing JSON for more details regarding format.)

Briefly, each key is separated from its value by a colon (:) . Each key/value pair is separated from its neighbors by a comma (,) . The JSON string begins with a left brace ({) and ends with a right brace (}) . Keys are surrounded by double quotation characters. Values may or may not beseparated by double quotation characters, depending on their type. Some characters inside of values, such as double quotation characters and backslashcharacters must be escaped with a backslash character (\" and \\) but that is not shown by this program.

Note that the display order for the key/value pairs shown in Figure 1 does not match the order in which the objects were populated in Listing 2 and Listing 3 . A HashMap does not impose an ordering on its contents and does not guarantee that the ordering will remainconstant over time.

The end of the program

Listing 5 also signals the end of the main method and the end of the program.

Run the program

I encourage you to copy the code from Listing 6 . Execute the code and confirm that you get the same results as those shown in Figure 1 . Experiment with the code, making changes, and observing the results of your changes. Make certain that youcan explain why your changes behave as they do.

Complete program listing

A complete listing of the program named Code.java is provided in Listing 6 .

Listing 6. The program named Code.java. /******************************************************************** Copyright 2016 R.G.BaldwinConstructs three JSONObject objects and saves them in an ArrayList. Transforms each JSONObject object into a String object and saves thestrings in a second ArrayList object. Displays the strings.Tested with Java 8, Win 7, and json-simple-1.1.1.jar. ********************************************************************/import org.json.simple.JSONObject; import java.util.ArrayList;import java.util.Iterator; class Code {public static void main(String[] args){//Create a container for several JSON objects. ArrayList<JSONObject>listA = new ArrayList<JSONObject>(); //Create a container for several JSON stringsArrayList<String>listB = new ArrayList<String>();//Create and populate the first JSONObject with unordered // key/value pairs.listA.add(new JSONObject()); listA.get(0).put("name","Joe");listA.get(0).put("age",21); listA.get(0).put("student",true);//Create and populate the second JSONObject. Note that the object// is populated in a different order than above. listA.add(new JSONObject());listA.get(1).put("student",false); listA.get(1).put("name","Sue");listA.get(1).put("age",32);//Create and populate the third JSONObject listA.add(new JSONObject());listA.get(2).put("name","Tom"); listA.get(2).put("age",19);listA.get(2).put("student",true); //Transform the three JSON objects into JSON strings and save// them in ListB. Could write them to disk for transfer to a // different programming environment at this point.Iterator<JSONObject>iteratorA = listA.iterator(); while (iteratorA.hasNext()){listB.add(iteratorA.next().toJSONString()); }//end while loop//Display the JSON strings currently stored in listB. Note that // the display order does not necessarily match the order in// which the original objects were populated. Iterator<String>iteratorB = listB.iterator(); while (iteratorB.hasNext()){System.out.println(iteratorB.next()); }//end while loop}//end main }//end class Code


This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Json0215: Encoding JSON Strings
  • File: Json0215.htm
  • Published: 05/31/16

Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. Ineither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please beaware that it is a copy of a module that is freely available on cnx.org and that it was made and published withoutmy prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.


Questions & Answers

newtons third law of motion
Nomvulah Reply
how to deal with newtons third law of motion
help me with Newtons laws of universal gravitation
Ntsako Reply
Every particle in the universe attract every other particle with a force that is directly proportional to the product of their mass and inversely proportional to the square of distance between them
guys plz help me how to calculate net force?
Thabiso Reply
its FN +Fg+ Ff+ Ft+Fapplied = Net Force or any force acting on that object
net force is all the forces acting on an Object that is your FN +FG even T +fy or fx anyforce that will act on the object
What's Newton second law
Newton's second law...is when resultant/net force acts on an object,the object will accelerate in the direction of the force at an acceleration directly proportional to the force and inversely proportional to the mass of the object
How do yu calculate normal force on a inclined
any one who can help me with chemistry
george Reply
Yeah Sure. I will help
OK first can you pls teach me how to to calculate on chemistry chapter
What is a derivative?
Hydrocarbon derivatives are compounds that are made primarily of carbon and hydrogen atoms with specific groups of atoms attached. These specific groups of atoms are called functional groups. Hydrocarbon derivatives contain at least one element that is electronegative.
How to find the integral of a specific function?
how do u calculate the mass of an object if u have been given the gradient of a graph?
Hlelelwe Reply
what is endothermic reaction
Mphumuzi Reply
what is the difference between intramolecular and intermolecular forces
Xiluva Reply
please help me here how do the molecular masses of the compounds change?
Khutso Reply
how to calculate atom mass
Mbali Reply
how do water molecules form
Mukonazwothe Reply
water molecules consist of two atoms of hydrogen linked by covalent bonding to the same atom of oxygen ...hydrogen bonds are formed easily when two water molecules come close together , but are easily broken when the water molecule moves apart again
similarity between vander waals forces and hydrogen bond
Angel Reply
type of intermolecular forces between 2 hydrogen and 2 oxygen
when an atom X of an element in Group 1 reacts to become X+
Rhulani Reply
You have O.5 dm^3 of a KNO3 solution of concentration 0.2 M .You need to dilute this solution to get a solution of concentration 0.18M. calculate how many cubic cm of H2O you need to achieve this
Thimna Reply
im really struggling with this question please help me
I don't know how to help i nee you help
what is a radium
Zieniengie Reply
when 3.22 moles ofAl react with 4.96 moles HBr, how many moles of H2 are formed?
Devina 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, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?