Gson转换同父类不同子类列表
来源:互联网 发布:mac网桥未连接 编辑:程序博客网 时间:2024/06/05 11:54
import com.google.gson.*;import com.google.gson.reflect.TypeToken;import java.lang.reflect.Type;import java.util.ArrayList;import java.util.List;public class Test { public static void main(String[] args) { String resultStr = "{'code':'00','personList':[{\"id\":\"123456\",\"product_category\":\"101\",\"name\":\"xiaofeifei\",\"age\":\"25\",\"ranking\":\"0\"},{\"id\":\"123456\",\"product_category\":\"201\",\"name\":\"xiaofeifei\",\"age\":\"25\",\"ranking\":\"1\"}]}"; Gson gson = new GsonBuilder().registerTypeAdapter(RANKING.class, new JsonDeserializer<RANKING>() { @Override public RANKING deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { int ranking = json.getAsJsonPrimitive().getAsInt(); return RANKING.values()[ranking]; } }).registerTypeAdapter(new TypeToken<List<BasePerson>>(){}.getType(), new JsonDeserializer<List<BasePerson>>() { @Override public List<BasePerson> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { List list = new ArrayList<BasePerson>(); JsonArray ja = json.getAsJsonArray(); for (JsonElement je : ja) { String type = je.getAsJsonObject().get("product_category").getAsString(); list.add(context.deserialize(je, PersonType.getByProductCategory(type).getType())); } return list; } }).create(); ResultEntity result = gson.fromJson(resultStr,ResultEntity.class); }}class BasePerson{ private Long id; private String product_category; private RANKING ranking; public RANKING getRanking() { return ranking; } public void setRanking(RANKING ranking) { this.ranking = ranking; } public String getProduct_category() { return product_category; } public void setProduct_category(String product_category) { this.product_category = product_category; } public Long getId() { return id; } public void setId(Long id) { this.id = id; }}class PersonWithName extends BasePerson { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; }}class PersonWithAge extends BasePerson{ private Integer age; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}enum PersonType { WITHNAME("名称", PersonWithName.class, "101"), WITHAGE("年龄", PersonWithAge.class, "201"); private String displayName; private Class<? extends BasePerson> type; private String product_category; public String getDisplayName() { return displayName; } public void setDisplayName(String displayName) { this.displayName = displayName; } public Class<? extends BasePerson> getType() { return type; } public void setType(Class<? extends BasePerson> type) { this.type = type; } public String getProduct_category() { return product_category; } public void setProduct_category(String product_category) { this.product_category = product_category; } private PersonType(String displayName, Class<? extends BasePerson> type , String product_category) { this.displayName = displayName; this.type = type; this.product_category = product_category; } public static PersonType getByProductCategory(String category){ for(PersonType t : values()){ if(t.product_category.equals(category)){ return t; } } return null; }}enum RANKING { TOP, BOTTOM;}class ResultEntity{ String code; List<BasePerson> personList; public List<BasePerson> getPersonList() { return personList; } public void setPersonList(List<BasePerson> personList) { this.personList = personList; } public String getCode() { return code; } public void setCode(String code) { this.code = code; }}
0 0
- Gson转换同父类不同子类列表
- Gson自定义转换器转换成不同的子类
- JAVA——编译运行含有包层的类、同包和不同包类的相互访问、不同包子类和父类的访问权限
- dynamic_cast子类转换父类
- java 父类、子类 类型转换【父类不能转为子类】
- Gson 转换工具类
- 父类与子类的转换条件
- 父类与子类之间的转换
- 父类和子类转换浅谈篇
- ABAP CLASS父类子类转换
- C#父类强制转换子类
- C# 子类向父类转换 测试
- java 子类与父类 转换
- Java子类与父类的转换
- 父类与子类之间的转换
- c++子类父类的转换关系
- java 父类与子类的转换
- Java子类与父类的转换
- Mysql5 实现交叉表查询
- Git碎片~~
- 使用npm升级node js
- HTML5移动端touch滑动与重力/方向感应结合
- Android字符串资源及其格式化
- Gson转换同父类不同子类列表
- stoi()和to_string()
- 为什么pthread_cond_wait需要传递mutex参数
- Servlet知识笔记
- Android开发艺术2之Activity的启动模式
- Hibernate Criteria的 Criterion,Projection,Restrictions等条件设置
- android 传参 StringRequest 线程 实现页面页面停留
- 跨线程操作窗体
- canvas雪景