Read Json data using javax.json.JsonReader - Read int, String and Array in java

Maven dependency -

<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
</dependency>
Or Download jars = bundle (27 KB) and bundle (122 KB)

If you are beginner don't worry learn how to Create new Maven project - In 2 minutes

c:/Ankit/myJson.json >

{
"empId" : 11,
"address" : [ "Paris", "London" ],
"birthDate" : 1534577772673,
"emp_name" : "ankit"
}


Read Json data using javax.json.JsonReader in java 

package com.ankit.json;

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 ReadJsonDataUsingJsonReader {
    public static void main(String[] args) {
          File jsonInputFilenew File("c:/Ankit/myJson.json");
          InputStream is;
          try {
                 isnew FileInputStream(jsonInputFile);

                 // Create JsonReader from file.
                 JsonReader jsonReader = Json.createReader(is);

                 // Create JsonObject from JsonReader.
                 JsonObject empjsonReader.readObject();
                 jsonReader.close();

                 // read string data
                 System.out.println("Emp_Name = "emp.getString("emp_name"));

                 // read integer data
                 System.out.println("Emp_Id = "emp.getInt("empId"));
                
                 // read Array/List
                 System.out.println("Address (array) = "emp.getJsonArray("address"));

          catch (FileNotFoundException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
          }

    }
}

//Output
/*
Emp_Name =  ankit
Emp_Id = 11
Address (array) = ["Paris","London"]
*/



Related links >

1. Jackson JSON -

We can use Jackson api for for processing JSON in java.

Jackson JSON examples


2. Java provides API (JSR 353) for Processing  JSON (JSR 353).
It provides -
  • Object Model API
  • Streaming API

Java API for JSON processing examples >



3. Simple  (simple.JSONObject)



4. Google Gson - processing java json


eEdit
Must read for you :