gpt4 book ai didi

java - 需要帮助创建类以使用 GSON 从 JSON 字符串映射到 pojo

转载 作者:行者123 更新时间:2023-12-01 22:05:04 24 4
gpt4 key购买 nike

我有以下 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 接收到的数据填充到回收器 View 中,并按子区域划分。我对如何创建适配器感到困惑。

最佳答案

首先,您的 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"
}
]
}
}
]
}
}
}
}

现在您可以创建类 -

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

}

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

}

行.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]]]]]]]]]

关于java - 需要帮助创建类以使用 GSON 从 JSON 字符串映射到 pojo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32945173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com