<< Chapter < Page Chapter >> Page >
Learn how to read a JSON string containing nested array data from a file, decode it, and display its component parts.

Revised: Thu Jun 02 19:28:45 CDT 2016

This page is included in the following Books:

Table of contents

Preface

This is a page from the book titled The json-simple Java Library . The book explains how to use the json-simple Java library to generate, transform, and query JSON text. This page explains how to read a JSON string containing nestedarray data from a file, decode it, and display its component parts.

Viewing tip

I recommend that you open another copy of this module in a separate browser window and use the following links to easily find and view the Figuresand Listings while you are reading about them.

Figures

  • Figure 1 . Screen shot of raw JSON string.
  • Figure 2 . Display first player's name.
  • Figure 3 . The first player's cards.
  • Figure 4 . Information about the second player.

Listings

  • Listing 1 . Read the file containing the JSON string.
  • Listing 2 . Display the JSON string.
  • Listing 3 . Access the game array.
  • Listing 4 . Get information about the first player.
  • Listing 5 . Display first player's cards.
  • Listing 6 . Get and display information about the second player.
  • Listing 7 . The program named Code.java.

General background information

You learned how to encode JSON data containing nested arrays and how to write the encoded JSON string to an output file in the page titled Json0225: Encoding JSON Arrays . The program that I will discuss and explain in the next section will read thatfile from the disk, decode it, and display its component parts.

Discussion and sample code

The program named Code (see Listing 7 ) reads and parses aninput file named junk.json , The file contains a JSON string with nested array data.

Read the file containing the JSON string

I will discuss and explain this program in fragments. The first fragment is shown in Listing 1 .

Listing 1 . Read the file containing the JSON string. class Code{ public static void main(String[]args){ //Instantiate a JSONObject object, which is a subclass of the// Java HashMap class. JSONObject jsonMap = null;try{ //Read json string from a file and parse it into a HashMap.jsonMap = (JSONObject)(JSONValue.parse(new FileReader("junk.json")));}catch(IOException ex){ ex.printStackTrace();}//end catch

The code in Listing 1 shows the beginning of the class and the beginning of the main method. This code reads the JSON string from the input file named junk.json and parses the data into an object of type JSONObject .

There is nothing new in Listing 1 so the code shouldn't need further explanation. When the code in Listing 1 has finished executing, all of the information from the JSON string in the file is encapsulated in the object oftype JSONObject referred to as jsonMap .

Questions & Answers

what does mean opportunity cost?
Aster Reply
what is poetive effect of population growth
Solomon Reply
what is inflation
Nasir Reply
what is demand
Eleni
what is economics
IMLAN Reply
economics theory describes individual behavior as the result of a process of optimization under constraints the objective to be reached being determined by
Kalkidan
Economics is a branch of social science that deal with How to wise use of resource ,s
Kassie
need
WARKISA
Economic Needs: In economics, needs are goods or services that are necessary for maintaining a certain standard of living. This includes things like healthcare, education, and transportation.
Kalkidan
What is demand and supply
EMPEROR Reply
deman means?
Alex
what is supply?
Alex
ex play supply?
Alex
Money market is a branch or segment of financial market where short-term debt instruments are traded upon. The instruments in this market includes Treasury bills, Bonds, Commercial Papers, Call money among other.
murana Reply
good
Kayode
what is money market
umar Reply
Examine the distinction between theory of comparative cost Advantage and theory of factor proportion
Fatima Reply
What is inflation
Bright Reply
a general and ongoing rise in the level of prices in an economy
AI-Robot
What are the factors that affect demand for a commodity
Florence Reply
price
Kenu
differentiate between demand and supply giving examples
Lambiv Reply
differentiated between demand and supply using examples
Lambiv
what is labour ?
Lambiv
how will I do?
Venny Reply
how is the graph works?I don't fully understand
Rezat Reply
information
Eliyee
devaluation
Eliyee
t
WARKISA
hi guys good evening to all
Lambiv
multiple choice question
Aster Reply
appreciation
Eliyee
explain perfect market
Lindiwe Reply
In economics, a perfect market refers to a theoretical construct where all participants have perfect information, goods are homogenous, there are no barriers to entry or exit, and prices are determined solely by supply and demand. It's an idealized model used for analysis,
Ezea
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