|
|
How to read Json data using JsonReader?
This page shows an example on how to read json using JsonReader. Reading data using JsonReader is somewhat similar to
XML parsing using DOM API.
As a first step add maven dependent jar files to your classpath. Here is the pom.xml file
for your reference:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>json_exmp</groupId>
<artifactId>json_exmp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.4</version>
</dependency>
</dependencies>
</project>
|
Input Json File: |
{
"emp_id": 1017,
"emp_name": "Nagesh Y",
"emp_designation": "Manager",
"department": "Java2Novice",
"salary": 30000,
"direct_reports": [
"Nataraj G",
"Kalyan",
"Mahitha"
],
"address": {
"street":"MG Road",
"city":"Bangalore"
}
}
|
Here is the java example to read json data using JsonReader object:
package com.javaapi.json.examples;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
public class ReadJsonExample {
public static void main(String a[]){
File jsonInputFile = new File("/Users/java2novice/jsonInput.txt");
InputStream is;
try {
is = new FileInputStream(jsonInputFile);
// Create JsonReader from Json.
JsonReader reader = Json.createReader(is);
// Get the JsonObject structure from JsonReader.
JsonObject empObj = reader.readObject();
reader.close();
// read string data
System.out.println("Emp Name: " + empObj.getString("emp_name"));
// read integer data
System.out.println("Emp Id: " + empObj.getInt("emp_id"));
// read inner json element
JsonObject addrObj = empObj.getJsonObject("address");
System.out.println("City: " + addrObj.getString("city"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|
|
Output: |
Emp Name: Nagesh Y
Emp Id: 1017
City: Bangalore
|
|
|
|
|
Java API for JSON processing examples
- How to read Json data using JsonReader?
- How to read Json array data using JsonArray?
- How to create Json Object using Object Model?
- How to create Json Array using Object Model?
- How to create Json Object using Streaming Model API?
- How to create Json Array using Streaming Model API?
|
|
|
What is wrapper class?
Everything in java is an object, except primitives. Primitives are
int, short, long, boolean, etc. Since they are not objects, they cannot
return as objects, and collection of objects. To support this, java provides
wrapper classes to move primitives to objects. Some of the wrapper classes
are Integer, Long, Boolean, etc.
Good judgment comes from experience, and experience comes from bad judgment.
-- Barry LePatner
|