Dedicated Server

Parse json object in android with free source code download

We will be using retrofit,gson for parsing, so go to your app level build.gradle and add the following as dependencies

implementation 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
please add the latest dependency versions for retrofit, converter-gson

Hi, we will be parsing the below json in this tutorial. Check this json here >>

Clearly, the json is of the form {..} and we can parse it as an object from android
Now inorder to generate model class/pojo class for above json, you may either create it automatically using or generate it manually

1) Automatic model class/pojo class generation using

You can make use of jsonschema2pojo to covert json to java object online. This is a really effective tool which even supports various annotation types like jackson, gson, moshi etc.
Specify Target language as Java, Source Type as JSON and annotation style as Gson
  • Now click preview and copy the generated java files
Now copy the contents of the class and add it in android studio project

Now copy the above generated contents into as shown below

public class SampleJson {
    private String title;
    private String description;
    private String name;
    private String id;

    public String getTitle() {
        return title;

    public void setTitle(String title) {
        this.title = title;

    public String getDescription() {
        return description;

    public void setDescription(String description) {
        this.description = description;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getId() {
        return id;

    public void setId(String id) { = id;

private void parseJson() {
        Retrofit retrofit = new Retrofit.Builder()

        RequestInterface request = retrofit.create(RequestInterface.class);
        Call<SampleJson> call1=request.getJson();
        call1.enqueue(new Callback<SampleJson>() {
            public void onResponse(Call<SampleJson> call, Response<SampleJson> response) {

            public void onFailure(Call<SampleJson> call, Throwable t) {


interface RequestInterface {
    Call<SampleJson> getJson();

Download free source code

No comments:

Post a Comment