Skip to content Skip to sidebar Skip to footer

How To Use Gson And Volley Parse Nested Json?

Here is the json data I want to use: { 'name' : 'Ravi Tamada', 'email' : 'ravi8x@gmail.com', 'phone' : { 'home' : '08947 000000', 'mobile'

Solution 1:

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

publicclassPeopleimplementsSerializable {
private String name ;
private String email;
private Phone phone;

 public Phone getPhone () {
    return phone;
}

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

publicvoidsetName (String name) {
    this.name = name;
}

public String getEmail () {
    return email;
}

publicvoidsetEmail (String email) {
    this.email = email;
}}

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

publicclassPhoneimplementsSerializable{
privateString home;

publicString getMobile () {
    return mobile;
}

publicvoid setMobile (String mobile) {
    this.mobile = mobile;
}

publicString getHome () {
    return home;
}

publicvoid setHome (String home) {
    this.home = home;
}

privateString mobile;}

3- Now update your code as follows:-

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

    @OverridepublicvoidonResponse(JSONObject jsonObject) {

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

    }
}, newResponse.ErrorListener() {
    @OverridepublicvoidonErrorResponse(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 !!!

Solution 2:

Replace your JavaBean class with

publicclassPeople {

@SerializedName("name")
@ExposeprivateString name;
@SerializedName("email")
@ExposeprivateString email;
@SerializedName("phone")
@ExposeprivatePhone phone;

/**
* 
* @return
* The name
*/publicStringgetName() {
return name;
}

/**
* 
* @paramname
* The name
*/publicvoidsetName(String name) {
this.name = name;
}

/**
* 
* @return
* The email
*/publicStringgetEmail() {
return email;
}

/**
* 
* @paramemail
* The email
*/publicvoidsetEmail(String email) {
this.email = email;
}

/**
* 
* @return
* The phone
*/publicPhonegetPhone() {
return phone;
}

/**
* 
* @paramphone
* The phone
*/publicvoidsetPhone(Phone phone) {
this.phone = phone;
}

}

And

publicclassPhone {

@SerializedName("home")
@ExposeprivateString home;
@SerializedName("mobile")
@ExposeprivateString mobile;

/**
* 
* @return
* The home
*/publicStringgetHome() {
return home;
}

/**
* 
* @paramhome
* The home
*/publicvoidsetHome(String home) {
this.home = home;
}

/**
* 
* @return
* The mobile
*/publicStringgetMobile() {
return mobile;
}

/**
* 
* @parammobile
* The mobile
*/publicvoidsetMobile(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 :)

Solution 3:

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 = newJsonObjectRequest(Request.Method.GET, APITEST,null, newResponse.Listener<JSONObject>() {

        @OverridepublicvoidonResponse(JSONObject jsonObject) {

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

        }
    }, newResponse.ErrorListener() {
        @OverridepublicvoidonErrorResponse(VolleyError volleyError) {



        }
    });

Solution 4:

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

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

Now you have Home Phone number in home string.

Post a Comment for "How To Use Gson And Volley Parse Nested Json?"