thirtvanke
thirtvanke

Reputation: 39

How to use gson and volley parse nested json?

Here is the json data I want to use:

{
     "name" : "Ravi Tamada", 
     "email" : "[email protected]",
     "phone" : 
      {
         "home" : "08947 000000",
         "mobile" : "9999999999"
      }

}

Here is my JsonObjectRequest:

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, APITEST,null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject jsonObject) {

            Gson gson = new Gson();
            People people;
            people = gson.fromJson(jsonObject.toString(),People.class);
            tv_city.setText(""+people.email);

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {



        }
    });

It's ok with tv_city.setText(""+people.email())...


Here is my javabean class:

public class People {

    public String name ;
    public String email;

    public class Phone{
        public String home;
        public String mobile;

    }

}

Now I want to get "home" number,how?

Upvotes: 1

Views: 5597

Answers (4)

Adarsh Yadav
Adarsh Yadav

Reputation: 3770

1- You have to update your bean class as follows :-

public class People implements Serializable {
private String name ;
private String email;
private Phone phone;

 public Phone getPhone () {
    return phone;
}

public void setPhone (Phone phone) {
    this.phone = phone;
}
public String getName () {
    return name;
}

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

public String getEmail () {
    return email;
}

public void setEmail (String email) {
    this.email = email;
}}

2- Create a new bean class Phone.java :-

public class Phone implements Serializable{
private String home;

public String getMobile () {
    return mobile;
}

public void setMobile (String mobile) {
    this.mobile = mobile;
}

public String getHome () {
    return home;
}

public void setHome (String home) {
    this.home = home;
}

private String mobile;}

3- Now update your code as follows:-

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, APITEST,null, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(JSONObject jsonObject) {

        Gson gson = new Gson();
        People people;
        people = gson.fromJson(jsonObject.toString(),People.class);
        tv_city.setText(""+people.email);
        //for getting Home & Mobile number
          String home=people.getPhone.getHome();
          String mobile=people.getPhone.getMobile();

    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError volleyError) {



    }
});

Note:- My above bean is as per expected api response in your question. But if you have nested objects then you have to choose either List<Phone> or ArrayList<Phone> inside in your People bean and then create its getters and setters.

Hope this will help you !!!

Upvotes: 8

Aniruddha K.M
Aniruddha K.M

Reputation: 7511

you can also get the data directly from the json object like this

if(JsonObject!=null){
String email=JsonObject.getString("email");
}  

OR
To make it work write getters() and setters() in your model object(person object) you can auto generate it too . once you do that get the data like this

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, APITEST,null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject jsonObject) {

            Gson gson = new Gson();
            People people;
            people = gson.fromJson(jsonObject.toString(),People.class);
            tv_city.setText(""+people.getEmail());

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {



        }
    });

Upvotes: 1

Newbiee
Newbiee

Reputation: 592

Replace your JavaBean class with

public class People {

@SerializedName("name")
@Expose
private String name;
@SerializedName("email")
@Expose
private String email;
@SerializedName("phone")
@Expose

private Phone phone;

/**
* 
* @return
* The name
*/
public String getName() {
return name;
}

/**
* 
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}

/**
* 
* @return
* The email
*/
public String getEmail() {
return email;
}

/**
* 
* @param email
* The email
*/
public void setEmail(String email) {
this.email = email;
}

/**
* 
* @return
* The phone
*/
public Phone getPhone() {
return phone;
}

/**
* 
* @param phone
* The phone
*/
public void setPhone(Phone phone) {
this.phone = phone;
}

}

And

public class Phone {

@SerializedName("home")
@Expose
private String home;
@SerializedName("mobile")
@Expose
private String mobile;

/**
* 
* @return
* The home
*/
public String getHome() {
return home;
}

/**
* 
* @param home
* The home
*/
public void setHome(String home) {
this.home = home;
}

/**
* 
* @return
* The mobile
*/
public String getMobile() {
return mobile;
}

/**
* 
* @param mobile
* The mobile
*/
public void setMobile(String mobile) {
this.mobile = mobile;
}

}

and then you can make call in your JsonResponse like

JSONObject phone=jsonObject.getJSONObject("phone");

String home=phone.getHome();

will return you the home Number.

Hope it helps :)

Upvotes: 1

Hari Krishnan
Hari Krishnan

Reputation: 6302

JSONObject phone=jsonObject.getJSONObject("phone");

String home=phone.getString("home");

Now you have Home Phone number in home string.

Upvotes: 0

Related Questions