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.

2
  • Make model from json and parse simply using GSON. Commented Feb 16, 2017 at 12:55
  • Use this site to create POJO classe jsonschema2pojo.org Commented Feb 16, 2017 at 13:00

4 Answers 4

1

Simple!

JSONObject jsonObj = new JSONObject(yourStringHere).optJSONObject("Data");
JSONArray jsonDetail = jsonObj.optJSONArray("Details");
JSONArray jsonOther = jsonObj.optJSONArray("Other");
Sign up to request clarification or add additional context in comments.

1 Comment

This is not Gson.
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;
    }

}

Comments

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

Comments

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();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.