Uso de GSON para analizar una matriz JSON
Tengo un archivo JSON como este:
[
{
"number": "3",
"title": "hello_world",
}, {
"number": "2",
"title": "hello_world",
}
]
Antes cuando los archivos tenían un elemento raíz yo usaba:
Wrapper w = gson.fromJson(JSONSTRING, Wrapper.class);
Código Pero no puedo pensar cómo codificar la clase Wrapper
ya que el elemento raíz es un array.
He intentado usar:
Wrapper[] wrapper = gson.fromJson(jsonLine, Wrapper[].class);
Con:
public class Wrapper{
String number;
String title;
}
Pero no he tenido suerte. ¿De qué otra manera puedo leer esto usando este método?
P. S Tengo esto para trabajar usando:
JsonArray entries = (JsonArray) new JsonParser().parse(jsonLine);
String title = ((JsonObject)entries.get(0)).get("title");
Pero preferiría saber cómo hacerlo (si es posible) con ambos métodos.
93
3 answers
El problema es causado por una coma después del último elemento de su matriz (después de cada title
). Si lo elimina y cambia sus datos a
[
{
"number": "3",
"title": "hello_world"
}, {
"number": "2",
"title": "hello_world"
}
]
Wrapper[] data = gson.fromJson(jElement, Wrapper[].class);
funcionará bien.
98
Author: Pshemo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-31 18:22:57
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-31 18:22:57
Gson gson = new Gson();
Wrapper[] arr = gson.fromJson(str, Wrapper[].class);
class Wrapper{
int number;
String title;
}
Parece funcionar bien. Pero hay una coma extra ,
en tu cadena.
[
{
"number" : "3",
"title" : "hello_world"
},
{
"number" : "2",
"title" : "hello_world"
}
]
37
Author: Narendra Pathai,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-21 21:11:06
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-21 21:11:06
public static <T> List<T> toList(String json, Class<T> clazz) {
if (null == json) {
return null;
}
Gson gson = new Gson();
return gson.fromJson(json, new TypeToken<T>(){}.getType());
}
Ejemplo de llamada:
List<Specifications> objects = GsonUtils.toList(products, Specifications.class);
9
Author: chenyueling,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-10 08:10:39
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-10 08:10:39