GSON解析JSON值

omhiaaxx  于 2023-01-27  发布在  其他
关注(0)|答案(2)|浏览(180)

我正在处理一个用Java编写的Web服务,我必须从JSON格式中获取响应。下面是JSON。我如何正确地获取值以便使用它们?

{
  "message": "string",
  "validationErrors": [
    {
      "code": "string",
      "message": "string"
    }
  ],
  "exceptionMessages": [
    {
      "code": "string",
      "message": "string"
    }
  ],
  "trailId": "string",
  "isSuccessful": true
}

这是我正在尝试的,但我得到了一个错误。

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;

Type listErrors = new TypeToken<List<Response>>() {}.getType();
List<Response> list = gson.fromJson(response.toString(), listErrors);

这是我在测试时得到的错误。

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

Response.java

import java.util.List;

public class Response{
    public Response() {
        super();
    }
    
    private String message;
    private List<ValidationErrors> errors;

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

    public String getMessage() {
        return message;
    }

    public void setErrors(List<ValidationErrors> errors) {
        this.errors = errors;
    }

    public List<ValidationErrors> getErrors() {
        return errors;
    }
}

ValidationErrors.java

public class ValidationErrors {
    public ValidationErrors() {
        super();
    }
    
    private String code;
    private String message;

    public void setCode(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }

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

    public String getMessage() {
        return message;
    }
}
c0vxltue

c0vxltue1#

当你的json响应是对象时,你会得到这个错误,你试图解析它,因为它是一个数组,但它不是..
这就是为什么它显示为com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
您应该按如下所述替换代码

Type listErrors = new TypeToken<List<Response>>() {}.getType();
List<Response> list = gson.fromJson(response.toString(), listErrors);

Response objectResponse = gson.fromJson(response.toString(), Response.class);
jc3wubiy

jc3wubiy2#

我的理解是正确的,您只得到一个对象,而不是List:

Response result = gson.fromJson(response.toString(), Response.class);

要正确Map对象,还必须重命名属性,以便它们适合json结构中的键名:

import java.util.List;

public class Response{
    public Response() {
        super();
    }
    
    private String message;
    private List<ValidationErrors> validationErrors;

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

    public String getMessage() {
        return message;
    }

    public void setValidationErrors(List<ValidationErrors> errors) {
        this.validationErrors = errors;
    }

    public List<ValidationErrors> getValidationErrors() {
        return errors;
    }
}

或者添加SerializedName注解:

import java.util.List;

public class Response{
    public Response() {
        super();
    }
    
    private String message;
    @SerializedName("validationErrors")
    private List<ValidationErrors> errors;

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

    public String getMessage() {
        return message;
    }

    public void setErrors(List<ValidationErrors> errors) {
        this.errors = errors;
    }

    public List<ValidationErrors> getErrors() {
        return errors;
    }
}

并且添加用于缺失密钥exceptionMessagestrailIdisSuccessful的属性。

相关问题