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

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