Thursday 10, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 6 October 2013

Read JSON using GSON Stream


In this tutorial we are going to see how to read JSON Data from file using GSON
1. Create project called JSONExample.
2. Create package called com.javatutorialscorner.gson 
3. Create java class called GSONStreamRead under com.javatutorialscorner.gson.
GSONStreamRead.java
package com.javatutorialscorner.gson;

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

import com.google.gson.stream.JsonReader;

public class GSONStreamRead {

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

  try {
   jsonReader = new JsonReader(new FileReader(
     "C:\\jtc\\javatutorialscorner.json"));
   jsonReader.beginObject();
   while (jsonReader.hasNext()) {
    String name = jsonReader.nextName();

    if (name.equals("Name")) {
     System.out.println("String : " + jsonReader.nextString());
    } else if (name.equals("URL")) {
     System.out.println("String : " + jsonReader.nextString());
    } else if (name.equals("Tutorials")) {

     jsonReader.beginArray();
     while (jsonReader.hasNext()) {
      System.out.println("Arary List : "
        + jsonReader.nextString());
     }
     jsonReader.endArray();
    } else {
     jsonReader.skipValue();
    }

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

 }

}

Now run the program see the following output in console.

String : Java Tutorials Corner

String : http://www.javatutorialcorner.com


Arary List : Java


Arary List : Servlet


Arary List : JSP


Arary List : Struts


Arary List : Hibernate


Arary List : Spring

Shop and help us

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

Related Posts:

  • Read JSON using GSON Stream In this tutorial we are going to see how to read JSON Data from file using GSON 1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.gson  3. Create java class called GSONStreamR… Read More
  • JSON Pretty Print using GSONIn this tutorial we are going to see how to Create Pretty Print JSON String using GSON.1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.gson  3. Create java class called GsonPojo… Read More
  • How to Convert JSON String to Object using GSONIn this Tutorials we are going to see how to convert JSON String to Java object using GSON library. 1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.gson  3. Create java class ca… 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
  • How to Convert Object to JSON String using GSONJSON – JavaScript Object Notation.JSON is the simple, easy and lightweight framework for data exchange.It is good replacement for XML format StringGSON GSON is java library for create JSON String. It was created for use insid… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Read JSON using GSON Stream Rating: 5 Reviewed By: eHowToNow