In this blog, we going to discuss how to parse JSON using Gson and Jackson Library in android.
When you are developing an application and you are dealing with data JSON play an important role at that time. We get the data from the server API or from the database in form of JSON. It is easy to understand the format of the JSON data and we can easily use the data in our app.
We can not use the JSON object directly in our application we need to parse or convert the data in our class object.
So in this blog, we are going to learn how we can convert our JSON data to or class object using the Gson and Jackson library.
Implementation
For the implementation of JSON parsing suppose we have the JSON data file in our assets folder or we are getting the JSON data from the API response in the below format:
1 2 3 4 5 |
{ "success": true, "message": "success", "content": "json data parsing" } |
Pase data using Gson
Gson is a library that maps our JSON data equivalent to our java classes. For parsing the data using Gson add the below dependency in your build.gradle file
1 2 |
// Gson api "com.google.code.gson:gson:2.8.5" |
Now we need to create the model classes for storing our JSON data like below:
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 |
public class MyResponseModel { @SerializedName("success") @Expose public boolean success; @SerializedName("message") @Expose public String message; @SerializedName("content") @Expose public String content; public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } } |
After creating the correct model classes as per the JSON data we can parse the data to our model class.
1 2 3 4 5 6 7 8 9 10 11 |
try { InputStream myJsonFile = getAssets().open("my_json_data.json"); int size = myJsonFile.available(); byte[] buffer = new byte[size]; myJsonFile.read(buffer); myJsonFile.close(); String myJsonData = new String(buffer, "UTF-8"); MyResponseModel jsonResponse = gson.fromJson(myJsonData, MyResponseModel.class); } catch (IOException e) { e.printStackTrace(); } |
We are using the my_json_data.json file stored in our asset folder and with the use of the file Input stream, we are storing the son data on the myJsonData string variable.
Parse data using Jackson
For parsing the data using Jackson add the below dependency in your build.gradle file
1 2 |
//jackson parser implementation 'com.squareup.retrofit2:converter-jackson:2.7.2' |
Create the model classes for parsing the data using Jackson like below
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 |
@JsonInclude(JsonInclude.Include.NON_NULL) @JsonIgnoreProperties(ignoreUnknown = true) public class MyResponseModel { @JsonProperty("success") private String success; @JsonProperty("message") private String message; @JsonProperty("content") private String content; public String getSuccess() { return success; } public void setSuccess(String success) { this.success = success; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } } |
Parse the JSON data using Jackson library like below from the my_json_data.json
1 2 3 4 5 6 7 8 9 10 11 12 |
try { InputStream myJsonFile = getAssets().open("my_json_data.json"); int size = myJsonFile.available(); byte[] buffer = new byte[size]; myJsonFile.read(buffer); myJsonFile.close(); String myJsonData = new String(buffer, "UTF-8"); MyResponseModel jsonResponse = new ObjectMapper().readValue(myJsonData, MyResponseModel.class); } catch (IOException e) { e.printStackTrace(); } |
Jackson uses ObjectMapper to map the JSON data with our java class.
Conclusion:
In this blog, we have learned how to parse JSON using Gson and Jackson Library in android.
For more information regarding the Gson follow the link and for Jackson library follow link.
Thanks for reading this blog. You can also check other blogs from here.