Parsing and generating JSON data on Android Applications is a very common task. Most of the Web Services are in JSON format now. So, you need to find solutions to work efficiently with JSON format in your applications.
Several solutions exist but today, we are going to use Fastjson, a lightweight but powerful library made by Alibaba, the famous China giant. Fastjson is open source and available here on GitHub :
You can also watch this tutorial in video on YouTube :
Fastjson in brief
Fastjson can be used in Android Apps but also for Java Applications. It is used to convert Java POJO into the equivalent JSON representation. Obviously, it can also be used to convert a JSON string to an equivalent Java POJO hierarchy.
Fastjson offers the following advantages :
- It provides excellent performance for parsing and generating JSON data
- It works on client side but also on server side
- It provides simple toJSONString() and parseObject() methods to convert Java POJO to JSON and vice-versa
- It works with pre-existing unmodifiable objects which must be converted to and from JSON even if you don’t have their source code
- It supports arbitrarily complex objects
Using Fastjson in an Android App
To discover the usage of Fastjson in an Android Application, we are going to create a Java POJO hierarchy. We will consider the following objects :
- a User object with some properties like name
- a Group which will contain some users
- a Groups root object which will be used to represent the list of all the Group objects
With that hierarchy of objects, the first operation will be to create the equivalent representation in JSON format.
Then, we will parse the JSON string generated previously to try to find the complete Java objects hierarchy.
Adding the Fastjson dependency
First step is to add the dependency to Fastjson in the build.gradle file on our Android Studio project :
Creating our Java POJOs
The code for our Java POJOs will be quietly simple like you can see :
Creating a simple User Interface
To see the result of our JSON generating operation, we will use a simple User Interface with a TextView centered at the screen :
Creating some data
Now, we need to create some data to encode in JSON format. So, we create a dedicated method. Inside this method, we generate some groups with some users :
Generating JSON with Fastjson
To generate a representation of our Java objects in JSON, we have just to use the toJSONString() static method of the JSON object provided by Fastjson. It is really simple and powerful :
The result of that generation can be displayed on the TextView of our User Interface :
Parsing JSON into Java Objects
The inverse operation consists to parse the JSON string representation returned previously for getting the Java Objects corresponding hierarchy. For that, we have to pass the JSON string representation in parameter of the parseObject() static method of the JSON object provided by Fastjson.
Besides, we need to pass as a second parameter the root class of our hierarchy of objects. In our example, it’s the Groups class :
Complete code of the Main Activity
Like you can see, using Fastjson to parse and generate JSON from Java Objects is really simple. Below, you will find the complete code of the Main Activity we have used for our sample Android Application :
That’s all for that tutorial !
You can subscribe to the SSaurel’s Channel if you want to discover more tutorials on Android Development :