<< Chapter < Page Chapter >> Page >

There is nothing new in Listing 1 so that code shouldn't require further explanation. Note however, that the name "Tom" will become significant in alater discussion.

Instantiate and populate a JSONArray object

The code in Listing 2 instantiates and populates a JSONArray object.

Listing 2 . Instantiate and populate a JSONArray object. JSONArray arrayListA = new JSONArray(); arrayListA.add("2-club");arrayListA.add("3-heart"); arrayListA.add("4-diamond");arrayListA.add("5-spade");

Previous pages in this book have constructed JSON strings using a subclass of the Java HashMap class -- (the JSONObject class) . It is worth noting that JSON strings constructed in that manner are unordered.However, JSON arrays constructed using the JSONArray class, which is a subclass of the ArrayList class, are ordered lists.

The program that I will explain in this page is intended to represent the beginning state of a two-person card game where each player receives four cards.The code in Listing 2 constructs a list of the cards that will be dealt to one of the players.

Populate hashMapA with a key/value pair

The code in Listing 3 populates the JSONObject object referred to as hashMapA with a key/value pair where the key is "cards" and the value is an object of the JSONArray class containing a list of strings naming specific playing cards.

Listing 3 . Populate hashMapA. hashMapA.put("cards",arrayListA);

Note that hashMapA already contained a key/value pair identifying one of the players in the game named "Tom" (see Listing 1 ). Thus the code in Listing 1 through Listing 3 can be though of as "dealing" the cards identified in Listing 2 to the player named "Tom".

Create and populate another similar JSON object

Listing 4 creates and populates a second list of playing cards and deals them to the second player in the game whose name is "Joe".

Listing 4 . Create and populate another similar JSON object. JSONObject hashMapB = new JSONObject(); hashMapB.put("name","Joe");JSONArray arrayListB = new JSONArray();arrayListB.add("4-heart"); arrayListB.add("5-heart");arrayListB.add("6-club"); arrayListB.add("7-diamond");hashMapB.put("cards",arrayListB);

Put the players in the game

Now that the players have been created and have received their cards, it is time to put them in the game.

Listing 5 begins by adding the two players and their card arrays to a new object of type JSONArray . This results in nested arrays.

Listing 5 . Put the players in the game. JSONArray arrayListC = new JSONArray(); arrayListC.add(hashMapA);arrayListC.add(hashMapB); JSONObject hashMapC = new JSONObject();hashMapC.put("game",arrayListC);

Then Listing 5 creates a new JSONObject object to represent the game and populatesit with a key/value pair where the key is "game" and the value is the array containing the two players and their card arrays.

Write the JSON string to an output file

Listing 6 calls the writeJSONString method on the JSONObject object to encode the object into a JSON string and write it to an output filenamed junk.json .

Listing 6 . Write the JSON string to an output file. try{ PrintWriter out =new PrintWriter(new File("junk.json")); hashMapC.writeJSONString(out);out.flush(); }catch(IOException ex){ex.printStackTrace(); }//end catch}//end main }//end class Code

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
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Berger describes sociologists as concerned with
Mueller 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?