一、使用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文件
{"name":"Tim", "age":"40", "job":"basketball", "hobby": {"name":"sing","time":"1"} }
实体类
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 + "]"; } }
解析代码
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时需注意为空的属性将不会进行转换,切勿理解为会转换为空值。
实体类:
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 + "]"; } }
测试例子:
package com.test.gson; import com.google.gson.Gson; public class Demo { public static void main(String[] args) { Tim tim = new Tim(); tim.setAge("20"); tim.setName("tim"); tim.setJob(null); Gson gson = new Gson(); System.out.println(gson.toJson(tim)); } }
console打印:
{"name":"tim","age":"40"}
因为未设置job属性,转换的json格式数据无此属性。
五、gson转换json若想要体现属性为null或者未设置的属性为null,采用如下方式:
package com.test.gson; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class Demo { public static void main(String[] args) { Tim tim = new Tim(); tim.setAge("20"); tim.setName("tim"); tim.setJob(null); // Gson gson = new Gson(); // System.out.println(gson.toJson(tim)); Gson gson = new GsonBuilder().serializeNulls().create(); System.out.println(gson.toJson(tim)); } }
结果:
{"name":"tim","age":"20","job":null}
六、json的数据格式:
json的数据格式有:字符串、数字、布尔值、对象(包括空值null)、数组,举例如下:
package com.test.gson; import java.util.ArrayList; import java.util.List; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class JsonDemo { public static void main(String[] args) { NormalJson normalJson = new JsonDemo().new NormalJson(); normalJson.setPropertyString("hello world"); normalJson.setPropertyInt(1); normalJson.setPropertyBoolean(true); normalJson.setPropertyObject(new Object()); normalJson.setPropertyNull(null); List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); normalJson.setPropertyArray(list); Gson gson = new GsonBuilder().serializeNulls().create(); System.out.println(gson.toJson(normalJson)); } public class NormalJson { private String propertyString; private int propertyInt; private boolean propertyBoolean; private Object propertyObject; private Object propertyNull; private List<String> propertyArray; public String getPropertyString() { return propertyString; } public void setPropertyString(String propertyString) { this.propertyString = propertyString; } public int getPropertyInt() { return propertyInt; } public void setPropertyInt(int propertyInt) { this.propertyInt = propertyInt; } public boolean isPropertyBoolean() { return propertyBoolean; } public void setPropertyBoolean(boolean propertyBoolean) { this.propertyBoolean = propertyBoolean; } public Object getPropertyObject() { return propertyObject; } public void setPropertyObject(Object propertyObject) { this.propertyObject = propertyObject; } public List<String> getPropertyArray() { return propertyArray; } public void setPropertyArray(List<String> propertyArray) { this.propertyArray = propertyArray; } public Object getPropertyNull() { return propertyNull; } public void setPropertyNull(Object propertyNull) { this.propertyNull = propertyNull; } @Override public String toString() { return "NormalJson [propertyString=" + propertyString + ", propertyInt=" + propertyInt + ", propertyBoolean=" + propertyBoolean + ", propertyObject=" + propertyObject + ", propertyNull=" + propertyNull + ", propertyArray=" + propertyArray + "]"; } } }
运行结果:
{"propertyString":"hello world","propertyInt":1,"propertyBoolean":true,"propertyObject":{},"propertyNull":null,"propertyArray":["a","b"]}
七、gson库解析json为map时对于数字的处理:
解析为map时:
若用object接收value值时,int、long、float、double都会转为double类型;
若用string接收value值时,int、long、float、double都会按原有类型转换;
若用Integer接收value值时,int、long、float、double都会转为intege类型;
若用Double接收value值时,int、long、float、double都会转为double类型。
示例如下:
package com.test.gson; import java.util.Map; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class GsonMapDemo { public static void main(String[] args) { JsonMap jsonMap = new GsonMapDemo().new JsonMap(); jsonMap.setA(1); jsonMap.setB(2); jsonMap.setC(3); jsonMap.setD(4); Gson gson = new Gson(); System.out.println(gson.toJson(jsonMap)); System.out.println(gson.fromJson(gson.toJson(jsonMap), new TypeToken<Map<String, String>>() { }.getType())); System.out.println(gson.fromJson(gson.toJson(jsonMap), new TypeToken<Map<String, Object>>() { }.getType())); System.out.println(gson.fromJson(gson.toJson(jsonMap), new TypeToken<Map<String, Integer>>() { }.getType())); System.out.println(gson.fromJson(gson.toJson(jsonMap), new TypeToken<Map<String, Double>>() { }.getType())); } public class JsonMap { private int a; private long b; private float c; private double d; public int getA() { return a; } public void setA(int a) { this.a = a; } public long getB() { return b; } public void setB(long b) { this.b = b; } public float getC() { return c; } public void setC(float c) { this.c = c; } public double getD() { return d; } public void setD(double d) { this.d = d; } @Override public String toString() { return "JsonMap [a=" + a + ", b=" + b + ", c=" + c + ", d=" + d + "]"; } } }
运行结果如下:
{"a":1,"b":2,"c":3.0,"d":4.0}
{a=1, b=2, c=3.0, d=4.0}
{a=1.0, b=2.0, c=3.0, d=4.0}
{a=1, b=2, c=3, d=4}
{a=1.0, b=2.0, c=3.0, d=4.0}