Skip to content Skip to sidebar Skip to footer

Gson - Convert From Json To List<>

i'm having an issue converting Json to List<>, I have tried different solutions but no clue My json result looks like : { 'Return':0, 'List':[ { '

Solution 1:

In this JSON, you get a list of {Code, Label} objects, but in the List property of an object.

You first need to encapsulate this list in an other object. As in:

publicclassQuestionaireList {
  publicList<Questionaire> List;
}

List<Questionaire> qsts = gson.fromJson(result,  QuestionaireList.class).List;

Solution 2:

You need another class that maps the full result

publicclassMyJson{
     @SerializedName("List")private List<Questionaire> list;

    @SerializedName("Return")private Integer return1;

    //... getters and setters
}

Your type then needs to bind to "MyJson" or whatever you name the class.

Solution 3:

I'm assuming it's the List part you want to map to 'qstList'. If so then you need to extract it from the rest of the json first:

Gsongson=newGson();
JsonObjectresultObj= gson.fromJson(result, JsonObject.class);
JsonArrayjsonList= resultObj.get("List").getAsJsonArray();
TypelistType=newTypeToken<List<Questionaire>>(){}.getType();
qstList = gson.fromJson(jsonList, listType);

Post a Comment for "Gson - Convert From Json To List<>"