WenJackp
WenJackp

Reputation: 1

parse json data with gson on android

My model looks like this:

public class DefaultModel<T> {
    public int Status;
    public T JSON;
    public String ErrorMessage;
    public String InfoMessage;
}

The json font style

{"ErrorMessage":"null","InfoMessage":"null","JSON":
[{"DictId":"1","ItemCode":"1","ItemName":"sINGLE","SortNum":0,"
isChecked":true},{"ItemName":"WenJackp","SortNum":0,"isChecked":false}]
,"Status":100}

I want parse this json, but gson throws exception

com.google.gson.internal.LinkedTreeMap cannot be cast to xxxxx.DictItem

How to parse ?


add more details, I use this method parse json data:

The above is when I deal with JSON parsing method used by the, I want use Default<List<DictItem>> this type , T == List<DictItem>

Upvotes: 0

Views: 68

Answers (1)

Stimsoni
Stimsoni

Reputation: 3156

For type 'T' use

List<InnerClass> json;

Then you can put an inner class into your DefaultModel that would have

String dictId;
String itemCode;
String itemName;
String sortNum;
boolean isChecked;

On a side note these variables should be marked private and accessed via a getter. You can also have different variable names if you wish by annotating them with @SerializedName

eg

@SerializedName("ErrorMessage")
private String myErrowMsg;

Upvotes: 1

Related Questions