目前,我正在研究改造。我正在尝试解析我的JSON响应:
nvm install v6.9.5
目前,我将它作为我的API进行翻新:
{
"code": 0,
"message": "Success",
"billers": [
{
"billerCode": "001 ",
"billerName": "ABACUS BOOK CARD CORPORATION ",
"billerType": "C",
"label1": "Branch Code ",
"label2": "Origin of Deposit ",
"label3": "Collection Date ",
"label4": " ",
"label5": " "
},
{
"billerCode": "195 ",
"billerName": "ACCTN INC. ",
"billerType": "C",
"label1": "Account No. ",
"label2": "Subcriber's Name ",
"label3": null,
"label4": null,
"label5": null
},
目前不知道如何获得“代码”,“消息”和数组“帐单”以及数组帐单内部。我想获取“帐单代码”。
在研究改造时,我以网页 Retrofit Android Example – Fetching JSON from URL 作为参考。
更新:
我应用了您的答案,但是我仍然无法解析JSON内容。这是我的代码:
public interface Api {
String BASE_URL = "my url"
@GET("get-billers")
Call<List<Hero>> getHeroes();
}
答案 0 :(得分:2)
使用模型类并像这样调用您的方法:
@GET("get-billers")
Call<Billers> getBillers();
模型类别:
public class Biller {
@SerializedName("billerCode")
private String billerCode;
@SerializedName("billerName")
private String billerName;
@SerializedName("billerType")
private String billerType;
@SerializedName("label1")
private String label1;
@SerializedName("label2")
private String label2;
@SerializedName("label3")
private Object label3;
@SerializedName("label4")
private Object label4;
@SerializedName("label5")
private Object label5;
public String getBillerCode() {
return billerCode;
}
public void setBillerCode(String billerCode) {
this.billerCode = billerCode;
}
public String getBillerName() {
return billerName;
}
public void setBillerName(String billerName) {
this.billerName = billerName;
}
public String getBillerType() {
return billerType;
}
public void setBillerType(String billerType) {
this.billerType = billerType;
}
public String getLabel1() {
return label1;
}
public void setLabel1(String label1) {
this.label1 = label1;
}
public String getLabel2() {
return label2;
}
public void setLabel2(String label2) {
this.label2 = label2;
}
public Object getLabel3() {
return label3;
}
public void setLabel3(Object label3) {
this.label3 = label3;
}
public Object getLabel4() {
return label4;
}
public void setLabel4(Object label4) {
this.label4 = label4;
}
public Object getLabel5() {
return label5;
}
public void setLabel5(Object label5) {
this.label5 = label5;
}
}
---------------------- com.test.Billers.java ------------------ -
public class Billers {
@SerializedName("code")
private int code;
@SerializedName("message")
private String message;
@SerializedName("billers")
private List<Biller> billers = null;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<Biller> getBillers() {
return billers;
}
public void setBillers(List<Biller> billers) {
this.billers = billers;
}
}
更新: 像这样更新您的回复。
call.enqueue(new Callback<Billers>() {
@Override
public void onResponse(Call<Billers> call, Response<Billers> response) {
DebugUtils.log(""+response.message());
DebugUtils.log(""+response.body());
DebugUtils.log(""+response.toString());
Billers billers=response.body();
List<Biller> heroList = billers.getBillers();
DebugUtils.log(""+billers.getMessage());
for (int i = 0; i < heroList.size(); i++) {
DebugUtils.log(""+heroList.get(i).getBillerName());
}
}
@Override
public void onFailure(Call<Billers> call, Throwable t) {
}
});
答案 1 :(得分:0)
您可以像上面这样为上述JSON内容创建POJO类:
-----------------------------------com.example.Biller.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Biller {
@SerializedName("billerCode")
@Expose
private String billerCode;
@SerializedName("billerName")
@Expose
private String billerName;
@SerializedName("billerType")
@Expose
private String billerType;
@SerializedName("label1")
@Expose
private String label1;
@SerializedName("label2")
@Expose
private String label2;
@SerializedName("label3")
@Expose
private Object label3;
@SerializedName("label4")
@Expose
private Object label4;
@SerializedName("label5")
@Expose
private Object label5;
public String getBillerCode() {
return billerCode;
}
public void setBillerCode(String billerCode) {
this.billerCode = billerCode;
}
public String getBillerName() {
return billerName;
}
public void setBillerName(String billerName) {
this.billerName = billerName;
}
public String getBillerType() {
return billerType;
}
public void setBillerType(String billerType) {
this.billerType = billerType;
}
public String getLabel1() {
return label1;
}
public void setLabel1(String label1) {
this.label1 = label1;
}
public String getLabel2() {
return label2;
}
public void setLabel2(String label2) {
this.label2 = label2;
}
public Object getLabel3() {
return label3;
}
public void setLabel3(Object label3) {
this.label3 = label3;
}
public Object getLabel4() {
return label4;
}
public void setLabel4(Object label4) {
this.label4 = label4;
}
public Object getLabel5() {
return label5;
}
public void setLabel5(Object label5) {
this.label5 = label5;
}
}
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("code")
@Expose
private Integer code;
@SerializedName("message")
@Expose
private String message;
@SerializedName("billers")
@Expose
private List<Biller> billers = null;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<Biller> getBillers() {
return billers;
}
public void setBillers(List<Biller> billers) {
this.billers = billers;
}
}
API接口将如下所示:
public interface Api {
String BASE_URL = "my URL"
@GET("get-billers")
Call<List<Biller>> getBillers();
}
这是在Retrofit中使用POJO类的方法。请尝试像这样使用它。