I would like to parse this JSON file with Gson:
[
{
"id": "1",
"anneeMois": "201611",
"dateModification": "2016-04-18",
"montantValide": "500.00",
"nbJustificatifs": "3",
"visitorId": "1",
"etat": {
"id": "1",
"libEtat": "Saisie clôturée"
}
},
{
"id": "8",
"anneeMois": "201617",
"dateModification": "2016-08-16",
"montantValide": "650.00",
"nbJustificatifs": "7",
"visitorId": "1",
"etat": {
"id": "1",
"libEtat": "Saisie clôturée"
}
},
{
"id": "2",
"anneeMois": "201610",
"dateModification": "2016-03-19",
"montantValide": "300.00",
"nbJustificatifs": "2",
"visitorId": "1",
"etat": {
"id": "2",
"libEtat": "Fiche créée, saisie en cours"
}
}
]
My classes are:
public class FicheFrais implements java.io.Serializable
{
private int id;
private String anneeMois;
private Date dateModification;
private float montantValide;
private int nbJustificatifs;
private int visitorId;
@SerializedName("etat")
protected Etat etat;
.....
public class Etat implements java.io.Serializable{
private int id_etat;
private String lib_etat;
...
My code is:
public static <T> List<T> toList(String json, Class<T> typeClass)
{
List<T> arr =new Gson().fromJson(json, new
ListParameterizedType<T>
(typeClass));
return arr;
}
List<FicheFrais> mesFichesFrais = toList(sb.toString(),FicheFrais.class);
I can read my ArrayList
but my object is empty.
Etat
id_etat = 0
lib_etat = null
I give you my function
public class ListParameterizedType<T> implements ParameterizedType
{
private Class<?> wrapped;
public ListParameterizedType(Class<T> wrapper)
{
this.wrapped = wrapper;
}
@Override
public Type[] getActualTypeArguments()
{
return new Type[] { wrapped };
}
@Override
public Type getRawType()
{
return List.class;
}
@Override
public Type getOwnerType()
{
return null;
}
}
When i call my arrayList with this code
List<FicheFrais> mesFichesFrais =
toList(sb.toString(),FicheFrais.class);
I obtain values in all fields except in my object Etat who is empty
Have you a example with a serialization like : - class with a object - use library Gson - flux to json to arraylist Thanks