Gson get value by key example shows how to get JSON value by key using the Gson library. We can directly get value by specifying the key using JsonObject or we can get value using the POJO class.
1. Using JsonObject
This approach converts the string JSON data to JsonObject using the fromJson
method. Once we get the JsonObject, we can specify the key in the get
method to get the corresponding value from it.
1 |
public JsonElement get(java.lang.String memberName) |
The get
method returns a JsonElement object. We need to use the getAsXXX method to get the value from it. For example, for an int value, we need to use the getAsInt
method.
1 |
public int getAsInt() |
This method returns the entry as a primitive int value.
Here is an example of that.
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 |
package com.javacodeexamples.gsonexamples; import com.google.gson.Gson; import com.google.gson.JsonObject; public class GsonGetValueByKeyExample { public static void main(String[] args) { String data = "{\"Id\": \"EMP001\",\"Name\": \"Alex\",\"Salary\": 4.5}"; /* * Convert string data to a JsonObject * using the fromJson method */ JsonObject jsonObject = new Gson().fromJson(data, JsonObject.class); /* * Now we can use relevant get and getAsXXX methods * to get the value of different keys. */ //get the string data System.out.println(jsonObject.get("Name").getAsString()); //get the double data System.out.println(jsonObject.get("Salary").getAsDouble()); } } |
Output
1 2 |
Alex 4.5 |
Make sure to use the respective getAsXXX method for the data type of the value.
2. Using the Pojo class
Instead of parsing JSON data in the JsonObject object, we can directly convert the data to a Pojo class. In this case, we need to use the getter method corresponding to the key to access its value.
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package com.javacodeexamples.gsonexamples; import com.google.gson.Gson; public class GsonGetValueByKeyExample { public static void main(String[] args) { String data = "{\"Id\": \"EMP001\",\"Name\": \"Alex\",\"Salary\": 4.5}"; /* * Convert string data to an employee * object using the fromJson method */ Emp employee = new Gson().fromJson(data, Emp.class); /* * Now we can use relevant getter methods * to get the value of different keys. */ //get the string data System.out.println(employee.getName()); //get the double data System.out.println(employee.getSalary()); } } class Emp{ private String Id; private String Name; private double Salary; public String getId() { return Id; } public void setId(String id) { Id = id; } public String getName() { return Name; } public void setName(String name) { Name = name; } public double getSalary() { return Salary; } public void setSalary(double salary) { Salary = salary; } } |
Output
1 2 |
Alex 4.5 |
In this approach, we need to make sure to define the Pojo fields with the correct data types. Notice that in the above example, the salary key has a value without double quotes in the string JSON data and it also contains a decimal point number. Hence, in the Emp class, I’ve defined the salary field as double.
Please let me know your views in the comments section below.