parse Json for Java – org.json

Parse JSON for Java using org.json org.json has classes to parse Json for Java. It also converts between JSON and XML, HTTP header, Cookies, and CDF. The main classes are: org.json.JSONObject – This class stores unordered key-value pairs. The value can be Boolean, JSONArray, Number, String or JSONObject.NULL. It has constructors to take in a … Read more

Java – Json Simple

Overview of The API Classes Json Simple is, as the name suggest, a very simple API. The API itself is made up of around 13 classes. The main classes are : JSONParser – This parses Json text. It takes in a java.io.Reader or a String Object. It is also possible to pass a ContentHandler or … Read more

Java Google Json (Gson) New in Gson 2.3

What’s new in Gson 2.3 In this article we look at the additions in Google Json 2.3. There are three main changes : @TypeAdapter annotation Support for JsonPath using JsonReader.getPath() New Methods in JsonArray we now present examples for all three @TypeAdapter Annotation TypeAdapters are used to specify rules to convert a java class to … Read more

Java Google Json (Gson) Custom Serializer

Just as we saw in the previous tutorial, Gson provides way to specify custom serializers and deserializers. Register a custom serializer with the GsonBuilder if you need you own way to convert a java object to json and you a custom deserializer if you dont like Gson’s way of converting json to the java object. … Read more

Java Google Json (Gson) Type Adapter

Using Custom Type Adapter In the earlier tutorials we have seen how gson can serialize and deserialize java classes with or without hierarchies. By default, it introspects the classes and comes with with a strategy for serializing and deserializing it. However, in some cases, you want to specify your own conversion strategy. That is, you … Read more

Java Google Json (Gson) Serializing Inner classes

Serializing inner classes Gson can serialize inner classes and static nested classes. The detailed example below demonstrates the following things. Serializing class containing static nested class Serializing class containing non static nested class (Inner class) De-serializing json to a class containing static and non static inner class Serializing static nested class (without the enclosing type) … Read more

Java Google Json (Gson) Serializing Generics

Serializing list As we saw in the previous tutorial serializing and deserializing classes with generic types is non trivial since generic type information is lost while serializing. Gson provides a class called com.google.gson.reflect.TypeToken to store generic types. The example below shows how to use the TypeToken class to serialize and deserialize Classes with generic types. … Read more

Java Google Json (Gson) Serializing Collections

Serializing list Serializing Collections should have been similar to serializing other objects. However, the problem is that Collections are generic and the generic type information is not maintained in the json. We therefore pass the type while deserializing list. Note that if the Collection has different types of objects then there is no way to … Read more