欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

解决:Gson解析java实体类字段中包含json或其他值报错

程序员文章站 2024-03-16 09:18:34
...
1、String转成List<T>方法:
public static <T> List<T> parseJsonToList(String jsonData, Class<T> myClass) {
    Gson gson = new Gson();
    Type type = new ParameterizedTypeImpl(myClass);
    List<T> array = gson.fromJson(jsonData, type);
    return array;
}

2、调用:

List<User> mrUserList = JsonUtils.parseJsonToList(map.get("UserList") + "", User.class);

3、User类

public class User {
    private Integer id; 
    private String extStr;
    public String getExtStr() {        
        return extStr;
    }

    public void setExtStr(String extStr) {
        this.extStr = extStr;
    }

}

4、当extStr为json值,比如:{"vip_id":"0","user_nick":"123"}

会报:

-com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 1637 path $[0].extStr
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
	at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
	at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
	at com.google.gson.Gson.fromJson(Gson.java:927)
	at com.google.gson.Gson.fromJson(Gson.java:892)
	at com.google.gson.Gson.fromJson(Gson.java:841)

我刚开始将extStr改为Object,

public class User {
    private Integer id; 
    private String extStr;
    public Object getExtStr() {        
        return extStr;
    }

    public void setExtStr(Object extStr) {
        this.extStr = extStr;
    }

}

当插入到数据库的时候,

<insert id="uploadMrTroop" parameterType="java.util.HashMap" >
     insert into user(id, extStr)
     values
        <foreach collection="mrUserList" item="item" separator=",">
             (#{item.id,jdbcType=INTEGER},#{item.extStr,jdbcType=LONGVARCHAR})
        </foreach>
</insert>

会报

Caused by: java.sql.SQLException: Incorrect string value: '\xAC\xED\x00\x05sr...' for column 'extStr' at row 1
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)

5、最终解决方法:

方法一:

extStr改为Object,在插入数据库之前,先遍历一遍List,将extStr重新放入

Iterator<User> mrUserListIterator = mrUserList.iterator();
        while (mrUserListIterator.hasNext()) {
            User userNew = mrUserListIterator.next();
            userNew.setExtStr(userNew.getExtStr+"");        
        }

方法二:

mybatis中insert的时候,实际是获取的实体类中的get方法

通过debug,发现extStr的类型为LinkedTreeMap,修改get方法

public Object getExtStr() {
        if(extStr instanceof LinkedTreeMap){

            LinkedTreeMap tm = (LinkedTreeMap) extStr;
            JSONObject jsonObject = new JSONObject();
            Iterator it = tm.keySet().iterator();
            while (it.hasNext()) {
                String key = (String) it.next();
                String value = (String) tm.get(key);
                jsonObject.put(key,value);
            }
            return jsonObject.toString();
        }else{
           return extStr;
        }
    }