将字段解析为对象或数组时出错

时间:2015-10-08 00:27:55

标签: java arrays json gson

我有以下Json字符串,我想要反序列化。问题是:由于这个字符串来自服务器,我无法改变它,我需要反序列化为POJO。您可以看到Grafs键对每个子区域具有不同的值。一个是对象,另一个是数组。我怎么处理这个?

{
"Status": "true",
"Result: {
    "rows": {
        "row": {
            "status": true,
            "subareas": [
                {
                    "nome": "Associacao Utente",
                    "id": 9,
                    "grafs": {
                        "rows": {
                            "id": 6,
                            "nome": "Associacao Utente",
                            "tipo": "PIE",
                            "serv": "MV_AS_UTENTE_POR_NEGOCIO",
                            "periodo": "ANO"
                        }
                    }
                }, {
                    "nome": "Chaves",
                    "id": 60,
                    "grafs": {
                        "rows": [
                            {
                                "id": 35,
                                "nome": "Chaves Criados por ano",
                                "tipo": "LINHA",
                                "serv": "MV_ASSOC_TOTAL_CHAVES",
                                "periodo": "ANO"
                            }, {
                                "id": 592,
                                "nome": "Chaves Associado Ao User Portal",
                                "tipo": "BAR",
                                "serv": "MV_ASSOC_USER_CHAVES",
                                "periodo": "TODOS"
                            }, {
                                "id": 593,
                                "nome": "Chaves Associado Ao Negocios",
                                "tipo": "BAR",
                                "serv": "MV_ASSOC_CHAVES",
                                "periodo": "TODOS"
                            }
                        ]
                    }
                }
            ]
        }
    }
}

}

以下是我的课程。

public class Example {
    private String Status;
    private Result Result;

    public String getStatus() {
        return Status;
    }

    public void setStatus(String status) {
        Status = status;
    }

    public Result getResult() {
        return Result;
    }

    public void setResult(Result result) {
        Result = result;
    }

    @Override
    public String toString() {
        return "Example [Status=" + Status + ", Result=" + Result + "]";
    }

}

public class Result {
    private Rows rows;

    public Rows getRows() {
        return rows;
    }

    public void setRows(Rows rows) {
        this.rows = rows;
    }

    @Override
    public String toString() {
        return "Result [rows=" + rows + "]";
    }
}

public class Grafs {
    private List<Rows_> rows = new ArrayList<>();

    public List<Rows_> getRows() {

        return rows;
    }

    public void setRows(List<Rows_> Rows) {
        this.rows = Rows;
    }


    @Override
    public String toString() {
        return "Grafs [rows=" + rows + "]";
    }
}

public class Row {
    private Boolean status;
    private List<Subarea> subareas = new ArrayList<>();

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public List<Subarea> getSubareas() {
        return subareas;
    }

    public void setSubareas(List<Subarea> subareas) {
        this.subareas = subareas;
    }

    @Override
    public String toString() {
        return "Row [status=" + status + ", subareas=" + subareas + "]";
    }
}

public class Row_ {
    private Integer id;
    private String nome;
    private String serv;
    private String periodo;

    public Integer getId() {
        return id;
    }

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

    public String getNome() {
        return nome;
    }

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

    public String getServ() {
        return serv;
    }

    public void setServ(String serv) {
        this.serv = serv;
    }

    public String getPeriodo() {
        return periodo;
    }

    public void setPeriodo(String periodo) {
        this.periodo = periodo;
    }

    @Override
    public String toString() {
        return "Row_ [id=" + id + ", nome=" + nome + ", serv=" + serv
               + ", periodo=" + periodo + "]";
    }
}

public class Rows {
    private Row row;

    public Row getRow() {
        return row;
    }

    public void setRow(Row row) {
        this.row = row;
    }

    @Override
    public String toString() {
        return "Rows [row=" + row + "]";
    }
}

public class Rows_ {
    private Row_ row;

    public Row_ getRow() {
        return row;
    }

    public void setRow(Row_ row) {
        this.row = row;
    }
}

public class Subarea {
    private String nome;
    private Integer id;

    private Grafs grafs;

    public String getNome() {
        return nome;
    }

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

    public Integer getId() {
        return id;
    }

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

    public Grafs getGrafs() {
        return grafs;
    }

    public void setGrafs(Grafs grafs) {
        this.grafs = grafs;
    }

    @Override
    public String toString() {
        return "Subarea [nome=" + nome + ", id=" + id + ", grafs=" + grafs
               + "]";
    }
}

使用这些类我收到以下错误:

Expected BEGIN_ARRAY  but was BEGIN_OBJECT at line 13 column 18.

我将Rows_声明为arraylist并且遇到了一个对象。但第二个Rows_确实是一个阵列。我该如何解决这个问题?

具有一个元素的数组仍应呈现为数组。这就是我使用数组的原因。但是它给出了我描述的错误。

感谢您的帮助。我真的很感激。

1 个答案:

答案 0 :(得分:1)

您可以使用TypeAdapterFactory进行转换。这是一个将该功能添加到所有List成员类型的工厂 -

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;

public class SingletonListTypeAdapterFactory implements TypeAdapterFactory {
     public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {

       Type type = typeToken.getType();
       if (typeToken.getRawType() != List.class
           || !(type instanceof ParameterizedType)) {
         return null;
       }
       Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0];
       TypeAdapter<?> elementAdapter = gson.getAdapter(TypeToken.get(elementType));
       TypeAdapter<T> arrayAdapter = gson.getDelegateAdapter(this, typeToken);
       return (TypeAdapter<T>) newSingtonListAdapter((TypeAdapter<Object>) elementAdapter, (TypeAdapter<List<Object>>) arrayAdapter);
     }

     private <E> TypeAdapter<List<E>> newSingtonListAdapter(
         final TypeAdapter<E> elementAdapter,
         final TypeAdapter<List<E>> arrayAdapter) {
       return new TypeAdapter<List<E>>() {

         public void write(JsonWriter out, List<E> value) throws IOException {
           if(value == null || value.isEmpty()) {
             out.nullValue();
           } else if(value.size() == 1) {
            elementAdapter.write(out, value.get(0));
           } else {
             arrayAdapter.write(out, value);
           }
         }

         public List<E> read(JsonReader in) throws IOException {
           if (in.peek() != JsonToken.BEGIN_ARRAY) {
             E obj = elementAdapter.read(in);
             return Collections.singletonList(obj);
           }
           return arrayAdapter.read(in);
         }
       };
     }
   }

作为奖励,如果需要,它也会以相同的方式序列化。如果您还想序列化为数组,请通过调用write替换arrayAdapter.write方法。

给你,在建设时加入你的gson -

Gson gson = new GsonBuilder().registerTypeAdapterFactory(new SingletonListTypeAdapterFactory())
        .create();