2014-10-04 43 views
0

如何使用谷歌的GSON複雜的JSON轉換爲Java對象複雜的JSON到Java對象轉換和我的JSON是一些這樣的事:如何使用GSON

{ 
    "error": "200", 
    "status": "OK", 
    "BarList": { 
     "Bar1": { 
      "Name": "yash", 
      "sex": "male", 
      "Type": "barowner", 
      "userId": "x25df", 
      "ContactNo": "1234567890", 
      "zipCode": "110055", 
      "Address": "Ghumtarastachaltigali", 
      "Email": "[email protected]" 
     }, 
     "Bar2": { 
      "Name": "yash", 
      "sex": "male", 
      "Type": "barowner", 
      "userId": "x25df", 
      "ContactNo": "1234567890", 
      "zipCode": "110055", 
      "Address": "Ghumtarastachaltigali", 
      "Email": "[email protected]" 
     } 
    } 
} 

對於這個JSON映射到我的java對象我取得了3類 第一:BarListResponse - 在我這樣做: -

public class BarListResponse { 

    @SerializedName("error") 
    @Expose(serialize = false) 
    String errrocode; 

    @Expose(serialize = false) 
    String status; 

    @SerializedName("data") 
    Bar bar_list[]; 

    public String getErrrocode() { 
     return errrocode; 
    } 

    public void setErrrocode(String errrocode) { 
     this.errrocode = errrocode; 
    } 

    public String getStatus() { 
     return status; 
    } 

    public void setStatus(String status) { 
     this.status = status; 
    } 

    public Bar[] getLst() { 
     return bar_list; 
    } 

    public void setLst(Bar lst[]) { 
     this.bar_list = lst; 
    } 
} 

第二欄列表:

public class BarList { 

    @SerializedName("Bar") 
    Bar bar[]; 

    public Bar[] getBar() { 
     return bar; 
    } 

    public void setBar1(Bar bar[]) { 
     this.bar = bar; 
    } 
} 

;第三是

public class Bar { 

    String Name; 
    String sex; 
    String type; 
    String userId; 
    double ContactNo; 
    double zipCode; 
    String Address; 
    String Email; 

    public String getName() { 
     return Name; 
    } 
    public void setName(String name) { 
     Name = name; 
    } 
    public String getSex() { 
     return sex; 
    } 
    public void setSex(String sex) { 
     this.sex = sex; 
    } 
    public String getType() { 
     return type; 
    } 
    public void setType(String type) { 
     this.type = type; 
    } 
    public String getUserId() { 
     return userId; 
    } 
    public void setUserId(String userId) { 
     this.userId = userId; 
    } 
    public double getContactNo() { 
     return ContactNo; 
    } 
    public void setContactNo(double contactNo) { 
     ContactNo = contactNo; 
    } 
    public double getZipCode() { 
     return zipCode; 
    } 
    public void setZipCode(double zipCode) { 
     this.zipCode = zipCode; 
    } 
    public String getAddress() { 
     return Address; 
    } 
    public void setAddress(String address) { 
     Address = address; 
    } 
    public String getEmail() { 
     return Email; 
    } 
    public void setEmail(String email) { 
     Email = email; 
    } 
} 

從這個我想通過一個獲取各條之一的細節。

請幫忙解決這個問題。 在此先感謝。

+0

它被命名爲BarList而不是數據。 @SerializedName(「data」) Bar bar_list []; – 2014-10-04 13:35:16

+0

你試過新的Gson()。fromJson(userinput,BarList.class); – 2018-02-08 15:41:21

回答

0

您不能將json對象({...})映射到java列表,因此您必須爲「list」對象創建一個自己的類來映射對象。這是非常不理想的,因爲你必須爲列表中的每個項目創建一個屬於自己的屬性,但是如果你無法控制json,你必須處理它。

的類應該看起來像那麼BarListResponse.java如下:

public class BarListResponse { 

    @SerializedName("error") 
    @Expose(serialize = false) 
    String errrocode; 

    @Expose(serialize = false) 
    String status; 

    @SerializedName("BarList") 
    BarList bar_list; 

    public String getErrrocode() { 
     return errrocode; 
    } 

    public void setErrrocode(String errrocode) { 
     this.errrocode = errrocode; 
    } 

    public String getStatus() { 
     return status; 
    } 

    public void setStatus(String status) { 
     this.status = status; 
    } 

    public BarList getLst() { 
     return bar_list; 
    } 

    public void setLst(BarList lst) { 
     this.bar_list = lst; 
    } 
} 

BarList.java

public class BarList { 
    Bar Bar1; 
    Bar Bar2; 
    ... 
} 

如果你以某種方式在JSON輸出控制,你可以改變將「BarList」添加到json數組中:

{ 
    "error": "200", 
    "status": "OK", 
    "BarList": [ 
     { 
      "Name": "yash", 
      "sex": "male", 
      "Type": "barowner", 
      "userId": "x25df", 
      "ContactNo": "1234567890", 
      "zipCode": "110055", 
      "Address": "Ghumtarastachaltigali", 
      "Email": "[email protected]" 
     }, 
     { 
      "Name": "yash", 
      "sex": "male", 
      "Type": "barowner", 
      "userId": "x25df", 
      "ContactNo": "1234567890", 
      "zipCode": "110055", 
      "Address": "Ghumtarastachaltigali", 
      "Email": "[email protected]" 
     } 
    ] 
} 

並更改響應類爲以下內容:

public class BarListResponse { 

    @SerializedName("error") 
    @Expose(serialize = false) 
    String errrocode; 

    @Expose(serialize = false) 
    String status; 

    @SerializedName("BarList") 
    List<Bar> bar_list; 

    public String getErrrocode() { 
     return errrocode; 
    } 

    public void setErrrocode(String errrocode) { 
     this.errrocode = errrocode; 
    } 

    public String getStatus() { 
     return status; 
    } 

    public void setStatus(String status) { 
     this.status = status; 
    } 

    public List<Bar> getLst() { 
     return bar_list; 
    } 

    public void setLst(List<Bar> lst) { 
     this.bar_list = lst; 
    } 
} 

有了這種方法,你不需要額外的「BarList」類和列表可以包含「無限」的條目,而不改變你內部的階級結構。