In this article I am going to explain how to read JSON with JAVA using Google-json library. The basic part of the JSON is JsonElement. There are 4 types JSON elements.
  1. Json Object : Surrounded with curly braces and have child JSON elements
  2. Json Primitive Element : It contains field name and it's value
  3. Json Array Element : It contains field name and array of JSON elements.
  4. Json Null Element : It contains nothing 

Program Flow

Program flow for JSON reading is shown in the below diagram. 
  1. If element is JSON Object then iterate it into field names and JSON elements and process those JSON elements. 
  2. If element is JSON Array then iterate it into JSON elements and process those JSON elements.
  3. If element is JSON Primitive then print its value as String




Java Code :

In the below example there are 3 JSON input strings, One is normal JSON String, Second-one is JSON Array, Third-one is Null.
import java.io.StringReader;
import java.util.Map.Entry;
import java.util.Set;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class JSONExample {
    public static void main(String[] args) {
        JsonParser parser = new JsonParser();
        // JSON String
        String json = "{" + "\"age\":100," 
                + "\"name\":\"srinivas\","
                + "\"school\":\"\","
                + "\"messages\":[\"msg 1\",\"msg 2\",\"msg 3\"]}";
        // JSON Array
        String jsonArray = "[\"msg 1\",\"msg 2\",\"msg 3\"]";
        // JSON NULL
        String jsonNull = "";
        
        Object jsonElement = parser.parse(new StringReader(json));
        Object jsonEleArray = parser.parse(new StringReader(jsonArray));
        Object jsonEleNull = parser.parse(new StringReader(jsonNull));
        
        printJson((JsonElement) jsonElement);
        printJson((JsonElement) jsonEleArray);
        printJson((JsonElement) jsonEleNull);
    }

    public static void printJson(JsonElement jsonElement) {
        
        // Check whether jsonElement is JsonObject or not
        if (jsonElement.isJsonObject()) {
            Set<Entry<String, JsonElement>> ens = ((JsonObject) jsonElement).entrySet();
            if (ens != null) {
                // Iterate JSON Elements with Key values
                for (Entry<String, JsonElement> en : ens) {
                    System.out.println(en.getKey() + " : ");
                    printJson(en.getValue());
                }
            }
        } 
        
        // Check whether jsonElement is Arrary or not
        else if (jsonElement.isJsonArray()) {
                    JsonArray jarr = jsonElement.getAsJsonArray();
                    // Iterate JSON Array to JSON Elements
                    for (JsonElement je : jarr) {
                        printJson(je);
                    }
        }
        
        // Check whether jsonElement is NULL or not
        else if (jsonElement.isJsonNull()) {
            // print null
            System.out.println("null");
        } 
        // Check whether jsonElement is Primitive or not
        else if (jsonElement.isJsonPrimitive()) {
            // print value as String
            System.out.println(jsonElement.getAsString());
        } 
        
    }
}

11 comments:

  1. do not just write the concept of json.Demonstrate the live implementation of autcomplete in facebook,mail id auto completion in gmail.Also include where else a begineer can directly use this concept

    ReplyDelete
  2. Very good explanation with code example.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete

Blogroll

Follow this blog by Email

Popular Posts