2015-10-05 27 views
0

請幫助我。我有以下的JSON字符串,至極我應該解析到POJO:需要幫助創建類使用GSON從json字符串映射到pojo的類

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

}

,我有這些類反序列化到POJO,至極的工作,感謝SAURABH:

public class Example { 
private String Status; 
private Result Result; 
public String getStatus() { 
    return Status; 
} 
public void setStatus(String status) { 
    Status = status; 
} 
public Result getResult() { 
    return Result; 
} 
public void setResult(Result result) { 
    Result = result; 
} 
@Override 
public String toString() { 
    return "Example [Status=" + Status + ", Result=" + Result + "]"; 
} 

} 


public class Result { 
private Rows rows; 

public Rows getRows() { 
    return rows; 
} 

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

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

} 


public class Rows { 
private Row row; 

public Row getRow() { 
    return row; 
} 
public void setRow(Row row) { 
    this.row = row; 
} 
@Override 
public String toString() { 
    return "Rows [row=" + row + "]"; 
} 
} 


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

public class Row { 
private Boolean status; 
private List<Subarea> subareas = new ArrayList<Subarea>(); 
public Boolean getStatus() { 
    return status; 
} 
public void setStatus(Boolean status) { 
    this.status = status; 
} 
public List<Subarea> getSubareas() { 
    return subareas; 
} 
public void setSubareas(List<Subarea> subareas) { 
    this.subareas = subareas; 
} 
@Override 
public String toString() { 
    return "Row [status=" + status + ", subareas=" + subareas + "]"; 
} 
} 


public class Subarea { 
private String nome; 
private Integer id; 
private Grafs grafs; 
public String getNome() { 
    return nome; 
} 
public void setNome(String nome) { 
    this.nome = nome; 
} 
public Integer getId() { 
    return id; 
} 
public void setId(Integer id) { 
    this.id = id; 
} 
public Grafs getGrafs() { 
    return grafs; 
} 
public void setGrafs(Grafs grafs) { 
    this.grafs = grafs; 
} 
@Override 
public String toString() { 
    return "Subarea [nome=" + nome + ", id=" + id + ", grafs=" + grafs 
      + "]"; 
} 
} 


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

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

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


public class Row_ { 
private Integer id; 
private String nome; 
private String serv; 
private String periodo; 
public Integer getId() { 
    return id; 
} 
public void setId(Integer id) { 
    this.id = id; 
} 
public String getNome() { 
    return nome; 
} 
public void setNome(String nome) { 
    this.nome = nome; 
} 
public String getServ() { 
    return serv; 
} 
public void setServ(String serv) { 
    this.serv = serv; 
} 
public String getPeriodo() { 
    return periodo; 
} 
public void setPeriodo(String periodo) { 
    this.periodo = periodo; 
} 
@Override 
public String toString() { 
    return "Row_ [id=" + id + ", nome=" + nome + ", serv=" + serv 
      + ", periodo=" + periodo + "]"; 
} 
} 

我需要幫助填充從JSON接收的數據到回收者視圖,除以子區域。我很困惑如何創建適配器。請幫幫我。

+1

我看到的都是一些POJO的,但沒有將JSON轉換爲POJO的代碼。 – hotzst

+0

考慮使用JSON和檢查瞭解如何:http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java –

回答

3

首先,你的JSON應具有對稱性(見「grafs」鑰匙「分區」項下) - 在第一個值是作爲 -

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

而在第二個值是作爲 -

"grafs" : { 
     "rows" : [ 

所以,我只是做了他們正確的,因爲 -

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

現在你可以創建類爲 -

Example.java

public class Example { 
    private String Status; 
    private Result Result; 
    public String getStatus() { 
     return Status; 
    } 
    public void setStatus(String status) { 
     Status = status; 
    } 
    public Result getResult() { 
     return Result; 
    } 
    public void setResult(Result result) { 
     Result = result; 
    } 
    @Override 
    public String toString() { 
     return "Example [Status=" + Status + ", Result=" + Result + "]"; 
    } 

} 

Result.java

public class Result { 
    private Rows rows; 

    public Rows getRows() { 
     return rows; 
    } 

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

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

} 

Rows.java

public class Rows { 
    private Row row; 

    public Row getRow() { 
     return row; 
    } 
    public void setRow(Row row) { 
     this.row = row; 
    } 
    @Override 
    public String toString() { 
     return "Rows [row=" + row + "]"; 
    } 
} 

Row.java

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

public class Row { 
    private Boolean status; 
    private List<Subarea> subareas = new ArrayList<Subarea>(); 
    public Boolean getStatus() { 
     return status; 
    } 
    public void setStatus(Boolean status) { 
     this.status = status; 
    } 
    public List<Subarea> getSubareas() { 
     return subareas; 
    } 
    public void setSubareas(List<Subarea> subareas) { 
     this.subareas = subareas; 
    } 
    @Override 
    public String toString() { 
     return "Row [status=" + status + ", subareas=" + subareas + "]"; 
    } 
} 

Subarea.java

public class Subarea { 
    private String nome; 
    private Integer id; 
    private Grafs grafs; 
    public String getNome() { 
     return nome; 
    } 
    public void setNome(String nome) { 
     this.nome = nome; 
    } 
    public Integer getId() { 
     return id; 
    } 
    public void setId(Integer id) { 
     this.id = id; 
    } 
    public Grafs getGrafs() { 
     return grafs; 
    } 
    public void setGrafs(Grafs grafs) { 
     this.grafs = grafs; 
    } 
    @Override 
    public String toString() { 
     return "Subarea [nome=" + nome + ", id=" + id + ", grafs=" + grafs 
       + "]"; 
    } 
} 

Grafs.java

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

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

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

Row_.java

public class Row_ { 
    private Integer id; 
    private String nome; 
    private String serv; 
    private String periodo; 
    public Integer getId() { 
     return id; 
    } 
    public void setId(Integer id) { 
     this.id = id; 
    } 
    public String getNome() { 
     return nome; 
    } 
    public void setNome(String nome) { 
     this.nome = nome; 
    } 
    public String getServ() { 
     return serv; 
    } 
    public void setServ(String serv) { 
     this.serv = serv; 
    } 
    public String getPeriodo() { 
     return periodo; 
    } 
    public void setPeriodo(String periodo) { 
     this.periodo = periodo; 
    } 
    @Override 
    public String toString() { 
     return "Row_ [id=" + id + ", nome=" + nome + ", serv=" + serv 
       + ", periodo=" + periodo + "]"; 
    } 
} 

現在,你可以按照以下測試此 -

Main.java

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

public class Main { 
    private static Gson gson; 

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

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

而且結果是 -

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

謝謝很多人...真的幫助。 –

+0

又一個愚蠢的:)問題。對於JSON來說,對稱性意味着什麼? –

+0

我已經編輯了詳細的答案。 – Saurabh