Skip to main content
añadido error
Origen Enlace
Fumatamax
  • 407
  • 3
  • 14

P.D. Dejo el mensaje completo del Error que me tira.

     com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 148 path $.stats
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
            at com.google.gson.Gson.fromJson(Gson.java:927)
            at com.google.gson.Gson.fromJson(Gson.java:892)
            at com.google.gson.Gson.fromJson(Gson.java:841)
            at com.google.gson.Gson.fromJson(Gson.java:813)
            at 
com.test.MainActivity$1.onClick(MainActivity.java:67)
            at android.view.View.performClick(View.java:6294)
            at android.view.View$PerformClick.run(View.java:24770)
            at android.os.Handler.handleCallback(Handler.java:790)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
         Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 148 path $.stats
            at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
            at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80)
            at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
            at com.google.gson.Gson.fromJson(Gson.java:927) 
            at com.google.gson.Gson.fromJson(Gson.java:892) 
            at com.google.gson.Gson.fromJson(Gson.java:841) 
            at com.google.gson.Gson.fromJson(Gson.java:813) 
            at 
com.test.MainActivity$1.onClick(MainActivity.java:67) 
            at android.view.View.performClick(View.java:6294) 
            at android.view.View$PerformClick.run(View.java:24770) 
            at android.os.Handler.handleCallback(Handler.java:790) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:164) 
            at android.app.ActivityThread.main(ActivityThread.java:6494) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

P.D. Dejo el mensaje completo del Error que me tira.

     com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 148 path $.stats
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
            at com.google.gson.Gson.fromJson(Gson.java:927)
            at com.google.gson.Gson.fromJson(Gson.java:892)
            at com.google.gson.Gson.fromJson(Gson.java:841)
            at com.google.gson.Gson.fromJson(Gson.java:813)
            at 
com.test.MainActivity$1.onClick(MainActivity.java:67)
            at android.view.View.performClick(View.java:6294)
            at android.view.View$PerformClick.run(View.java:24770)
            at android.os.Handler.handleCallback(Handler.java:790)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
         Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 148 path $.stats
            at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
            at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80)
            at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
            at com.google.gson.Gson.fromJson(Gson.java:927) 
            at com.google.gson.Gson.fromJson(Gson.java:892) 
            at com.google.gson.Gson.fromJson(Gson.java:841) 
            at com.google.gson.Gson.fromJson(Gson.java:813) 
            at 
com.test.MainActivity$1.onClick(MainActivity.java:67) 
            at android.view.View.performClick(View.java:6294) 
            at android.view.View$PerformClick.run(View.java:24770) 
            at android.os.Handler.handleCallback(Handler.java:790) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:164) 
            at android.app.ActivityThread.main(ActivityThread.java:6494) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Origen Enlace
Fumatamax
  • 407
  • 3
  • 14

Como recorrer un Json con Array

Buenas estoy teniendo problemas para recorrer un json con array o eso creo, os dejo el codigo json:

 {
    "username": "Pepiko",
    "platform": "pc",
    "stats": {
        "kills_solo": 1,
        "placetop1_solo": 0,
        "placetop10_solo": 1,
        "placetop25_solo": 1
    },
    "totals": {
        "kills": 13,
        "wins": 0,
        "kd": 0.41
    }
}

El problema lo tengo para poder acceder por ejemplo a "stats" o "totals". Cuando lanzo el emulador, me arroja el siguiente Error:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 148 path $.stats

Creo que dice que esta esperando un Array, pero creo que estoy pasando le un objecto. La duda es que no se como puedo sacar un array a partir de un List<>

FichaUsuario.java

public class FichaUsuario {
         public String username;
         public String platform;
         public List<stats> stats;
}

stats.java

public class stats {
    public int kills_solo;
    public int placetop1_solo;
    public int placetop10_solo;
}

MainActivity.java

try {
    Response response = client.newCall(request).execute();
    if( response.isSuccessful() ){
        String jsonResponder = response.body().string();

        Log.d("Testeo", jsonResponder);
        Gson gson = new Gson(); // Carga Libreria Gson
        FichaUsuario fichaUsuario = gson.fromJson(jsonResponder, FichaUsuario.class);
        Log.d("Testeo", fichaUsuario.username + fichaUsuario.platform + fichaUsuario.stats.placetop10_solo);

    }else{
        Log.d("ERROR", response.body().string());
    }
} catch (IOException e) {
    e.printStackTrace();
}

¿Que es lo que estoy haciendo mal?, ¿Que pasos debo seguir para mostrar los atributos de stats del json?. Recuedo que trabajo con la libreria Gson, Gracias de Antemano Comunidad.