当前位置: 首页 > 知识库问答 >
问题:

Android GSON需要BEGIN_ARRAY,但在第1行第2列是BEGIN_OBJECT

章涵蓄
2023-03-14
{
  "Message": "The request is invalid.",
  "ModelState": {
    "": [
      "Name c@gmail.com is already taken.",
      "Email 'c@gmail.com' is already taken."
    ]
  }
}
ErrorRequest page = gson.fromJson(response.getResponseString(), ErrorRequest.class);
package com.devinedesign.cleanride.domain;

import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

public class ErrorRequest
{
    @SerializedName("Message")
    private String message;

    @SerializedName("ModelState")
    private List<ModelState> modelState;

    public ErrorRequest(String message,List<ModelState> modelState)
    {
        this.message    = message;
        this.modelState = modelState;
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }

    public List<ModelState> getModelState()
    {
        return modelState;
    }

    public void setModelState(List<ModelState> modelState)
    {
        this.modelState = modelState;
    }
}
package com.devinedesign.cleanride.domain;

import com.google.gson.annotations.SerializedName;


public class ModelState
{
    @SerializedName("ModelState")
    private String modelState;

    public ModelState(String modelState)
    {
        this.modelState = modelState;
    }

    public String getModelState() {
        return modelState;
    }

    public void setModelState(String modelState) {
        this.modelState = modelState;
    }
}

共有1个答案

赫连卓
2023-03-14

这里的ModelState是一个对象。

"ModelState": {

但是,您将它声明为列表,因此Gson试图解析数组

@SerializedName("ModelState")
private List<ModelState> modelState;

你可以用

@SerializedName("ModelState")
private ModelState modelState;
{
    "": [
      "Name c@gmail.com is already taken.",
      "Email 'c@gmail.com' is already taken."
    ]
}
 类似资料: