一、使用gson解析json数据时,如果出现某一个属性为null的情况(且并无报错信息),首先检查json文件的属性名称和实体类的属性名称是否一致。
举例如下(正常情况,此处读取本地的一个json文件):
json文件
{"name":"Tim", "age":"40", "job":"basketball", "hobby":"dai"}
解析代码如下
String jsonstring=FileUtil.readAssets(ctx,"tim.json");
Gson g=new Gson();
tim=g.fromJson(jsonstring, Tim.class);
实体类如下
package com.example.jsondemoerror;
public class Tim {
private String name;
private String age;
private String job;
private String hobby;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "Tim [name=" + name + ", age=" + age + ", job=" + job
+ ", hobby=" + hobby + "]";
}
}
json文件的各属性名称和实体类的名称一致时,解析成功。
若出现名称不一致:
1、json文件的名称写错,实体类未错,比如hobby写为hobb,出现Tim [name=Tim, age=40, job=basketball, hobby=null]。
2、实体类属性名称写错,json未错,比如把hobby写为hobb,出现Tim [name=Tim, age=40, job=basketball, hobb=null]。
二、使用gson可以直接解析出实体类的集合。
json文件
{"name":"Tim", "age":"40", "job":"basketball", "hobby": [{"name":"sing","time":"1"}, {"name":"dance","time":"1"}, {"name":"swim","time":"1"}, {"name":"dai","time":"1"}]}
实体类如下
package com.example.jsondemoerror; import java.util.List; public class Tims { private String name; private String age; private String job; private List<Hobby> hobby; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public List<Hobby> getHobby() { return hobby; } public void setHobby(List<Hobby> hobby) { this.hobby = hobby; } @Override public String toString() { return "Tims [name=" + name + ", age=" + age + ", job=" + job + ", hobby=" + hobby + "]"; } }
解析代码
String jsonstring=FileUtil.readAssets(ctx,"tims.json"); Gson g=new Gson(); tims=g.fromJson(jsonstring, Tims.class); Log.d("JsonErrorActivity", tims.toString()+tims.getHobby().get(0).getName());
log内容
Tims [name=Tim, age=40, job=basketball, hobby=[Hobby [name=sing, time=1], Hobby [name=dance, time=1], Hobby [name=swim, time=1], Hobby [name=dai, time=1]]]sing
三、使用gson可以解析出实体类。
json文件
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
{"name":"Tim", "age":"40", "job":"basketball", "hobby": {"name":"sing","time":"1"} }
实体类
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.example.jsondemoerror; public class Timsh { private String name; private String age; private String job; private Hobby hobby; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Hobby getHobby() { return hobby; } public void setHobby(Hobby hobby) { this.hobby = hobby; } @Override public String toString() { return "Timsh [name=" + name + ", age=" + age + ", job=" + job + ", hobby=" + hobby + "]"; } }
解析代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
String jsonstring=FileUtil.readAssets(ctx,"timsh.json"); Gson g=new Gson(); timsh=g.fromJson(jsonstring, Timsh.class); Log.d("JsonErrorActivity", timsh.toString()+timsh.getHobby().getName());
log内容
Timsh [name=Tim, age=40, job=basketball, hobby=Hobby [name=sing, time=1]]sing
四、使用gson转对象为json时需注意为空的属性将不会进行转换,切勿理解为会转换为空值。
实体类:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package gson; public class Tim { private String name; private String age; private String job; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } @Override public String toString() { return "Tim [name=" + name + ", age=" + age + ", job=" + job + "]"; } }
测试例子:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package gson; public class Tim { private String name; private String age; private String job; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } @Override public String toString() { return "Tim [name=" + name + ", age=" + age + ", job=" + job + "]"; } }
console打印:
{"name":"tim","age":"40"}
因为未设置job属性,转换的json格式数据无此属性。