【发布时间】:2017-04-04 23:49:30
【问题描述】:
我在尝试使用 JsonConverter 填充我的列表视图时遇到此异常
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 应为 BEGIN_ARRAY 但在第 1 行第 1 列路径 $
这是我的 JsonConverter 类:
public class JsonConverter<T> {
public JsonConverter() {
}
public ArrayList<T> toArrayList(String jsonString, Class<T> clazz) {
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat("dd/MM/yy HH:mm:ss");
Gson gson = builder.create();
JsonConverter.ListParameterizedType type = new JsonConverter.ListParameterizedType(clazz);
ArrayList list = (ArrayList)gson.fromJson(jsonString, type);
return list;
}
public List<T> toList(String jsonString, Class<T> clazz) {
ArrayList list = this.toArrayList(jsonString, clazz);
return list;
}
private static class ListParameterizedType implements ParameterizedType {
private Type type;
private ListParameterizedType(Type type) {
this.type = type;
}
public Type[] getActualTypeArguments() {
return new Type[]{this.type};
}
public Type getRawType() {
return ArrayList.class;
}
public Type getOwnerType() {
return null;
}
}
}
这是我的模型类:
public class Product implements Serializable {
@SerializedName("pid")
public int pid;
@SerializedName("name")
public String name;
@SerializedName("qty")
public int qty;
@SerializedName("price")
public String description;
@SerializedName("image_url")
public String image_url;
@SerializedName("date")
public String date;
}
我这样称呼它:
private ArrayList<Product> productList;
@Override
public void processFinish(String s) {
productList = new JsonConverter<Product>().toArrayList(s, Product.class);
.
.
.
不知道是不是我这里做错了
【问题讨论】:
-
你能发布json吗?
标签: android json arraylist android-json