JsonMappingException: Can not deserialize instance of com.example.package.XXX out of START_ARRAY token

1

I am trying to use FIREBASE in Android and I have uploaded my JSON created in my FIREBASE account, but something I do not understand is that I do not get the JSON wrong, or the "Retrieving Data" part of the Firebase I'm doing wrong, Here I am the JSON:

{
  "Persona": [
    {
      "nombre": "X",
      "apellido": "XX",
      "ciudad": "XXX",
      "hijoList": [
        {
          "nombreHijo": "XXXX",
          "edadHijo": "XXXXX"
        }
       ]
},
{......}

It connects me and makes me a snapshot without any problem, I show it by toast and it connects me well, but when it comes to using my class Person to get the first and last name, there is something wrong, I teach my class Person :

public class Persona{

private String nombre;      
private String apellido;     
private String ciudad;     

private ArrayList<Hijo> hijoList;  // Clase hijo con nombre y edad como atributos

public Persona(){
    //empty default constructor, necessary for Firebase to be able to deserialize Persona
}

And then the simple code that just in case I put it in case I have not understood the API FB well:

firebase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            System.out.println("El numero total son: " + dataSnapshot.getChildrenCount());
            for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                Persona post = postSnapshot.getValue(Persona.class);
                System.out.println("JSON FIREBASE: "+post.getNombre() + " - " + post.getApellido());
            }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            Toast.makeText(SplashActivity.this, "No se cargaron los personajes debido a: "
                    + firebaseError, Toast.LENGTH_SHORT).show();
        }
    });

What is the problem? can it be because the JSON has Person as an array? Thanks in advance

    
asked by M. Mariscal 13.02.2016 в 10:46
source

2 answers

0

The error was that in the JSON is composed of array of Persona , then in the

firebase.addValueEventListener(new ValueEventListener(){....});

must be changed by:

firebase.child("Persona").addValueEventListener(new ValueEventListener(){....});

I hope it has helped you

    
answered by 23.02.2016 / 09:58
source
0

I see that your implementation is correct:

 <Clase> post = postSnapshot.getValue(<Clase>.class);

But your class persona does not contain the methods get() for nombre and apellido :

    public class Persona{

            private String nombre;      
            private String apellido;     
            private String ciudad;     
...
...
...

            public String getNombre(){
                   return nombre;
            } 

            public String getApellido(){
                   return apellido;
            } 
        } 

More information: Firebase, getting data. (English)

    
answered by 13.02.2016 в 19:31