GSON Tree Model API provides JsonParser help you parse Json string to tree model of JsonElement. Beside, JsonElement provides method isJsonXXX() and getAsYYY() to check type of Json element and get value of element on that tree node.
On the writing, Gson Tree Model API provides method toJsonTree help you converts a java object into a tree JsonElement also. Using this JsonElement you could manipulate the Tree Model at runtime, for example add/update/remove fields/property.
Other interesting posts you may like
Now you should practice with gson completed sample below.
GSON Tree Model API library dependency should be added in pom.xml
1 2 3 4 5 |
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.6.2</version> </dependency> |
Read JSON data from input file, for example input.json
1 |
{"name":"Banana","price":86,"produceby":["Cuba","Indonesia","Brazil"]} |
This program reads json data from file then display output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.javabycode.json; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; public class GsonTreeModelReadingSample { public static void main(String[] args) throws FileNotFoundException { FileReader reader = new FileReader(new File("C:/javabycode/json/input.json")); JsonParser parser = new JsonParser(); // Parse input to json element JsonElement element = parser.parse(reader); System.out.println("********** Parse json string to tree model using Gson **********"); if (element.isJsonObject()) { JsonObject fruit = element.getAsJsonObject(); // read as string System.out.println("name = " + fruit.get("name").getAsString()); // read as double System.out.println("price = " + fruit.get("price").getAsDouble()); // read as array JsonArray arr = fruit.getAsJsonArray("produceby"); for (int i = 0; i < arr.size(); i++) { System.out.println("produceby = " + arr.get(i).getAsString()); } } } } |
Run it and you should get the output
1 2 3 4 5 6 |
********** Parse json string to tree model using Gson ********** name = Banana price = 86.0 produceby = Cuba produceby = Indonesia produceby = Brazil |
You also add/update/remove on tree model and convert it into Json string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.javabycode.json; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; public class GsonTreeModelWriteSample { public static void main(String args[]) { Fruit fruit = getFruit(); Gson gson = new GsonBuilder().create(); JsonElement jsonElement = gson.toJsonTree(fruit); System.out.println("Original Tree model JSON : " + jsonElement); System.out.println("********** Add/update/remove element on JSON tree model **********"); JsonObject jsonObject = jsonElement.getAsJsonObject(); // add a new property jsonObject.addProperty("recommend", "Should eat"); // remove an existing property jsonObject.remove("cost"); // update an existing property jsonObject.getAsJsonArray("pruduceby").set(0, new JsonPrimitive("India")); System.out.println("Updated Tree model JSON : " + jsonObject); } private static Fruit getFruit() { Fruit fruit = new Fruit(); fruit.setName("Orange"); fruit.setCost(78); fruit.getPruduceby().add("Cuba"); fruit.getPruduceby().add("Indonesia"); fruit.getPruduceby().add("Brazil"); return fruit; } } |
The bean Fruit is used to setup data for tree model json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.javabycode.json; import java.util.ArrayList; import java.util.List; public class Fruit { private String name; private double cost; private List<String> pruduceby = new ArrayList<String>(); public String getName() { return name; } public void setName(String name) { this.name = name; } public double getCost() { return cost; } public void setCost(double cost) { this.cost = cost; } public void setPruduceby(List<String> pruduceby) { this.pruduceby = pruduceby; } public List<String> getPruduceby() { return pruduceby; } } |
Here is a ouput
1 2 3 |
Original Tree model JSON : {"name":"Orange","cost":78.0,"pruduceby":["Cuba","Indonesia","Brazil"]} ********** Add/update/remove element on JSON tree model ********** Updated Tree model JSON : {"name":"Orange","pruduceby":["India","Indonesia","Brazil"],"recommend":"Should eat"} |
Give you other bonus on how to Convert Java collections into JSON and JSON to collections