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.
- Json Object : Surrounded with curly braces and have child JSON elements
- Json Primitive Element : It contains field name and it's value
- Json Array Element : It contains field name and array of JSON elements.
- Json Null Element : It contains nothing
Program Flow
Program flow for JSON reading is shown in the below diagram.
- If element is JSON Object then iterate it into field names and JSON elements and process those JSON elements.
- If element is JSON Array then iterate it into JSON elements and process those JSON elements.
- 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()); } } }
I love this script
ReplyDeletenice explanation, thanks
ReplyDeletedo 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
ReplyDeletesure
Deletevery helpful!
ReplyDeleteVery good explanation with code example.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeletenot bad......
ReplyDeletegreat
ReplyDeleteคาสิโนออนไลน์ที่น่าเชื่อถือและมีความเป็นมืออาชีพที่สุดในตอนนี้
ReplyDeleteโปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
เพียงแค่คุณสมัคร สล็อตออนไลน์ กับทางทีมงานของเราเพียงเท่านั้น
ร่วมมาเป็นส่วนหนึ่งกับเว็บไซต์คาสิโนออนไลน์ของเราได้เลยค่ะ
สมัครสล็อตออนไลน์ >>> Goldenslot
สนใจร่วมสนุกกับ คาสิโนออนไลน์ คลิ๊กได้เลย
มีทั้งคาสิโนออนไลน์ หวยออนไลน์ ฟุตบอลออนไลน์ สล็อตออนไลน์ และอื่นๆอีกมากมาย
New web site is looking good. Thanks for the great effort. Buy Pinterest Followers
ReplyDeleteMotoGP 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.
ReplyDeleteI 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