Tuesday 15, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
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.

 JSONExample.java

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(
"C:\\jtc\\javatutorialscorner.json"));

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) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

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

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • JSON Tree Model in JacksonIn this Tutorials we are going to see how to read and write JSON Tree model using Jackson JsonNode. The JsonNode is used to read and write tree structured date.It is similar to XML Dom tree. 1. Create project called JSONE… Read More
  • JSON to Map using JacksonIn this tutorial we are going to see how to convert JSON into Map and retrieve the values   1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.jackson  3. add jackson-map… Read More
  • How to Create Pretty Print JSON using JacksonIn this Tutorials we are going to see how to create Pretty Print JSON using Jackson. 1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.jackson  3. Create java class called… Read More
  • Write JSON using GSON Streaming Gson can read and write JSON as an object model or stream.Gson’s object model uses the same approach as XML DOM and XML pull parser.Gson doesn’t support event-based model like SAX. Streaming Access JsonReader and JsonWriter … Read More
  • Create Map to JSON using JacksonIn this tutorial we are going to see how to create JSON from map and print json as string and also write json in file 1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.jackson  … Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Read JSON Data from File Rating: 5 Reviewed By: eHowToNow