Java 需要帮助创建使用GSON从JSON字符串映射到pojo的类吗
我有以下JSON字符串,应该解析为POJO:Java 需要帮助创建使用GSON从JSON字符串映射到pojo的类吗,java,json,gson,deserialization,Java,Json,Gson,Deserialization,我有以下JSON字符串,应该解析为POJO: { "Status": "true", "Result": { "rows": { "row": { "status": true, "subareas": [ { &q
{
"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 + "]";
}
}
公共类示例{
私有字符串状态;
私人结果;
公共字符串getStatus(){
返回状态;
}
公共无效设置状态(字符串状态){
状态=状态;
}
公共结果getResult(){
返回结果;
}
公共void setResult(结果){
结果=结果;
}
@凌驾
公共字符串toString(){
返回“Example[Status=“+Status+”,Result=“+Result+”]”;
}
}
公开课成绩{
私人行;
公共行getRows(){
返回行;
}
公共无效集合行(行){
this.rows=行;
}
@凌驾
公共字符串toString(){
返回“Result[rows=“+rows+”]”;
}
}
公共类行{
私人行;
公共行getRow(){
返回行;
}
公共无效集合行(行){
this.row=行;
}
@凌驾
公共字符串toString(){
返回“Rows[row=“+row+”]”;
}
}
导入java.util.ArrayList;
导入java.util.List;
公共类行{
私有布尔状态;
私有列表子区域=新的ArrayList();
公共布尔getStatus(){
返回状态;
}
公共无效设置状态(布尔状态){
这个状态=状态;
}
公共列表getSubreas(){
返回分区;
}
公共无效集合子区域(列出子区域){
this.subreas=子区域;
}
@凌驾
公共字符串toString(){
返回“Row[status=“+status+”,subreas=“+subreas+”]”;
}
}
公共类分区{
私有字符串名称;
私有整数id;
私人祖父;
公共字符串getNome(){
返回nome;
}
公共无效集合名称(字符串名称){
this.nome=nome;
}
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
公共Grafs getGrafs(){
还乡;
}
公共无效集Grafs(Grafs-Grafs){
this.grafs=grafs;
}
@凌驾
公共字符串toString(){
return“Subarea[nome=“+nome+”,id=“+id+”,grafs=“+grafs
+ "]";
}
}
导入java.util.ArrayList;
导入java.util.List;
公共课Grafs{
私有列表行=新的ArrayList();
公共列表getRows(){
返回行;
}
公共无效集合行(列表行){
this.rows=行;
}
@凌驾
公共字符串toString(){
返回“Grafs[rows=“+rows+”]”;
}
}
公共类排{
私有整数id;
私有字符串名称;
私有字符串服务;
私有字符串周期;
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
公共字符串getNome(){
返回nome;
}
公共无效集合名称(字符串名称){
this.nome=nome;
}
公共字符串getServ(){
返回服务;
}
公共无效设置服务(字符串服务){
this.serv=serv;
}
公共字符串getPeriodo(){
返回周期;
}
公共void setPeriodo(字符串periodo){
this.periodo=periodo;
}
@凌驾
公共字符串toString(){
返回“行”[id=“+id+”,nome=“+nome+”,serv=“+serv
+“,periodo=“+periodo+”]”;
}
}
我需要帮助将从JSON接收到的数据填充到按子区域划分的recyclerview。我对如何创建适配器感到困惑。首先,JSON应该具有对称性(请参见“子区域”键下的“grafs”键)- 在第一个值中,它是-
"grafs" : {
"rows" : {
"row" : {
"grafs" : {
"rows" : [
第二个值是-
"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 + "]";
}
}
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 + "]";
}
}
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);
}
}
Result.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 + "]";
}
}
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 + "]";
}
}
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);
}
}
Rows.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 + "]";
}
}
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 + "]";
}
}
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);
}
}
Row.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 + "]";
}
}
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 + "]";
}
}
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);
}
}
Grafs.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 + "]";
}
}
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 + "]";
}
}
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);
}
}
现在,您可以按如下方式进行测试-
Main.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 + "]";
}
}
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 + "]";
}
}
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应该具有对称性(请参见“子区域”键下的“grafs”键)- 在第一个值中,它是-
"grafs" : {
"rows" : {
"row" : {
"grafs" : {
"rows" : [
第二个值是-
"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 + "]";
}
}
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 + "]";
}
}
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);
}
}
Result.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 + "]";
}
}
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 + "]";
}
}
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);
}
}
Rows.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 + "]";
}
}
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 + "]";
}
}
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);
}
}
Row.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 + "]";
}
}
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 + "]";
}
}
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);
}
}
Grafs.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 + "]";
}
}
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 + "]";
}
}
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);
}
}
现在,您可以按如下方式进行测试-
Main.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 + "]";
}
}
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 + "]";
}
}
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应该具有对称性(请参见“子区域”键下的“grafs”键)- 在第一个值中,它是-
"grafs" : {
"rows" : {
"row" : {
"grafs" : {
"rows" : [
第二个值是-
"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 + "]";
}
}
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 + "]";
}
}
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);
}
}
Result.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 + "]";
}
}
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 + "]";
}
}
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);
}
}
Rows.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 + "]";
}
}
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 + "]";
}
}
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);
}
}
Row.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 + "]";
}
}
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 + "]";
}
}
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);
}
}
Grafs.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 + "]";
}
}
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 + "]";
}
}
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);
}
}
现在,您可以按如下方式进行测试-
Main.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 + "]";
}
}
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 + "]";
}
}
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应该具有对称性(请参见“子区域”键下的“grafs”键)- 在第一个值中,它是-
"grafs" : {
"rows" : {
"row" : {
"grafs" : {
"rows" : [
第二个值是-
"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 + "]";
}
}
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 + "]";
}
}
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);
}
}
Result.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 + "]";
}
}
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 + "]";
}
}
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);
}
}
Rows.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 + "]";
}
}
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 + "]";
}
}
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);
}
}
Row.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 + "]";
}
}
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 + "]";
}
}
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);
}
}
Grafs.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 + "]";
}
}
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 + "]";
}
}
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);
}
}
现在,您可以按如下方式进行测试-
Main.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 + "]";
}
}
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 + "]";
}
}
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]]]]]]]]]
我所看到的是一些Pojs,但没有代码可以将JSON转换成POJO。考虑使用JSON并检查它如何理解:我所看到的是一些POJOS,但没有代码可以将JSON转换成POJOS。请考虑使用JSON并检查这一点来理解如何:我所看到的是一些POJOS,但是没有代码会改变JSON。在Pojs中。考虑使用JSON并检查这一点,以了解如何:我所看到的是一些POJO,但没有代码可以将JSON转换成POJOS。请考虑使用JSON并检查这一点来理解如何:一个更哑:)问题。对称对JSON意味着什么?我已经编辑了我的问题,请再次检查并帮助我,我是