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

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