Example of JSON parsing
As we mentioned, we will show how we can parse a json file, so we will make our own .json
file. This file is named jsonTestFile.json
and has the next structure:
jsonTestFile.json:
{
"id": 1,
"firstname": "Katerina",
"languages": [
{ "lang":"en" , "knowledge":"proficient" },
{ "lang":"fr" , "knowledge":"advanced" },
]
"job":{
"site":"www.javacodegeeks.com",
"name":"Java Code Geeks",
}
}
Now create a java file in your project, named JsonParseTest
. Then paste the following code.
JsonParseTest.java:
01 | package com.javacodegeeks.javabasics.jsonparsertest; |
03 | import java.io.FileNotFoundException; |
04 | import java.io.FileReader; |
05 | import java.io.IOException; |
06 | import java.util.Iterator; |
08 | import org.json.simple.JSONArray; |
09 | import org.json.simple.JSONObject; |
10 | import org.json.simple.parser.JSONParser; |
11 | import org.json.simple.parser.ParseException; |
13 | public class JsonParseTest { |
15 | private static final String filePath = "C:\\Users\\katerina\\Desktop\\jsonTestFile.json" ; |
17 | public static void main(String[] args) { |
21 | FileReader reader = new FileReader(filePath); |
23 | JSONParser jsonParser = new JSONParser(); |
24 | JSONObject jsonObject = (JSONObject) jsonParser.parse(reader); |
27 | String firstName = (String) jsonObject.get( "firstname" ); |
28 | System.out.println( "The first name is: " + firstName); |
31 | long id = ( long ) jsonObject.get( "id" ); |
32 | System.out.println( "The id is: " + id); |
35 | JSONArray lang= (JSONArray) jsonObject.get( "languages" ); |
38 | for ( int i= 0 ; i<lang.size(); i++){ |
39 | System.out.println( "The " + i + " element of the array: " +lang.get(i)); |
41 | Iterator i = lang.iterator(); |
45 | JSONObject innerObj = (JSONObject) i.next(); |
46 | System.out.println( "language " + innerObj.get( "lang" ) + |
47 | " with level " + innerObj.get( "knowledge" )); |
50 | JSONObject structure = (JSONObject) jsonObject.get( "job" ); |
51 | System.out.println( "Into job structure, name: " + structure.get( "name" )); |
53 | } catch (FileNotFoundException ex) { |
55 | } catch (IOException ex) { |
57 | } catch (ParseException ex) { |
59 | } catch (NullPointerException ex) { |
Now lets explain the code above. After we create an instance of JSONParser
, we create a JSONObject
by parsing the FileReader
of our .json
file. This JSONObject
contains a collection of key-value pairs, from which we can get every value of the json file. To retrieve primitive objects, get()
method of the JSONObject's
instance is called, defining the specified key as an argument. It is important to add the suitable cast to the method. For array types in json file, JSONArray
is used that represents an ordered sequence of values. As you can notice in the code, an Iterator
should be used in order to take each value of the json array. A structure in the json file, signs the creation of a new JSONObject
in order to retrieve the values.
You can see the output of the execution below.
No comments:
Post a Comment