arrays - GSON 阵列,错误消息 : Expected a string but was BEGIN_ARRAY

标签 arrays json gson android

我想为我的数组使用 GSON

我看了几个例子,但无法让它与我的代码一起工作。
Using GSON to parse a JSON array .

我收到此错误消息:应为字符串,但在第 1 行列处为 BEGIN_ARRAY

我在这个项目中遵循的原始教程涵盖了解析 Json 对象。

我的 Json:

[{
   "nid": "25",
   "title": "angry guy",
   "body": "fhjk gjj"
}, {
   "nid": "24",
   "title": "25 mobile",
   "body": "25 test tes"
}, {
   "nid": "8",
   "title": "new post 4",
   "body": "sdfsdf sdfsdf"
}, {
   "nid": "7",
   "title": "new post",
   "body": "sdf sdf sdfsdf"
}]

我的代码:

String finalJson = buffer.toString();
            JSONArray parentArray = new JSONArray(finalJson);
            List<ExerciseModel> exerciseModelList = new ArrayList<>();

            Gson gson = new Gson();
            for(int i=0; i<parentArray.length(); i++){
                JSONObject finalObject = parentArray.getJSONObject(i);
                ExerciseModel exerciseModel = gson.fromJson(finalObject.toString(), ExerciseModel.class);
                exerciseModelList.add(exerciseModel);
            }

            return exerciseModelList;

我的模型:

    public class ExerciseModel {

    private int nid;
    private String title;
    private String body;

    public int getNid() {
        return nid;
    }
    public void setNid(int nid) {
        this.nid = nid;
    }
    public String getTitle() {
        return title;
    }
    public String toString() {
        return this.title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getBody() {
        return body;
    }
    public void setBody(String body) {
        this.body = body;
    }
}

提前致谢

最佳答案

你的类(class)应该是

public class ExerciseModel
{
  private String nid;

  public String getNid() { return this.nid; }

  public void setNid(String nid) { this.nid = nid; }

  private String title;

  public String getTitle() { return this.title; }

  public void setTitle(String title) { this.title = title; }

  private String body;

  public String getBody() { return this.body; }

  public void setBody(String body) { this.body = body; }

}

GSON 代码的代码应该是:

String json = "[{ \"nid\": \"25\", \"title\": \"angry guy\", \"body\": \"fhjk gjj\" }, { \"nid\": \"24\", \"title\": \"25 mobile\", \"body\": \"25 test tes\" }, { \"nid\": \"8\", \"title\": \"new post 4\", \"body\": \"sdfsdf sdfsdf\" }, { \"nid\": \"7\", \"title\": \"new post\", \"body\": \"sdf sdf sdfsdf\" }]";
Type listOfTestObject = new TypeToken<List<ExerciseModel>>() {}.getType();
ArrayList<ExerciseModel> models = new Gson().fromJson(json, listOfTestObject);
System.out.println(models.get(0).getTitle());

关于arrays - GSON 阵列,错误消息 : Expected a string but was BEGIN_ARRAY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39161354/

相关文章:

javascript - NodeJS 中奇怪的数组行为

Objective-C 从对象数组中创建逗号分隔字符串的最简单方法

javascript - 如何从 Promise 中检索 axios 数据

c++ - 如何在不使用拷贝的情况下更改 QJson 层次结构中的 QJsonObject 值?

java - Jackson - 由于构造函数导致的 JsonMappingException

c - 为什么会出现段错误(核心已转储)?

javascript - 即使发生匹配,$.grep 也会返回空数组

database - 家谱:关联数据库中的家庭关系

java - Gson:我们可以在类型适配器中获取序列化的字段名称吗?

java - 如何处理在 socket java 上发送的大对象?