1

How to parse multiple JSON arrays inside a JSON object using Gson?

{
    "id": 1,
    "Data": {
        "Details": [{
            "Code": "1",
            "Name": "John"
        }, {
            "Code": "2",
            "Name": "Peter"
        }],
        "Other": [{
            "age": "56",
            "gender": "M"
        }, {
            "age": "66",
            "gender": "M"
        }]
    },
    "message": "SUCCESS"
}

Any help would be appreciated.

Lyubomyr Shaydariv
  • 20,327
  • 12
  • 64
  • 105
user3210013
  • 47
  • 1
  • 3

4 Answers4

1

Simple!

JSONObject jsonObj = new JSONObject(yourStringHere).optJSONObject("Data");
JSONArray jsonDetail = jsonObj.optJSONArray("Details");
JSONArray jsonOther = jsonObj.optJSONArray("Other");
Mayura Devani
  • 440
  • 3
  • 17
1

You can make POJO classes to pass in Gson for JSON parsing

Gson gson = new Gson();
Detail details = gson.fromJson(response, Detail .class);

com.example.Data.java

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Data {

    @SerializedName("Details")
    @Expose
    private List<Detail> details = null;

    @SerializedName("Other")
    @Expose
    private List<Other> other = null;

    public List<Detail> getDetails() {
        return details;
    }

    public void setDetails(List<Detail> details) {
        this.details = details;
    }

    public List<Other> getOther() {
        return other;
    }

    public void setOther(List<Other> other) {
        this.other = other;
    }

}

com.example.Detail.java

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Detail {

    @SerializedName("Code")
    @Expose
    private String code;

    @SerializedName("Name")
    @Expose
    private String name;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

com.example.Example.java

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

    @SerializedName("id")
    @Expose
    private Integer id;

    @SerializedName("Data")
    @Expose
    private Data data;

    @SerializedName("message")
    @Expose
    private String message;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

com.example.Other.java

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Other {

    @SerializedName("age")
    @Expose
    private String age;

    @SerializedName("gender")
    @Expose
    private String gender;

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

}
Lyubomyr Shaydariv
  • 20,327
  • 12
  • 64
  • 105
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
0

GO to below link and create Pojo classes.

http://pojo.sodhanalibrary.com/

Once created use gson to get data from json

YourPojoClass obj = new Gson().fromJson(jsonResponse, YourPojoClass.class);

Try this and update answer

Pratik
  • 452
  • 4
  • 17
0

Create simple POJO class..

public class JsonResponse{
    int id;
    DataResponse data;
    String message;

    //setter and getter
}

public class DataResponse{
     List<DetailsResponse> Details;
     List<OthersResponse> Other;

     //setter and getter
}

public class DetailsResponse{
     String Code;
     String Name;

     //setter and getter
}

public class OthersResponse{
     String age;
     String gender;

     //setter and getter
}

Finally,

JsonResponse data = new Gson().fromJson(YOUR_JSON,JsonResponse.class);
//how to use
int id = data.getId();
List<DetailsResponse> tt = data.getData().getDetails();
List<OthersResponse> to = data.getData().getOthers();
ZeroOne
  • 8,996
  • 4
  • 27
  • 45