使用Jackson进行反序列化时无法识别的字段错误

时间:2014-08-06 20:10:11

标签: java json deserialization

我在将JSON反序列化为我的POJO对象时遇到了问题。

这是我的JSON

{"userID":"346","userMemID":"10","userFirstName":"FIRSTNAME","userLastName":"LASTNAME","userSkillLevel":"7","userProviderID":"098"}

这是我的班级

public class Drivers {

private String userID = "";
private String userMemID = "";
private String userFirstName = "";
private String userLastName = "";
private String userSkillLevel = "";
private String userProviderID = "";


public String getUserID() {
    return userID;
}

public void setUserID(String userID) {
    this.userID = userID;
}

public String getUserMemID() {
    return userMemID;
}

public void setUserMemID(String userMemID) {
    this.userMemID = userMemID;
}

public String getUserFirstName() {
    return userFirstName;
}

public void setUserFirstName(String userFirstName) {
    this.userFirstName = userFirstName;
}

public String getUserLastName() {
    return userLastName;
}

public void setUserLastName(String userLastName) {
    this.userLastName = userLastName;
}

public String getUserSkillLevel() {
    return userSkillLevel;
}

public void setUserSkillLevel(String userSkillLevel) {
    this.userSkillLevel = userSkillLevel;
}

public String getUserProviderID() {
    return userProviderID;
}

public void setUserProviderID(String userProviderID) {
    this.userProviderID = userProviderID;
}

}

这是收到JSON的函数。

@POST
@Path("/save")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public ResourceResponse saveDrivers(Drivers entities) {
    ResourceResponse resourceResponse = new ResourceResponse();
    driversBean.saveDrivers(entities);
    resourceResponse.setReturnCode(ResourceResponse.GOOD);
    return resourceResponse;
}

这是发送JSON的代码我不认为这是问题,因为JSON字符串格式正确

saveDrivers: function(criteria) {
        var mySettings = {
                type: "POST",
                url: "../resources/drivers/save",
                data: ko.toJSON($ata.drivers.model),
                cache: false,
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function(data) {
                    if(data.returnCode === "001") {
                        alert("Save successful!");
                    }
                    else { 
                        alert("saveDrivers FAILED!");
                    }
                },
                error: function() { alert("saveDrivers FAILED!"); }
            };
        $.ajax(mySettings); 
    }

我收到的错误说无法识别的字段“userID”未标记为可忽略。

请帮助我在我的智慧结束。

2 个答案:

答案 0 :(得分:0)

如果您可以访问JSON字符串,那么可以使用ObjectMapper将其转换为Java对象

String jsonString = "{\"userID\":\"346\",\"userMemID\":\"10\",\"userFirstName\":\"FIRSTNAME\",\"userLastName\":\"LASTNAME\",\"userSkillLevel\":\"7\",\"userProviderID\":\"098\"}";
ObjectMapper mapper = new ObjectMapper();
Drivers driver = mapper.readValue(jsonString, Drivers.class);
System.out.println(driver.getUserID()); // 346

答案 1 :(得分:0)

您可以尝试映射您的课程吗?我有一种预感,你的PropertyNamingStrategy没有正确反映JSON属性名称。

import com.fasterxml.jackson.annotation.JsonProperty;

public class Drivers {

    @JsonProperty("userID");
    private String userID = "";

    @JsonProperty("userMemID");
    private String userMemID = "";

    @JsonProperty("userFirstName");
    private String userFirstName = "";

    @JsonProperty("userLastName");
    private String userLastName = "";

    @JsonProperty("userSkillLevel");
    private String userSkillLevel = "";

    @JsonProperty("userProviderID");
    private String userProviderID = "";

    public String getUserID() {
        return userID;
    }

    public void setUserID(String userID) {
        this.userID = userID;
    }

    public String getUserMemID() {
        return userMemID;
    }

    public void setUserMemID(String userMemID) {
        this.userMemID = userMemID;
    }

    public String getUserFirstName() {
        return userFirstName;
    }

    public void setUserFirstName(String userFirstName) {
        this.userFirstName = userFirstName;
    }

    public String getUserLastName() {
        return userLastName;
    }

    public void setUserLastName(String userLastName) {
        this.userLastName = userLastName;
    }

    public String getUserSkillLevel() {
        return userSkillLevel;
    }

    public void setUserSkillLevel(String userSkillLevel) {
        this.userSkillLevel = userSkillLevel;
    }

    public String getUserProviderID() {
        return userProviderID;
    }

    public void setUserProviderID(String userProviderID) {
        this.userProviderID = userProviderID;
    }

}