项目中用到这么两个类,需要变成json对象,使用的gson版本是1.4。
public class SelectItem<T> implements Serializable{
private T value;
private String text;
private String image;
public SelectItem(T value, String text){
this.value = value;
this.text = text;
}
public SelectItem(T value, String text, String image){
this.value = value;
this.text = text;
this.image = image;
}
public int compareTo(Object object){
if (object == null) return 0;
if (object instanceof SelectItem){
@SuppressWarnings("unchecked")
SelectItem<T> o = (SelectItem<T>)object;
return new CompareToBuilder().append(value, o.value).toComparison();
}else{
return 0;
}
}
public T getValue() {
return value;
}
public String getText() {
return text;
}
public String getImage() {
return image;
}
}
public class DoubleSelect<T> extends SelectItem<T>{
private List<SelectItem<?>> subs;
public DoubleSelect(T value, String text){
super(value, text);
}
public DoubleSelect(T value, String text, String image){
super(value, text, image);
}
public <E> DoubleSelect<T> addSubItem(E value, String text){
if (subs == null){
subs = new ArrayList<SelectItem<?>>();
}
subs.add(new SelectItem<E>(value, text));
return this;
}
public <E> DoubleSelect<T> addSubItem(E value, String text, String image){
if (subs == null){
subs = new ArrayList<SelectItem<?>>();
}
subs.add(new SelectItem<E>(value, text, image));
return this;
}
public List<SelectItem<?>> getSubs() {
return subs;
}
}
定义的gson类如下
public class DoubleSelectGson<T> {
public String toJson(List<SelectItem<T>> items){
GsonBuilder gb = new GsonBuilder().serializeNulls();
gb.registerTypeAdapter(DoubleSelect.class, new DoubleSelectSerializer());
Gson gson = gb.create();
return gson.toJson(items);
}
private class DoubleSelectSerializer implements JsonSerializer<DoubleSelect<T>> {
public JsonElement serialize(DoubleSelect<T> src, Type typeOfSrc, JsonSerializationContext context) {
Class<?> c = src.getValue().getClass();
JsonObject rn = new JsonObject();
if (c.equals(String.class)){
rn.addProperty("value", (String)src.getValue());
}else if (c.equals(Integer.class)){
rn.addProperty("value", (Integer)src.getValue());
}else if (c.equals(Long.class)){
rn.addProperty("value", (Long)src.getValue());
}
if (src.getSubs() != null){
Class<?> s = src.getSubs().get(0).getValue().getClass();
SelectItemGson subGson = null;
if (s.equals(String.class)){
subGson = new SelectItemGson<String>();
}else if (s.equals(Integer.class)){
subGson = new SelectItemGson<Integer>();
}else if (s.equals(Long.class)){
subGson = new SelectItemGson<Long>();
}
String json = subGson.toJson(src.getSubs());
rn.addProperty("sub", json.replaceAll("\"", "'"));
}
return rn;
}
}
}
public class SelectItemGson<T> {
public String toJson(List<SelectItem<T>> items){
GsonBuilder gb = new GsonBuilder().serializeNulls();
gb.registerTypeAdapter(SelectItem.class, new SelectItemSerializer());
Gson gson = gb.create();
return gson.toJson(items);
}
public class SelectItemSerializer implements JsonSerializer<SelectItem<T>> {
public JsonElement serialize(SelectItem<T> src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject rn = new JsonObject();
Class<?> c = src.getValue().getClass();
if (c.equals(String.class)){
rn.addProperty("value", (String)src.getValue());
}else if (c.equals(Integer.class)){
rn.addProperty("value", (Integer)src.getValue());
}else if (c.equals(Long.class)){
rn.addProperty("value", (Long)src.getValue());
}
rn.addProperty("text", src.getText());
rn.addProperty("image", src.getImage());
return rn;
}
}
}
调用方法是
new DoubleSelectGson<Integer>().toJson(ConstantUtil.allGenders);
allGenders就是一个List<SelectItem<Integer>>的对象。
得到的返回值是
[{"value":1,"sub":"[{\u0027value\u0027:\u0027shaver\u0027,\u0027text\u0027:\u0027剃须刀\u0027,\u0027image\u0027:null},{\u0027value\u0027:\u0027neckcloth\u0027,\u0027text\u0027:\u0027领带\u0027,\u0027image\u0027:null}]"},{"value":2,"sub":"[{\u0027value\u0027:\u0027skirt\u0027,\u0027text\u0027:\u0027裙子\u0027,\u0027image\u0027:null},{\u0027value\u0027:\u0027high-heel shoe\u0027,\u0027text\u0027:\u0027高跟鞋\u0027,\u0027image\u0027:null}]"}]
分享到:
相关推荐
gson 解析泛型和将泛型转为json字符串
Gson解析json,实现泛型解析数据,根据用户传入的类型,解析称用户需要的,Common \CommonList 里面的status message data相关的约定可以更具开发者与服务器的约定修改
Json转换利器Gson之实例一-简单对象转化和带泛型的List转化 (http://blog.csdn.net/lk_blog/article/details/7685169) Json转换利器Gson之实例二-Gson注解和GsonBuilder ...
使用Gson解析Json和Json数组,通俗易懂
先说一下遇到的问题:通过使用GSON泛型进行报文转换的时候想要对部分关键字段加密,发现在封装好的方法中,对个别字段的加密满足不了。 解决过程:首先通过反射获取到bean下的对象名称。 对象名称获取到了之后需要...
之前在Volley框架JsonObjectRequest和JsonArrayRequest的使用中搭配了Gson库,操作JSON数据格式非常方便,所以这里详细阐述下Gson的使用方法。
json字符串实体bean或者List互转(gson和jackson,支持泛型),支持json的格式化,所需要的包在代码也有说明。还有少的可以告知我,谢谢
Json解析工具类完善一下,使用GSON+JsonPath+泛型来提高自己写JSON解析的效率 http://blog.csdn.net/b275518834/article/details/49819831
Gson实例解析Json。
解放你的双手,不用再去一个一个JSONObject去处理解析问题,两行代码搞定一切
android Gson使用实例 注释详细
GsonDemo是利用第三方库Gson库来是实现json的解析和反转解析
Google Gson解析Json数据应用实例
1.实现get,post请求 2.支持上传,下载功能 3.支持Gson泛型解析json数据 4.使用mvc设计模式 5.支持自定义exception 6.支持自定义controller 7.封装glide图片库使用
介绍:Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. 压缩包里有以下文件: ...
om.google.code.gson/gson/2.6.2 Gson maven依赖 java 开发包
VolleyLib框架是一个集合Volley 网络通讯库, Gson解析库 ,org.apache.http部分源码的定制化网络框架 , 具备文件上传、文件下载、Form表单、Json数据协议、Gson 解析Json生成泛型实体等功能。 在实际开发中,网络...
Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库。可以将一个 JSON 字符串转成一个 Java 对象,或者反过来。 java对象或者list转换为json字符串
包含以下java源文件: com.google.gson.DefaultDateTypeAdapter.class com.google.gson.ExclusionStrategy.class com.google.gson.FieldAttributes.class com.google.gson.FieldNamingPolicy.class ...
GSON简介 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。 Gson是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库。可以将一个JSON字符串...