- It is a format used to store and exchange data.
- XML and JSON are popular Data Interchange Formats
Json is a simply text format that facilitates reading and writing. It is a
widely used data-interchange language because its parsing and its
generation is easy for machines.
1. Set the environment
Set a proper environment for the compiler, in order to recognize the
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
Otherwise, you have to add the newest version of
Example:
TestFile.json
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
// get a String from the JSON object
String firstName = (String) jsonObject.get("firstname");
System.out.println("The first name is: " + firstName);
// get a number from the JSON object
long id = (long) jsonObject.get("id");
System.out.println("The id is: " + id);
// get an array from the JSON object
JSONArray lang= (JSONArray) jsonObject.get("languages");
// take the elements of the json array
for(int i=0; i<lang.size(); i++){
System.out.println("The " + i + " element of the array: "+lang.get(i));
}
Iterator i = lang.iterator();
// take each value from the json array separately
while (i.hasNext()) {
JSONObject innerObj = (JSONObject) i.next();
System.out.println("language "+ innerObj.get("lang") +
" with level " + innerObj.get("knowledge"));
}
// handle a structure into the json object
JSONObject structure = (JSONObject) jsonObject.get("job");
System.out.println("Into job structure, name: " + structure.get("name"));
Program works as follows:
After we create an instance of
Output:
1. Set the environment
Set a proper environment for the compiler, in order to recognize the
JSON's classes. If you want to built your project via Maven, you should add the following dependency to your pom.xml:<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
Otherwise, you have to add the newest version of
json-simple-1.1.1.jar in your CLASSPATH.Example:
TestFile.json
{
"id": 1,
"firstname": "Venkat",
"languages": [
{
"lang": "en",
"knowledge": "proficient"
},
{
"lang": "fr",
"knowledge": "advanced"
}
],
"job": {
"site": "www.javacodegeeks.com",
"name": "Java Code Geeks"
}
}
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
// get a String from the JSON object
String firstName = (String) jsonObject.get("firstname");
System.out.println("The first name is: " + firstName);
// get a number from the JSON object
long id = (long) jsonObject.get("id");
System.out.println("The id is: " + id);
// get an array from the JSON object
JSONArray lang= (JSONArray) jsonObject.get("languages");
// take the elements of the json array
for(int i=0; i<lang.size(); i++){
System.out.println("The " + i + " element of the array: "+lang.get(i));
}
Iterator i = lang.iterator();
// take each value from the json array separately
while (i.hasNext()) {
JSONObject innerObj = (JSONObject) i.next();
System.out.println("language "+ innerObj.get("lang") +
" with level " + innerObj.get("knowledge"));
}
// handle a structure into the json object
JSONObject structure = (JSONObject) jsonObject.get("job");
System.out.println("Into job structure, name: " + structure.get("name"));
Program works as follows:
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.Output:
The first name is: Venkat
The id is: 1
The 0 element of the array: {"knowledge":"proficient","lang":"en"}
The 1 element of the array: {"knowledge":"advanced","lang":"fr"}
language en with level proficient
language fr with level advanced
Into job structure, name: Java Code Geeks

No comments:
Post a Comment