<< Chapter < Page Chapter >> Page >

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 is provided in Listing 6 .

Listing 6 . The program named Code.java. /******************************************************************** Copyright 2016 R.G.BaldwinOLD CODE: Constructs three JSONObject objects and saves them in an ArrayList.Transforms each JSONObject object into a String object and saves the strings in a second ArrayList object.NEW CODE:Decodes the strings into JSONObjects using the JSONValue parse method and saves them in a third ArrayList object.Decodes the strings into JSONObjects using the JSONParser class and saves them in a fourth ArrayList object.Displays both sets of decoded JSON strings. Tested with Java 8, Win 7, and json-simple-1.1.1.jar.********************************************************************/ import org.json.simple.JSONObject;import org.json.simple.JSONValue; import org.json.simple.parser.JSONParser;import org.json.simple.parser.ParseException; import java.util.ArrayList;import java.util.Iterator; class Code {public static void main(String[] args){//OLD CODE: //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//NEW CODE BEGINS HERE//Create a container for decoded JSON strings. ArrayList<JSONObject>listC = new ArrayList<JSONObject>(); //Decode and display JSON strings using the parse method of the// JSONValue class System.out.println("Decode using JSONValue class");decodeC(listB,listC); display(listC);System.out.println();//blank line//Create another container for decoded JSON strings. ArrayList<JSONObject>listD = new ArrayList<JSONObject>();//Decode and display JSON strings using the parse method of the // JSONParser classSystem.out.println("Decode using JSONParser class"); decodeD(listB,listD);display(listD); System.out.println();//blank line}//end main //---------------------------------------------------------------////Decode a list of JSON strings into JSONObject objects using the// parse method of the JSONValue class static void decodeC(ArrayList input,ArrayList output){String temp = null; Iterator<String>iterator = input.iterator(); while (iterator.hasNext()){temp = iterator.next(); output.add(JSONValue.parse(temp));}//end while loop }//end decodeC//---------------------------------------------------------------////Decode a list of JSON strings into JSONObject objects using the // parse method of the JSONParser classstatic void decodeD(ArrayList input,ArrayList output){ JSONParser parser = new JSONParser();String temp = null; Iterator<String>iterator = input.iterator(); while (iterator.hasNext()){temp = iterator.next(); try{//ParseException; must be caught or declared to be thrown. output.add(parser.parse(temp));}catch(ParseException pex){ pex.printStackTrace();}//end catch }//end while loop}//end decodeD //---------------------------------------------------------------////Display the values in a list of JSONObject objects for a known// set of keys. static void display(ArrayList input){JSONObject temp = null; Iterator<JSONObject>iterator = input.iterator(); while (iterator.hasNext()){temp = iterator.next(); System.out.print("Name: " + temp.get("name"));System.out.print(" Age: " + temp.get("age")); System.out.println(" Is student? " + temp.get("student"));}//end while loop }//end display//---------------------------------------------------------------// }//end class Code

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Json0220: Decoding JSON Strings
  • File: Json0220.htm
  • Published: 05/31/16
Disclaimers:

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.

-end-

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
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?

Ask