Tutorial by Examples

If you need to create a JSONObject and put data in it, consider the following example: // Create a new javax.json.JSONObject instance. JSONObject first = new JSONObject(); first.put("foo", "bar"); first.put("temperature", 21.5); first.put("year", 2016);...
If you need to get data from a JSONObject, consider the following example: String json = "{\"foo\":\"bar\",\"temperature\":21.5,\"year\":2016,\"message\":{\"Hello\":\"world\"},\"months\":[\"January\",\&qu...
JSONObject and JSONArray have a few methods that are very useful while dealing with a possibility that a value your are trying to get does not exist or is of another type. JSONObject obj = new JSONObject(); obj.putString("foo", "bar"); // For existing properties of the corre...
Lets assume you have a class called Person with just name private class Person { public String name; public Person(String name) { this.name = name; } } Code: Gson g = new Gson(); Person person = new Person("John"); System.out.println(g.toJson(person)); /...
Lets assume you have a class called Person with just name private class Person { public String name; public Person(String name) { this.name = name; } } Code: Gson gson = new Gson(); String json = "{\"name\": \"John\"}"; Person person ...
String json = "{\"name\": \"John\", \"age\":21}"; JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject(); System.out.println(jsonObject.get("name").getAsString()); //John System.out.println(jsonObject.get("age").getAs...
Pojo Model public class Model { private String firstName; private String lastName; private int age; /* Getters and setters not shown for brevity */ } Example: String to Object Model outputObject = objectMapper.readValue( "{\"firstName\":\"J...
Iterate over JSONObject properties JSONObject obj = new JSONObject("{\"isMarried\":\"true\", \"name\":\"Nikita\", \"age\":\"30\"}"); Iterator<String> keys = obj.keys();//all keys: isMarried, name & age while (keys.has...
You can use method chaining while working with JSONObject and JSONArray. JSONObject example JSONObject obj = new JSONObject();//Initialize an empty JSON object //Before: {} obj.put("name","Nikita").put("age","30").put("isMarried","true&quot...
If you need to add a property with a null value, you should use the predefined static final JSONObject.NULL and not the standard Java null reference. JSONObject.NULL is a sentinel value used to explicitly define a property with an empty value. JSONObject obj = new JSONObject(); obj.put("some...
Here is a simple JsonArray which you would like to convert to a Java ArrayList: { "list": [ "Test_String_1", "Test_String_2" ] } Now pass the JsonArray 'list' to the following method which returns a corresponding...
Suppose you have a pojo class Person public class Person { public String name; public Person(String name) { this.name = name; } } And you want to parse it into a JSON array or a map of Person objects. Due to type erasure you cannot construct classes of List<Person&g...

Page 1 of 1