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());
        } 
        
    }
}

13 comments:

  1. nice explanation, thanks

    ReplyDelete
  2. 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
  3. Very good explanation with code example.

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

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

    ReplyDelete
  6. New web site is looking good. Thanks for the great effort. Buy Pinterest Followers

    ReplyDelete
  7. MotoGP 21 PC Game Free Download is the next installment in the popular motorcycle simulation racing game series. Like the previous parts of the series, production is officially authorized by the Motorcycle World Championship, thanks to which it includes not only real tracks and two-wheelers, but also classes, teams and competitors.

    ReplyDelete
  8. I love this. It is soo informative. Are you also searching for cheap assignment writing help we are the best solution for you. We are best known for delivering the best services to students without having to break the bank

    ReplyDelete

Blogroll

Popular Posts