Reputation: 1
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:
create ParameterizedType and you see buildType method
ParameterizedType mType = buildType(DefaultModel.class, ArrayList.class, DictItem.class);
parse json data
DefaultModel<List<DictItem>> mResult = mGson.fromJson(json, mType);
create new ParameterizedType
protected ParameterizedType buildType(final Class raw, final Type... args) {
return new ParameterizedType() {
public Type getRawType() {
return raw;
}
public Type[] getActualTypeArguments() {
return args;
}
public Type getOwnerType() {
return null;
}
};
}
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
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