JsonMappingException:无法从Integral数中实例化类型的值;没有single-int-arg构造函数/工厂方法

时间:2016-02-06 09:00:18

标签: java android firebase

我试图查询我在Firebase上的JSON树中的一些数据。我继续创建我的POJO,但我一直收到JsonMappingException: Can not instantiate value of type [simple type, class net.rmoreno.ally.Ally] from Integral number; no single-int-arg constructor/factory method错误。我已经做到这一点,只考虑String email,但它不断崩溃并告诉我问题在于积分数。我该如何解决这个问题。

以下是使其崩溃的代码

query.addChildEventListener(new ChildEventListener() {

       ArrayList<Ally> allyArrayList = new ArrayList<>();

       @Override
       public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            for(DataSnapshot userSnapshot: dataSnapshot.getChildren()){
                Ally information = userSnapshot.getValue(Ally.class);
                allyArrayList.add(information);

      }
}

这是我的POJO

@JsonIgnoreProperties({"latitude", "longitude","phone","availability","email"})
public class Ally {

    String name;
    //String email;
    //String phone;
    //int availability;

    public Ally(){

    }

    public String getName(){
        return name;
    }

//    public String getEmail(){
//        return email;
//    }

//    public String getPhone(){
//        return phone;
//    }

//    public int getAvailability(){
//        return availability;
//    }
}

这是我的堆栈轨道

 com.firebase.client.FirebaseException: Failed to bounce to type
            at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185)
            at net.rmoreno.ally.MainActivity$1.onChildAdded(MainActivity.java:46)
            at com.firebase.client.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:58)
            at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
            at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class net.rmoreno.ally.Ally] from Integral number; no single-int-arg constructor/factory method
            at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromInt(StdValueInstantiator.java:320)
            at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromNumber(BeanDeserializerBase.java:1012)
            at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:138)
            at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:123)
            at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
            at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
            at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
            at net.rmoreno.ally.MainActivity$1.onChildAdded(MainActivity.java:46)
            at com.firebase.client.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:58)
            at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
            at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

编辑:这是Firebase上的JSON结构

enter image description here

1 个答案:

答案 0 :(得分:1)

在您的JSON中,您有一个数组,其余字段如availabilityemailname等存在于该数组中。

在Ally类中,您试图直接解析这些内部对象。

你应该做的事情如下:

这是内部对象类:

@JsonIgnoreProperties({"latitude", "longitude","phone","availability","email"})
public class ArrayObject {

    private String name;
    private String email;
    private String phone;
    private int availability;

    public ArrayObject() {}

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

    public String getPhone() {
        return phone;
    }

    public int getAvailability() {
        return availability;
    }
}

以下是 Ally 类:

public class Ally {
    private ArrayObject arrObj;

    /* Getter-Setters */

    public Ally() {}
}