JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

How to read Json array data using JsonArray?


This page shows how to read Json array data using JsonArray object.

Note: Refer How to read Json data using JsonReader? page for dependent libraries.

Here is the input json file:

Json Input 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"
    }
}

Java example to read json array data:

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.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonValue;

public class ReadJsonArrayEx {

	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 json array
	        JsonArray arrObj = empObj.getJsonArray("direct_reports");
	        System.out.println("\nDirect Reports:");
	        for(JsonValue value : arrObj){
	        	System.out.println(value.toString());
	        }
	        
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Output:
Emp Name: Nagesh Y

Direct Reports:
"Nataraj G"
"Kalyan"
"Mahitha"
<< Previous Program | Next Program >>

Java API for JSON processing examples

  1. How to read Json data using JsonReader?
  2. How to read Json array data using JsonArray?
  3. How to create Json Object using Object Model?
  4. How to create Json Array using Object Model?
  5. How to create Json Object using Streaming Model API?
  6. How to create Json Array using Streaming Model API?
Knowledge Centre
What is race condition?
A race condition is a situation in which two or more threads or processes are reading or writing some shared data, and the final result depends on the timing of how the threads are scheduled. Race conditions can lead to unpredictable results and subtle program bugs. A thread can prevent this from happening by locking an object. When an object is locked by one thread and another thread tries to call a synchronized method on the same object, the second thread will block until the object is unlocked.
Famous Quotations
When I do good, I feel good; when I do bad, I feel bad, and that is my religion.
-- Abraham Lincoln

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.