728x90 AdSpace

Saturday, 5 October 2013

Read JSON Data from File

In this tutorials we are going to see how to read json data from file using JSONSimple jar.

Create project called JSONExample.
Create package called com.javatutorialscorner.json 
Create java class called JSONExample under com.javatutorialscorner.json.


package com.javatutorialscorner.json;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JSONExample {

* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub

JSONParser jsonParser = null;

try {
jsonParser = new JSONParser();
JSONObject json = (JSONObject) jsonParser.parse(new FileReader(

String name = (String) json.get("name");
System.out.println("Name : " + name);

JSONArray jsonArray = (JSONArray) json.get("Topics");
System.out.println("JSON Array : " + jsonArray.toString());

Iterator<String> iterator = jsonArray.iterator();

while (iterator.hasNext()) {
System.out.println("JSON Array String : " + iterator.next());
} catch (FileNotFoundException e) {
} catch (IOException e) {
// TODO Auto-generated catch block
} catch (ParseException e) {
// TODO Auto-generated catch block



Now run the program see the following output in console.

Name : www.javatutorialscorner.com

JSON Array : ["Java","J2EE","Servlet","JSP","JSF","Struts","Spring","Hibernate"]

JSON Array String : Java

JSON Array String : J2EE

JSON Array String : Servlet

JSON Array String : JSP

JSON Array String : JSF

JSON Array String : Struts

JSON Array String : Spring

JSON Array String : Hibernate

Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: Read JSON Data from File Rating: 5 Reviewed By: Annamalai Thangaraj