/ / Gson array parser - Android, arreglos, json, gson

Gson array parser - android, arrays, json, gson

Tengo respuesta json en esta forma:

String json = "{"0":{    "title" :"title 1" ,   "time" : "15:00"    } ,"1":{    "title" : "title 2"  ,  "time" :"16:00"   }}";

Y aquí está mi clase, estoy tratando de asignarla a:

    public class News implements Seriaizable{
@SerializedName("title")
private String title;

@SerializedName("time")
private String time;
}

Estoy luchando porque tengo una matriz sin un nombre que contiene un montón de otras matrices.

 Gson gson = new GsonBuilder().setPrettyPrinting().create();
News obj = gson.fromJson(reader, News.class);

¿Alguien podría guiarme en la dirección correcta?

Respuestas

1 para la respuesta № 1

Su JSON es incorrecto en formato. He añadido algunas comas para corregirlo.

Puedes resolver este problema usando mapas:

String json = "{"0":{    "title" :"title 1" ,   "time" : "15:00"    } ,"1":{    "title" : "title 2"  ,  "time" :"16:00"   }}";

Gson gson = new Gson();

Type type = new TypeToken<Map<Integer, News>>() {}.getType();

Map<Integer, News> map = gson.fromJson(json, type);

Ref: https://sites.google.com/site/gson/gson-user-guide#TOC-Collections-Examples