Java“Jackson”JsonMappingException:无法反序列化 FIELD_NAME 标记之外的浮点数实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12196244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:00:09  来源:igfitidea点击:

Java "Hymanson" JsonMappingException: Can not deserialize instance of float out of FIELD_NAME token

javajsonHymanson

提问by Dfr

with this class:

与这个类:

public class Products implements Serializable {
    private BigDecimal productId;
    private float priority;

    public float getPriority() {
        return priority;
    }

    public void setPriority(float priority) {
        this.priority = priority;
    }
}

When doing deserialization of such JSON data:

在对此类 JSON 数据进行反序列化时:

{"productId":47552,"priority":78}

Got this error:

得到这个错误:

org.codehaus.Hymanson.map.JsonMappingException: 
Can not deserialize instance of float out of FIELD_NAME token
 at [Source: org.apache.catalina.connector.CoyoteInputStream@103cf49; line: 1, \
 column: 290] (through reference chain: entity.Products["priority"])

But for this data (quotes around priority value)

但是对于这个数据(围绕优先级值的引用)

{"productId":47552,"priority":"78"}

works well, so it seems that Hymanson (1.9.9) does not respect numeric values ? I suspect something is wrong here.

效果很好,所以 Hymanson (1.9.9) 似乎不尊重数值?我怀疑这里有问题。

采纳答案by Manikandan

You are declaring the field priority as a float type and you try to deserialize the Json which contains int value. Hymanson try to call a setter function which accepts a integer value. So we need to add one setter like this.

您将字段优先级声明为浮点类型,并尝试反序列化包含 int 值的 Json。Hymanson 尝试调用一个接受整数值的 setter 函数。所以我们需要像这样添加一个 setter。

public void setPriority(int priority){
    this.priority = Float.valueOf(priority);
}

回答by sreemanth pulagam

Seems you declared priority as long and getter and setter methods are using float. can you check with

似乎您声明了优先级,因为 long 和 getter 和 setter 方法正在使用浮点数。你能检查一下吗

   public long getPriority() {
            return priority;
        }

        public void setPriority(long priority) {
            this.priority = priority;
        }