java - JsonMappingException : Can not instantiate value of type no single-long-arg constructor/factory method

标签 java json jackson deserialization zk

您好,在 zk 框架上尝试将 json 响应解析为 java 时出错

这是杰森的回复

{"currentTime":1355390722038,"text":"OK","data":{"limitExceeded":false,"references":{"stops":[],"situations":[],"trips":[],"routes":[],"agencies":[{"id":"AG1","privateService":false,"phone":"","timezone":"Asia/Kuala_Lumpur","disclaimer":"","name":"Panorama","lang":"en","url":"http://www.allcompanyonline.com/company/33/8/26/62438/PM-Cultural--Tourism-Sdn-Bhd.html"}]},"list":[{"lonSpan":0.1766824722290039,"lon":102.2011971473685,"agencyId":"AG1","lat":2.2659808772471948,"latSpan":0.15555363245723042}]},"code":200,"version":2}

我一直收到这个错误

com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.panorama.tripplan.pojo.Agency_coverage] from Long integral number; no single-long-arg constructor/factory method

这是根变量的 pojo

@JsonRootName(value = "currentTime")
public class Agency_coverage{
private Number code;
private Number currentTime;
private Data data;
private String text;
private Number version;

public Agency_coverage(){}

public Number getCode(){
    return this.code;
}
public void setCode(Number code){
    this.code = code;
}
public Number getCurrentTime(){
    return this.currentTime;
}
public void setCurrentTime(Number currentTime){
    this.currentTime = currentTime;
}
public Data getData(){
    return this.data;
}
public void setData(Data data){
    this.data = data;
}
public String getText(){
    return this.text;
}
public void setText(String text){
    this.text = text;
}
public Number getVersion(){
    return this.version;
}
public void setVersion(Number version){
    this.version = version;
}

这是我尝试调用 API 并解析响应的代码

java.net.URLConnection connection = new URL(url + "?" + query+ "&" + queryLat+ "&" + queryLon).openConnection();
       connection.setRequestProperty("Accept-Charset", charset);
       if ( connection instanceof HttpURLConnection)
       {
         //Jackson 2.X configuration settings
            mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

            mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
            mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
            mapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
            mapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

            //Tell Jackson to expect the JSON in PascalCase, instead of camelCase
            mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.PascalCaseStrategy());

          HttpURLConnection httpConnection =(HttpURLConnection) connection;  
        //Make the HTTP request, and deserialize the JSON response into the object

          Agency_coverage agencyData= new Agency_coverage();
          agencyData = mapper.readValue(agenturl, Agency_coverage.class);


          //System.out.println(agencyData.getData().getList().get(0).);
          System.out.println(agencyData.getCurrentTime());
          System.out.println(httpConnection.getResponseCode());

       }

如果你能理解这个问题,请帮忙,我对 java 开发有点陌生,我正在全力以赴

最佳答案

第一件事是按照@aymeric 的建议删除 @JsonRootName(value = "currentTime") 注释。这个注解告诉 Jackson 映射器,当您的 POJO 称这个东西为“Agency_coverage”时,匹配的 JSON 对象将称它为“currentTime”。事实并非如此。

现在,除此之外,您的 JSON 应该看起来更像这样:

{"Agency_coverage":{"currentTime":1355390722038,"text":"OK","data":"etc..."}}

这里的不同之处在于,您要为 Jackson 提供要解包的根元素。

您现在可以看到 Jackson 所说的错误。使用您提供的 JSON 和您使用的注释,它发现 currentTime 作为映射到 Agency_coverage POJO 的 JSON 元素,但 currentTime 的值> 是一个 long,无法映射。

关于java - JsonMappingException : Can not instantiate value of type no single-long-arg constructor/factory method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13876525/

相关文章:

javascript - 更改 JSON_Encode 输出格式(括号和逗号)

java - JsonView 序列化取决于某些条件/属性名称等

java - 如果输出 json 具有带点(.) 字符的键,如何在 Spring 中为 Rest api 创建域类

Java:有效(且安全)地删除最终 SortedMap<K,V> 中的元素序列?

java - 从 AssetManager Android 获取 FileChannel

java - 在不上网的情况下调用Spring Controller 方法

java - 如何使用 Jackson 将 JSON 数组中的嵌套值解析到列表中

Java 堆栈溢出错误

json - 创建一个列表并使用 Sencha Touch 从 JSON 源填充它

javascript - 将项目添加到数组 Angular