json-gson 解析泛型及解析null

参考://public static class paraJson {
public class paraJson{
static Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();

// static Gson gson = new Gson();
//解析 Json对象
public static <T> T parseJson(Class<T> type,String jsonStr)throws Exception{
// Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
return gson.fromJson(jsonStr,type);
}
// 将Json数组解析成相应的映射对象列表
public static <T> ArrayList<T> parseJsonArray(Class<T> myClass, String jsonStr)throws Exception{
Type type = new ListParameterizedType(myClass);
return gson.fromJson(jsonStr, type);
}

private static class ListParameterizedType implements ParameterizedType {
private Type type;
private ListParameterizedType(Type type) {
this.type = type;
}
@Override
public Type[] getActualTypeArguments() {
return new Type[] {type};
}
@Override
public Type getRawType() {
return ArrayList.class;
}
@Override
public Type getOwnerType() {
return null;
}
// implement equals method too! (as per javadoc)
}

/**
* @return 不区分类型 传什么解析什么
*/
public static <T> T toObj(String jsonStr, Type type) {
return gson.fromJson(jsonStr, type);
}
}

--------------------------------------------------

json解析有很多工具,这里说的是最常用也是解析速度最快的Gson,Gson是google家出的,有一个缺点就是无法设置null替换,
我们只能手动的批量替换服务器返回的null了,正常的接口定义的时候是绝对不允许服务器返回null的,后台结果却总会出现null!
如果搜索的话有一个常见的答案,
Gson gson = new GsonBuilder().serializeNulls().create();
但是这个却无法解决反序列问题,怎么解决呢?我在stackoverflow上找到了这个问题,亲测有效
http://stackoverflow.com/questions/9483348/gson-treat-null-as-empty-string/24252578#24252578

解决办法如下:

Gson gson  = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
//然后用上面一行写的gson来序列化和反序列化实体类type
gson.fromJson(json, type);
gson.toJson(type);

//NullStringToEmptyAdapterFactory的代码

public class NullStringToEmptyAdapterFactory<T> implements TypeAdapterFactory {
    @SuppressWarnings("unchecked")
    public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
        Class<T> rawType = (Class<T>) type.getRawType();
        if (rawType != String.class) {
            return null;
        }
        return (TypeAdapter<T>) new StringNullAdapter();
    }
}

// StringNullAdapter代码

public class StringNullAdapter extends TypeAdapter<String> {
    @Override
    public String read(JsonReader reader) throws IOException {
        // TODO Auto-generated method stub
        if (reader.peek() == JsonToken.NULL) {
            reader.nextNull();
            return "";
        }
        return reader.nextString();
    }
    @Override
    public void write(JsonWriter writer, String value) throws IOException {
        // TODO Auto-generated method stub
        if (value == null) {
            writer.nullValue();
            return;
        }
        writer.value(value);
    }
}
-------------------------------------------------
参考 http://blog.csdn.net/gl_mine_csdn/article/details/59652313
http://blog.csdn.net/a332324956/article/details/50792120
posted @ 2017-09-16 14:14  manmanlu  阅读(837)  评论(0编辑  收藏  举报