参考(不限于):http://www.jianshu.com/p/e740196225a4
@SerializedName(value = "emailAddress", alternate = {"email", "email_address"})
public String emailAddress;
//方法声明
public <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException { Object object = fromJson(json, (Type) classOfT); return Primitives.wrap(classOfT).cast(object);}
Gson gson = new Gson();
String jsonArray = "[\"Android\",\"Java\",\"PHP\"]";
String[] strings = gson.fromJson(jsonArray, String[].class);
//错误写法
List<String> strings =gson.fromJson(jsonArray, String[].class);
//正确写法
List<String> stringList = gson.fromJson(jsonArray, new TypeToken<List<String>>() {}.getType());
//原设计
public class UserResult {
public int code;
public String message;
public User data;
}
public class UserListResult {
public int code;
public String message;
public List<User> data;
}
String json = "{..........}";
Gson gson = new Gson();
UserResult userResult = gson.fromJson(json,UserResult.class);
User user = userResult.data;UserListResult userListResult = gson.fromJson(json,UserListResult.class);
List<User> users = userListResult.data;
//新设计
public class Result<T> {
public int code;
public String message;
public T data;
}
Type userType = new TypeToken<Result<User>>(){}.getType();
Result<User> userResult = gson.fromJson(json,userType);User user = userResult.data;
Type userListType = new TypeToken<Result<List<User>>>(){}.getType();
Result<List<User>> userListResult = gson.fromJson(json,userType);
List<User> users = userListResult.data;
Gson gson = new GsonBuilder()
//序列化null
.serializeNulls()
// 设置日期时间格式,另有2个重载方法
// 在序列化和反序化时均生效
.setDateFormat("yyyy-MM-dd")
// 禁此序列化内部类
.disableInnerClassSerialization()
//生成不可执行的Json(多了 )]}' 这4个字符)
.generateNonExecutableJson()
//禁止转义html标签
.disableHtmlEscaping()
//格式化输出
.setPrettyPrinting()
.create();
public class Category {
@Expose
public int id;
@Expose
public String name;
@Expose
public List<Category> children;
//不需要序列化,所以不加 @Expose 注解,
//等价于 @Expose(deserialize = false,serialize = false)
public Category parent;
}
Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.create();
gson.toJson(category);
Gson gson = new GsonBuilder()
.addSerializationExclusionStrategy(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
// 这里作判断,决定要不要排除该字段,return true为排除
if ("finalField".equals(f.getName())) return true;
//按字段名排除
Expose expose = f.getAnnotation(Expose.class);
if (expose != null && expose.deserialize() == false)
return true;
//按注解排除
return false;
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
// 直接排除某个类 ,return true为排除
return (clazz == int.class || clazz == Integer.class); }
})
.create();
//原json
{
"timestamp": 1449199363.89387,
"message": "Succeed",
"version": "1.0",
"results": [
{
"data": {
"recommended": 0,
"video_type": 2,
"vid": "23482078",
"title": "破风",
"movie_title": "破风",
"duration": "126",
"is_voted": 0,
"image": "http://i1.letvimg.com/lc02_isvrs/201509/11/16/21/a4bdc7ff-6d69-4875-bff8-dbe136a533ff/thumb/2_960_540.jpg",
"playcount": 3303475,
"share_link": "http://leying.duohappy.cn",
"desc": "无畏“破风手”传递正能量",
"code_rate": [
9,
13,
21,
22,
51
],
"is_downloadable": 0
},
"type": "video"
},
{
"data": {
"url": "http://www.baidu.com/",
"image": "http://i0.letvimg.com/lc02_isvrs/201506/11/16/26/dbbebacb-12f5-4356-a364-c9a888b884ba/thumb/2_1440_810.jpg",
"desc": "测试跳转描述",
"recommended": 1,
"title": "测试跳转"
},
"type": "link"
}
]
}
问题点是这个data数据,由于返回data数据,受到type的影响。无法固定List 里的泛型T,只能用Object来代替。实际运行List里面的object,可能是video,也可能是link。需要自己定义json的反序列化过程。即实现JsonDeserializer接口,代码如下。
public class VideoList {
public float timestamp;
public String message;
public String version;
public List<Object> results;
}
public class VideoBean {
}
public class ChooseH5Bean {
}
public class VideoListJsonDeserializer implements JsonDeserializer<VideoList> {
@Override
public VideoList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
VideoList videoList = new VideoList();
if (json.isJsonObject()) {
JsonObject jsonObject = json.getAsJsonObject();
videoList.timestamp = jsonObject.get("timestamp").getAsFloat();
videoList.message = jsonObject.get("message").getAsString();
videoList.version = jsonObject.get("version").getAsString();
JsonArray array = (JsonArray) jsonObject.get("results");
List<Object> list = new ArrayList<Object>();
for (int i = 0; i < array.size(); i++) {
JsonObject element = (JsonObject) array.get(i);
String type = element.get("type").getAsString();
Object item = null;
if ("video".equals(type)) {
item = context.deserialize(element.getAsJsonObject("data"), Video.class);
} else if ("link".equals(type)) {
item = context.deserialize(element.getAsJsonObject("data"), Link.class);
}
if (item != null) {
list.add(item);
}
}
videoList.results = list;
}
return videoList;
}
}
Gson gson = new GsonBuilder().registerTypeAdapter(VideoList.class, new VideoListJsonDeserializer()).create();
InputStream inputStream = getResources().openRawResource(R.raw.text);
VideoList videoList = gson.fromJson(new InputStreamReader(inputStream), VideoList.class);