【问题标题】:Need help creating the classes to map from a JSON string to pojo using GSON需要帮助创建类以使用 GSON 从 JSON 字符串映射到 pojo
【发布时间】:2015-10-05 09:26:50
【问题描述】:

我有以下 JSON 字符串,我应该将其解析为 POJO:

{
"Status": "true",
"Result": {
    "rows": {
        "row": {
            "status": true,
            "subareas": [
                {
                    "nome": "Associacao Utente",
                    "id": 9,
                    "grafs": {
                        "rows": [
                            {
                                "id": 6,
                                "nome": "AssociacaoUtente",
                                "tipo": "PIE",
                                "serv": "MV_AS_UTENTE_POR_NEGOCIO",
                                "periodo": "ANO"
                            }
                        ]
                    }
                },
                {
                    "nome": "Chaves",
                    "id": 60,
                    "grafs": {
                        "rows": [
                            {
                                "id": 35,
                                "nome": "ChavesCriadosporano",
                                "tipo": "LINHA",
                                "serv": "MV_ASSOC_TOTAL_CHAVES",
                                "periodo": "ANO"
                            },
                            {
                                "id": 592,
                                "nome": "ChavesAssociadoAoUserPortal",
                                "tipo": "BAR",
                                "serv": "MV_ASSOC_USER_CHAVES",
                                "periodo": "TODOS"
                            },
                            {
                                "id": 593,
                                "nome": "ChavesAssociadoAoNegocios",
                                "tipo": "BAR",
                                "serv": "MV_ASSOC_CHAVES",
                                "periodo": "TODOS"
                            }
                        ]
                    }
                }
            ]
        }
    }
}
}

我有这些类可以反序列化为 POJO,这要感谢 Saurabh:

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


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

public class Row {
private Boolean status;
private List<Subarea> subareas = new ArrayList<Subarea>();
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 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
            + "]";
}
}


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

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

public List<Row_> getRows() {
    return rows;
}
public void setRows(List<Row_> rows) {
    this.rows = rows;
}
@Override
public String toString() {
    return "Grafs [rows=" + rows + "]";
}
}


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,按子区域划分。我对如何创建适配器感到困惑。

【问题讨论】:

标签: java json gson deserialization


【解决方案1】:

首先,您的 JSON 应该具有对称性(请参阅“subareas”键下的“grafs”键)- 在第一个值中,它是 -

"grafs" : {
    "rows" : {
        "row" : {

第二个值是 -

"grafs" : {
      "rows" : [

所以,我只是把它们改成了-

{
    "Status": "true",
    "Result": {
        "rows": {
            "row": {
                "status": true,
                "subareas": [
                    {
                        "nome": "Associacao Utente",
                        "id": 9,
                        "grafs": {
                            "rows": [
                                {
                                    "id": 6,
                                    "nome": "AssociacaoUtente",
                                    "tipo": "PIE",
                                    "serv": "MV_AS_UTENTE_POR_NEGOCIO",
                                    "periodo": "ANO"
                                }
                            ]
                        }
                    },
                    {
                        "nome": "Chaves",
                        "id": 60,
                        "grafs": {
                            "rows": [
                                {
                                    "id": 35,
                                    "nome": "ChavesCriadosporano",
                                    "tipo": "LINHA",
                                    "serv": "MV_ASSOC_TOTAL_CHAVES",
                                    "periodo": "ANO"
                                },
                                {
                                    "id": 592,
                                    "nome": "ChavesAssociadoAoUserPortal",
                                    "tipo": "BAR",
                                    "serv": "MV_ASSOC_USER_CHAVES",
                                    "periodo": "TODOS"
                                },
                                {
                                    "id": 593,
                                    "nome": "ChavesAssociadoAoNegocios",
                                    "tipo": "BAR",
                                    "serv": "MV_ASSOC_CHAVES",
                                    "periodo": "TODOS"
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
}

现在您可以将类创建为 -

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

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

public class Row {
    private Boolean status;
    private List<Subarea> subareas = new ArrayList<Subarea>();
    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

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

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

    public List<Row_> getRows() {
        return rows;
    }
    public void setRows(List<Row_> rows) {
        this.rows = rows;
    }
    @Override
    public String toString() {
        return "Grafs [rows=" + rows + "]";
    }
}

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

现在,您可以按如下方式进行测试 -

Main.java

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.testgson.beans.Example;

public class Main {
    private static Gson gson;

    static {
        gson = new GsonBuilder().create();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
         String j = "{\"Status\":\"true\",\"Result\":{\"rows\":{\"row\":{\"status\":true,\"subareas\":[{\"nome\":\"Associacao Utente\",\"id\":9,\"grafs\":{\"rows\":[{\"id\":6,\"nome\":\"AssociacaoUtente\",\"tipo\":\"PIE\",\"serv\":\"MV_AS_UTENTE_POR_NEGOCIO\",\"periodo\":\"ANO\"}]}},{\"nome\":\"Chaves\",\"id\":60,\"grafs\":{\"rows\":[{\"id\":35,\"nome\":\"ChavesCriadosporano\",\"tipo\":\"LINHA\",\"serv\":\"MV_ASSOC_TOTAL_CHAVES\",\"periodo\":\"ANO\"},{\"id\":592,\"nome\":\"ChavesAssociadoAoUserPortal\",\"tipo\":\"BAR\",\"serv\":\"MV_ASSOC_USER_CHAVES\",\"periodo\":\"TODOS\"},{\"id\":593,\"nome\":\"ChavesAssociadoAoNegocios\",\"tipo\":\"BAR\",\"serv\":\"MV_ASSOC_CHAVES\",\"periodo\":\"TODOS\"}]}}]}}}}";
         Example r = gson.fromJson(j, Example.class);
         System.out.println(r);
    }
}

结果是 -

Example [Status=true, Result=Result [rows=Rows [row=Row [status=true, subareas=[Subarea [nome=Associacao Utente, id=9, grafs=Grafs [rows=[Row_ [id=6, nome=AssociacaoUtente, serv=MV_AS_UTENTE_POR_NEGOCIO, periodo=ANO]]]], Subarea [nome=Chaves, id=60, grafs=Grafs [rows=[Row_ [id=35, nome=ChavesCriadosporano, serv=MV_ASSOC_TOTAL_CHAVES, periodo=ANO], Row_ [id=592, nome=ChavesAssociadoAoUserPortal, serv=MV_ASSOC_USER_CHAVES, periodo=TODOS], Row_ [id=593, nome=ChavesAssociadoAoNegocios, serv=MV_ASSOC_CHAVES, periodo=TODOS]]]]]]]]]

【讨论】:

  • 还有一个愚蠢的 :) 问题。对称对 JSON 意味着什么?
  • 我已经编辑了我的问题,请再次检查并帮助我,我真的需要帮助,我不能放弃。不用担心反序列化,已经可以了。我现在需要的是在 recyclerview 中显示数据。我该怎么做?
  • 我没有接受正确的答案,因为我无法触摸 json 字符串。 json 来自服务器,应该按原样处理。我只能以编程方式更改结构,这是我的问题。我仍然无法做到。任何帮助,请。
  • @Saurabh 我了解您对 json 字符串所做的更改,但正如我告诉您的那样,我只能以编程方式修改字符串。关于如何做的任何线索?我需要做你做的同样的事情,但不同。也许是一些字符串方法。
  • 尝试与上述类相同的 JSON 字符串。更改 Main.java 中的 JSON 字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多