无法将json字符串解析为通用java对象

时间:2013-10-03 10:21:59

标签: java json jaxb jersey

我正在研究球衣网络服务。 like this以下是我在我的球衣客户端收到的json字符串

{"license":"12345","dimRequirement":[{"alternateId":"AlternateID","classificati‌​on":"1"}] }

但是当我解析它时会出错。我用jackson解析

    Unrecognized field "dimRequirement" (Class com.vxl.AppObject), not marked as ignorable
at [Source: java.io.StringReader@7be6f06c; line: 1, column: 49] (through reference chain: com.vxl.appanalytix.AppObject["dimRequirement"])
    at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:244)
    at org.codehaus.jackson.map.deser.StdDeserializer.reportUnknownProperty(StdDeserializer.java:589)
    at org.codehaus.jackson.map.deser.StdDeserializer.handleUnknownProperty(StdDeserializer.java:575)
    at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:684)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:515)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:351)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2131)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1402)
    at com.vxl.CheckJersy.main(CheckJersy.java:56)

我试过添加类型参考解析

new ObjectMapper().readValue(
                output,new TypeReference<AppObject<DimRequirement>>() { } );

泛类

    @XmlRootElement
@XmlSeeAlso({ DimRequirement.class})
    public class AppObject<T> implements Serializable {

        private List<T> list;
        private String license;

        public AppObject() {
            list = new ArrayList<T>();

        }

        public AppObject(List<T> list) {
            this.list = list;
        }

        @XmlAnyElement(lax = true)
        public List<T> getList() {
            return list;
        }

        public void setList(List<T> list) {
            this.list = list;
        }

        public String getLicense() {
            return license;
        }

        public void setLicense(String license) {
            this.license = license;
        }

    }

服务 我想为多个POST请求使用相同的服务返回AppObject<xxx>,AppObject<yyy>

@POST
    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    @Path("/getreq")
    @Override
    public AppObject getAllByClientNIsCurrent(
            AppObject<DimRequirement> appObject) {
        List<DimRequirement> dimreqlist = dimRequirementDao
                .getAllByClientNIsCurrent(appObject.getClientKey());
        AppObject<DimRequirement> appObject = new AppObject<DimRequirement>();
        appObject.setList(dimreqlist);
        return appObject;
    }

2 个答案:

答案 0 :(得分:0)

您的AppObject类有一个JSON中不存在的字段。您需要使用jackson注释创建类,以表示它可以被忽略。

答案 1 :(得分:0)

抱歉,关于我的其他答案,请忽略它。

我的代码完全按原样运行。所需要的只是变量“list”需要命名为“dimRequirement”才能使自动映射生效。您还需要修改getter和setter,因为一些Jackson设置可以让库使用它们。