这是我想要使用的json数据:
{
"name" : "Ravi Tamada",
"email" : "ravi8x@gmail.com",
"phone" :
{
"home" : "08947 000000",
"mobile" : "9999999999"
}
}
这是我的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) {
}
});
可以使用tv_city.setText(&#34;&#34; + people.email())...
这是我的javabean类:
public class People {
public String name ;
public String email;
public class Phone{
public String home;
public String mobile;
}
}
现在我想得到&#34; home&#34;数,如何?
答案 0 :(得分:6)
1-您必须按如下方式更新您的bean类: -
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-创建一个新的bean类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-现在按如下方式更新您的代码: -
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) {
}
});
注意: - 我的上面的bean是你问题中预期的api响应。但是如果您有嵌套对象,则必须在People bean中选择List<Phone>
或ArrayList<Phone>
,然后创建其getter和setter。
希望这会对你有帮助!!!
答案 1 :(得分:1)
您也可以直接从json对象获取数据,如
if(JsonObject!=null){
String email=JsonObject.getString("email");
}
或强>
要使其工作在模型对象(人物对象)中编写getters()和setters(),您也可以自动生成它。
一旦你这样做了这样的数据
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) {
}
});
答案 2 :(得分:1)
用
替换您的JavaBean类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;
}
}
并且
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;
}
}
然后你可以在你的JsonResponse中调用
JSONObject phone=jsonObject.getJSONObject("phone");
String home=phone.getHome();
将返回主页号码。
希望有所帮助:)
答案 3 :(得分:0)
JSONObject phone=jsonObject.getJSONObject("phone");
String home=phone.getString("home");
现在家庭电话号码中有家庭电话号码。