如何使用 gson android 中的模型检索 json?

how to retrieve json using models in gson android?

我有 json 数组,如下所示:

{
  "otg": [
    {
      "id": "1",
      "name": "Forum OTG Nasional",
      "description": "otg description",
      "banner": "",
      "date": "June, 18th 2015",
      "time": "08:06"
    }
  ]
}

我想通过使用 gson 的模型检索此 json, 这是我的模特 class :

import java.util.ArrayList;
import java.util.List;
import com.google.gson.annotations.Expose;

public class ModelB {

    @Expose
    private List<Otg> otg = new ArrayList<Otg>();

    public List<Otg> getOtg() {
        return otg;
    }
    public void setOtg(List<Otg> otg) {
        this.otg = otg;
    }

    public class Otg {

        @Expose
        private String id;
        @Expose
        private String name;
        @Expose
        private String description;
        @Expose
        private String banner;
        @Expose
        private String date;
        @Expose
        private String time;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getBanner() {
            return banner;
        }

        public void setBanner(String banner) {
            this.banner = banner;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }

    }

}

然后在我的代码下面使用模型检索 json :

@Override
public void updateModel(String models) {
    list_model = new ArrayList<ModelB>();

    try {
        List<ModelB> model = new Gson().fromJson(models, new TypeToken<List<ModelB>>() {
        }.getType()); // CANNOT READ THIS LINE

        Log.d("COUNT_check", model.size() + "");
        list_model = model;
    } catch (Exception e) {
        e.printStackTrace();
        Log.d("check_error", "error_home"); // ALWAYS DISPLAY IT
    }
}

但是我的代码无法在模型列表中检索,总是出错。 关于这个问题有什么解决方案?

i'have json array like below :

这不是一个数组,它是一个具有键值映射的对象,将一个键映射到一个数组(包含零个或多个 Otg 实例或任何你称之为的实例)。

因此您的数据不代表 List<ModelB>,而是单个 ModelB 实例:

ModelB model = new Gson().fromJson(s, ModelB.class);

这是错误消息所指示的内容:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
    at com.google.gson.Gson.fromJson(Gson.java:822)
    at com.google.gson.Gson.fromJson(Gson.java:775)
    at com.google.gson.Gson.fromJson(Gson.java:724)

通过将其反序列化为 List<ModelB>,您告诉解析器 JSON 数据应以数组 ([...]) 开头,而实际上它以对象 ({...}).

删除此

 @Expose
private List<Otg> otg = new ArrayList<Otg>();

public List<Otg> getOtg() {
    return otg;
}
public void setOtg(List<Otg> otg) {
    this.otg = otg;
}

 public class ModelB{

    @Expose
    private String id;
    @Expose
    private String name;
    @Expose
    private String description;
    @Expose
    private String banner;
    @Expose
    private String date;
    @Expose
    private String time;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getBanner() {
        return banner;
    }

    public void setBanner(String banner) {
        this.banner = banner;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

}

然后它会给你预期的列表数组。 否则,如果您想要与您拥有的相同模型,那么它将简单地解析为:

ModelB  gsonObj = gson.fromJson(jsonString, ModelB.class);

然后你可以得到你的arraylist

           ArrayList<ModelB> yourList=   gsonObj.getOtg();

如果您想检索 Otg 对象列表,则可以执行以下操作:

 @Override
 public void updateModel(String models) {
     ...
     List<Otg> otgs = new Gson().fromJson(models.getAsJsonObject().get("otg"),
                                new TypeToken<List<Otg>>() {
                                }.getType());
     ...
 }

也许可以这样尝试:

public class Clazz{

    public static class List extends ArrayList<Clazz> {}

    @SerializedName("custom_parameter")
    private String customParameter;


    public final String getCustomParameter(){
        return customParameter;
    }

    public final Clazz setCustomParameter(final String customParameter){
        this.customParameter = customParameter;
        return this;
    }

}

获得:

Clazz.List clazzList = new Gson().fromJson(jsonString, Clazz.List.class);