在Recyclerview中显示使用Gson从Json解析的数据

时间:2015-10-10 11:12:53

标签: android json gson android-recyclerview

我有这个Json字符串:

 {
  "Status": "true",
   "Result": {
    "rows": {
     "row": {
       "status": true,
        "subareas": [
        {
         "nome": "Associacao Utente",
         "id": 9,
         "grafs": {
          "rows": {
            "row": {
              "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"
            }
          ]
        }
      }
    ]
  }
}
}
}

使用Gson将其正确地反序列化为Pojo。下面我将向您展示我的解串器:

Example.java

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 + "]";
}

Result.java

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 + "]";
}

Rows.java

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 + "]";
}

Row.java

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 + "]";
}

Subarea.java

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
            + "]";
}

Grafs.java

public class Grafs {
private Row_[] rows;

public List<Row_> getRows() {
    return new LinkedList<Row_>( Arrays.asList(rows));
}
public void setRows(List<Row_> rows) {
    this.rows = (Row_[]) rows.toArray();
}
@Override
public String toString() {
    return "Grafs [rows=" + rows[0] + "]";
}
public static class Row_Deserializer implements JsonDeserializer<Row_[]>{
    @Override
    public Row_[] deserialize(JsonElement json, Type typeOfT,
    JsonDeserializationContext context) throws JsonParseException{
        if(json instanceof JsonArray){
            return new Gson().fromJson(json, Row_[].class);
        }
        System.out.println("ola");
        Rows1 child = context.deserialize(json, Rows1.class);
        System.out.println(child.getRow().getId());
        return new Row_[] {child.getRow()};
    }
}

Row_.java

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 + "]";
}

这可以很好地反序列化json。我需要帮助的是在recyclerview中显示一些这样的数据,以便我可以产生以下输出。我想用grafs名称显示每个子区域。第一个分区有一个grafs,第二个分区有grafs,你可以在输出中看到。

enter image description here

1 个答案:

答案 0 :(得分:0)

你可以用这一行解析你的json:

new Gson().fromJson(json, Example.class);

这是一种简单的方法。