JsonMappingException:无法实例化类型no single-long-arg构造函数/工厂方法的值

时间:2021-01-12 18:03:24

hi am getting error trying to parse a json response into java on zk framework

嗨我正在尝试在zk框架上解析json响应到java中

here is the jason reply

这是杰森的答复

{"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}

i keep getting this error

我一直收到这个错误

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

here is the pojo for the root variables

这是根变量的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;
}

here is the code where i attempt to call the api and parse the response

这是我尝试调用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());

       }

please help if you can understand this problem, i am a bit new to java development and am giving it my all

请帮助,如果你能理解这个问题,我对java开发有点新意,并且我全力以赴

1 个解决方案

#1


7  

The first thing is to remove the @JsonRootName(value = "currentTime") annotation as @aymeric suggested. This annotation is telling the Jackson mapper that, while your POJO calls this thing 'Agency_coverage', the matching JSON object will be calling it 'currentTime'. That is not the case.

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

Now, that aside, your JSON should look more like this:

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

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

The difference here is that you are providing the root element for Jackson to unwrap.

这里的不同之处在于,您正在为杰克逊提供解包的根元素。

You can now see what the error from Jackson was saying. With the JSON you provided and the annotation you were using, it found currentTime as the JSON element which maps to the Agency_coverage POJO but the value for currentTime was a long and couldn't be mapped.

你现在可以看到杰克逊的错误在说什么。使用您提供的JSON和您正在使用的注释,它发现currentTime作为映射到Agency_coverage POJO的JSON元素,但currentTime的值很长,无法映射。

#1


7  

The first thing is to remove the @JsonRootName(value = "currentTime") annotation as @aymeric suggested. This annotation is telling the Jackson mapper that, while your POJO calls this thing 'Agency_coverage', the matching JSON object will be calling it 'currentTime'. That is not the case.

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

Now, that aside, your JSON should look more like this:

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

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

The difference here is that you are providing the root element for Jackson to unwrap.

这里的不同之处在于,您正在为杰克逊提供解包的根元素。

You can now see what the error from Jackson was saying. With the JSON you provided and the annotation you were using, it found currentTime as the JSON element which maps to the Agency_coverage POJO but the value for currentTime was a long and couldn't be mapped.

你现在可以看到杰克逊的错误在说什么。使用您提供的JSON和您正在使用的注释,它发现currentTime作为映射到Agency_coverage POJO的JSON元素,但currentTime的值很长,无法映射。