日期:2014-05-20  浏览次数:20839 次

求教json的问题,google库gson的bug问题!
Java code

package toonyoo.ums.test;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.google.gson.annotations.Since;

public class User {   
    
    public User(String string, String string2, String string3, String string4) {
        // TODO Auto-generated constructor stub
        this.username=string;
        this.password=string2;
        this.gender=string3;
        this.sex=string4;
    }
    @SerializedName("pwd")  
    private String password;   
    @Expose  
    @SerializedName("uname")  
    private String username;   
    @Expose  
    @Since(1.1)  
    private String gender;   
    @Expose  
    @Since(1.0)  
    private String sex;
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }  

}  

调用:
        List<User> userList = new ArrayList<User>();  
        User jack = new User("123456","Jack", "Male","Female");   
        User marry = new User("888888","Marry", "Female","Male");   
        userList.add(jack);   
        userList.add(marry);   
        Gson son=new Gson();
        Type targetType = new TypeToken<List<User>>() {}.getType();  

        String str=son.toJson(userList, targetType);
        /*[{"pwd":"Jack","uname":"123456","gender":"Male","sex":"Female"},{"pwd":"Marry","uname":"888888","gender":"Female","sex":"Male"}]*/
        users =son.fromJson(str,targetType);///转换失败,提示下面的错误





Exception in thread "main" com.google.gson.JsonParseException: The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@36527f failed to deserialized json object [{"pwd":"Jack","uname":"123456","gender":"Male","sex":"Female"},{"pwd":"Marry","uname":"888888","gender":"Female","sex":"Male"}] given the type java.util.List<toonyoo.ums.test.User>
at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:63)
at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
at com.google.gson.JsonDeserializationVisitor.visitUsingCustomHandler(JsonDeserializationVisitor.java:76)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:106)
at com.google.gson.JsonDeserializationContextDefault.fromJsonArray(JsonDeserializationContextDefault.java:64)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:49)
at com.google.gson.Gson.fromJson(Gson.java:568)
at com.google.gson.Gson.fromJson(Gson.java:515)
at com.google.gson.Gson.fromJson(Gson.java:484)
at com.google.gson.Gson.fromJson(Gson.java:434)
at toonyoo.ums.test.JSONTest.jsonToObject(JSONTest.java:85)
at com.gsontest.Startup.main(Startup.java:11)
Caused by: java.lang.RuntimeException: No-args constructor for class toonyoo.ums.test.User does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
at com.google.gson.MappedObjectConstructor.constructWithNoArgConstructor(MappedObjectConstructor.java:64)