如何使用gson和volley解析嵌套的json? [英] How to use gson and volley parse nested json?
问题描述
{
name:Ravi Tamada ,
email:ravi8x@gmail.com,
电话:
{
home:08947 000000,
mobile :9999999999
}
}
$ b $
$ b
这是我的JsonObjectRequest: ,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(+ people.email())... p>
这是我的javabean类:
public class People {
public String name;
public String email;
公共类电话{
public String home;
public String mobile;
}
}
现在我想获得家庭号码,怎么样?
更新你的bean类,如下所示: - public class People实现Serializable {
private String name;
私人字符串电子邮件;
私人电话电话;
公用电话getPhone(){
返回电话;
}
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-现在更新您的代码,如下所示:
$ b $ pre $ 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);
//获取Home&手机号码
String home = people.getPhone.getHome();
String mobile = people.getPhone.getMobile();
},new Response。 ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError){
$ b}
});
注意: - 我的上面的bean是按照您的问题预期的api响应。但是如果你有嵌套的对象,那么你必须在你的内部选择 List< Phone>
或 ArrayList< Phone>
People bean,然后创建它的getter和setter。
希望这对你有帮助!!!
Here is the json data I want to use:
{
"name" : "Ravi Tamada",
"email" : "ravi8x@gmail.com",
"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?
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 !!!
这篇关于如何使用gson和volley解析嵌套的json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!