如何使用google和我的json的gson将复杂的json转换为java对象是这样的:
{
"error": "200",
"status": "OK",
"BarList": {
"Bar1": {
"Name": "yash",
"sex": "male",
"Type": "barowner",
"userId": "x25df",
"ContactNo": "1234567890",
"zipCode": "110055",
"Address": "Ghumtarastachaltigali",
"Email": "nahihairee@gmail.com"
},
"Bar2": {
"Name": "yash",
"sex": "male",
"Type": "barowner",
"userId": "x25df",
"ContactNo": "1234567890",
"zipCode": "110055",
"Address": "Ghumtarastachaltigali",
"Email": "nahihairee@gmail.com"
}
}
}
为了将这个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 :(得分: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输出,则可以更改&#34; BarList&#34;到一个json数组:
{
"error": "200",
"status": "OK",
"BarList": [
{
"Name": "yash",
"sex": "male",
"Type": "barowner",
"userId": "x25df",
"ContactNo": "1234567890",
"zipCode": "110055",
"Address": "Ghumtarastachaltigali",
"Email": "nahihairee@gmail.com"
},
{
"Name": "yash",
"sex": "male",
"Type": "barowner",
"userId": "x25df",
"ContactNo": "1234567890",
"zipCode": "110055",
"Address": "Ghumtarastachaltigali",
"Email": "nahihairee@gmail.com"
}
]
}
并将响应类更改为以下内容:
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;
}
}
通过这种方法,您不需要额外的&#34; BarList&#34;类和列表可以包含&#34;无限&#34;条目没有改变你的内部类结构。