应为 GSON BEGIN_ARRAY 但实际为 STRING

GSON Expected BEGIN_ARRAY but was STRING

我正在尝试将 gson 字符串解析为我的 java class,但由于照片的原因,我一直收到异常。

我正在使用 MySQL,图像使用 Blob 列。

我的javaClass在Android和服务器是这样的

public class Cliente {

    private Integer id;
    private byte[] foto;
    private String nome;
    private Date data_nascimento;
    private String endereco;
    private String telefone;
    private boolean ativo;
    private Date data_cadastro;

    public Integer getId() {
        return id;
    }

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

    public byte[] getFoto() {
        return foto;
    }

    public void setFoto(byte[] foto) {
        this.foto = foto;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public Date getData_nascimento() {
        return data_nascimento;
    }

    public void setData_nascimento(Date data_nascimento) {
        this.data_nascimento = data_nascimento;
    }

    public String getEndereco() {
        return endereco;
    }

    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }

    public String getTelefone() {
        return telefone;
    }

    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }

    public boolean isAtivo() {
        return ativo;
    }

    public void setAtivo(boolean ativo) {
        this.ativo = ativo;
    }

    public Date getData_cadastro() {
        return data_cadastro;
    }

    public void setData_cadastro(Date data_cadastro) {
        this.data_cadastro = data_cadastro;
    }


}

JSON 字符串列表(由于字符有限,照片字段为 NULL)

[{"id":1,"foto":null,"nome":"Guilherme","data_nascimento":"1993-12-23","endereco":"Rua Jornalista Angelo Zanuzzi, 560","telefone":"37055056","ativo":true,"data_cadastro":"2015-08-26"},{"id":2,"foto":null,"nome":"Lucas","data_nascimento":"2015-09-02","endereco":"Rua Jornalista Angelo Zanuzzi, 560","telefone":"37055056","ativo":true,"data_cadastro":"2015-08-26"}]

正在解析

ResponseEntity<?> responseEntity = Rest.makeRequest("services/clientes", HttpMethod.GET, null);
clienteList = gson.fromJson(responseEntity.getBody().toString(), new TypeToken<List<Cliente>>() {

在照片字段为 NULL 的情况下,gson 表现很好,但是当图像来自我的数据库时,它会抛出以下异常

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 18 path $[0].foto

我不知道如何将我的字符串解析为字节数组!

编辑

我的JSON是对的,复制/粘贴的时候忘了放[].

这是 Json 的开头,图像(blob)来自 mysql/hibernate

由于长度

,我不能post整个代码
[{"id":1,"foto":"/9j/4AAQSkZJRgABAgAAAQABAAD/7QA2UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAABkcAmcAFEphUnk5alpFY3RFUWo5Zy1adUxSAP/iC/hJQ0NfUFJPRklMRQABAQAAC+gAAAAAAgAAA

然后,当 GSON 尝试转换时,出现错误。

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 18 path $[0].foto

问题是,HIBERNATE 将照片作为字符串而不是 BYTE[] Array

然后,当我尝试解析时,GSON 抛出异常。 如果我将 Android 模型照片更改为字符串(私有字符串照片),gson 会正确解析,但我不知道如何将此 "byte string" 放入图像视图中。

我想接收字节[]以将其设置为图像视图

解决方案

实际上,Hibernate 将照片数据作为字符串发回,所以我必须将 android 中的模型更改为

private String foto;

而不是

private byte[] foto;

并且,在将其转换为我的 ImageView 之前,我必须使用

对字符串进行解码
byte[] bytes = Base64.decode(itemCardCliente.getFoto(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
imageView.setImageBitmap(bitmap);