0

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

Cristo
  • 1
  • 2

2 Answers2

0

your code seems correct. Just check whether your json string is proper, check your sb.toString() has etat variable in it.

rakesh
  • 4,368
  • 1
  • 19
  • 13
  • Thanks yes my code seems correct, the begin of sb.toString() is [{"id":"1","anneeMois":"201611","dateModification":"2016-04-18","montantValide":"500.00","nbJustificatifs":"3","visitorId":"1","etat":{"id":"1","libEtat":"Saisie cl\u00f4tur\u00e9e"}},{"id":"8","anneeMois":"201617","dateModification":"2016-08-16","montantValide":"650.00"," and he has etat – Cristo Apr 11 '17 at 09:34
  • https://jsonformatter.curiousconcept.com/ give me Valid JSON (RFC 4627) for my sb.ToString() .... – Cristo Apr 11 '17 at 09:39
  • Finally, i use a old solution without the library Gson – Cristo Apr 11 '17 at 16:46
  • have you tried with gson Type type = new ListParameterizedType(clazz); List mesFichesFrais = gson.fromJson(json, type); – Cristo Apr 12 '17 at 13:24
0

Finally, i use a old solution with JSONArray without the library Gson
This library works with a arraylist

Thank's for all answers

               jsonarray = new JSONArray(sb.toString());
        for(int i=0; i < jsonarray.length(); i++) {
            JSONObject jsonobject = jsonarray.getJSONObject(i);
            FicheFrais uneF = new FicheFrais();
            uneF.setId(Integer.parseInt( jsonobject.getString("id")));
            uneF.setAnneeMois(jsonobject.getString("anneeMois"));
            uneF.setDateModification(Date.valueOf(jsonobject.getString("dateModification")));
            uneF.setMontantValide(Float.parseFloat(jsonobject.getString("montantValide")));
            uneF.setNbJustificatifs(Integer.parseInt(jsonobject.getString("nbJustificatifs")));
            uneF.setVisitorId(Integer.parseInt(jsonobject.getString("visitorId")));
            // On récupère l'état
            JSONObject  unE = jsonobject.getJSONObject("etat");
            Etat unEtat = new Etat ();
            unEtat.setId_etat(unE.getInt("id"));
            unEtat.setLib_etat(unE.getString("libEtat"));
            uneF.setEtat(unEtat);
            mesFichesFrais.add(uneF);
Cristo
  • 1
  • 2