使用多个数组节点

时间:2016-01-14 04:44:39

标签: android json

{
    "CompanyDtls": [{
        "nComRefNo": 1,
        "cName": "HOLIDAY DEMO",
        "cSHName": "HDEM",
        "cAddress1": "XXXX-XXXX",
        "cAddress2": "XXX-SSSS",
        "cAddress3": "XXXXX-XXXXX",
        "nCreatedBy": 0,
        "dCreatedDate": "\/Date(1274725800000+0530)\/",
        "nModifiedBy": 0,
        "dModifiedDate": "\/Date(1408645800000+0530)\/",
        "bCancelled": "0         ",
        "bActive": true,
        "cLTNO": "LT:1522552522",
        "cSTNo": "SR.NO:3255255",
        "cPhoneNo": "XXXX-XXXXX",
        "cTIN": "PAN:25522451 TAN:25522451"
    }],
    "ReportDtls": [{
                "Srl": "",
                "Date": "\/Date(1421605800000+0530)\/",
                "BillFolio": "223/0",
                "GRCNo": 432236,
                "Guest": "ASSDSS",
                "Room": "208",
                "Charge": 1650807.840,
                "Refund": 0.000,
                "Disc": 0.000,
                "Tax": 328751.700,
                "Gross": 1979559.540,
                "Advance": 1111.000,
                "Tips": 0.000,
                "AddCharge": 0.000,
                "RoundOff": 0.460,
                "CashAmt": 0.000,
                "CreditAmt": 1978449.000,
                "BillAmt": 1978449.000,
                "User": "ADMIN",
                "nFinalBillId": 3417
            }, {
                "Srl": "",
                "Date": "\/Date(1422383400000+0530)\/",
                "BillFolio": "225/1",
                "GRCNo": 432287,
                "Guest": "Agent",
                "Room": "208",
                "Charge": 8340.540,
                "Refund": 0.000,
                "Disc": 0.000,
                "Tax": 1659.470,
                "Gross": 10000.010,
                "Advance": 0.000,
                "Tips": 0.000,
                "AddCharge": 0.000,
                "RoundOff": -0.010,
                "CashAmt": 10000.000,
                "CreditAmt": 0.000,
                "BillAmt": 10000.000,
                "User": "ADMIN",
                "nFinalBillId": 3419
            }]

我是android编程的新手,我想知道,我如何解析一个有这样两个数组节点的json?我经历了一些例子,但找不到我想要的东西。

提前感谢。

3 个答案:

答案 0 :(得分:2)

我认为你在那两个JSONArray“CompanyDtls”和“ReportDtls”中有一个JSONObject,如果是这样的话:

创建Model类以保存从JSON解析的值

CompantDetails.java (用于保存和解析公司详细信息的模型类)

public class CompantDetails {

int companyRefNo,nCreatedBy;
String companyName,cmpyAddress1,cmpyAddress2,cmpyAddress3;


public CompantDetails() {
    // TODO Auto-generated constructor stub
    this.companyRefNo =0;
    this.companyName="";
    this.cmpyAddress1="";
    this.cmpyAddress2="";
    this.cmpyAddress3="";
    this.nCreatedBy=0;

}

public CompantDetails(JSONObject jsCmpy){


    try {
        this.companyRefNo =jsCmpy.getInt("nComRefNo");
        this.companyName= jsCmpy.getString("cName");
        this.cmpyAddress1= jsCmpy.getString("cAddress1");
        this.cmpyAddress2= jsCmpy.getString("cAddress2");
        this.cmpyAddress3= jsCmpy.getString("cAddress3");
        this.nCreatedBy=jsCmpy.getInt("nCreatedBy");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 }
}

同样为 ReportDetails 创建模型类。

在主代码中添加以下代码:

JSONObject jsOb = new JSONObject("YOUR_FIRST_JSON");
JSONArray jscompanydts = jsOb.getJSONArray("CompanyDtls");
ArrayList<CompantDetails> cmpyDeList= new ArrayList<CompantDetails>();
for(int i =0;i<jscompanydts .length();i++){
JSONObject jsCompany = jscompanydts.getJSONObject(i);
CompantDetails cDetails = new CompantDetails (jsCompany);
cmpyDeList.add(cDetails);
 }

答案 1 :(得分:1)

好吧,我会指导你如何做到这一点。基本上就是我解析json的方式。

首先,我借助 JSONEditor Chrome应用程序检查json是否有效。 JSONEditor的Link

然后我复制这个结构,并使用另一个在线工具网站json to pojo将此json转换为pojos。

然后我在我的android项目中映射模型。然后,当我将JSON作为字符串时,我使用gson libaray将json转换为jave对象。

网站

Link

GSON的

Link

我就是这样做的。但我建议你首先应该尝试使用任何libaray json到java对象。只需映射模型并设置值,然后获取值。简单!祝你好运

答案 2 :(得分:-1)

在这种情况下,您需要创建模型类来将json值存储到java对象中,如果您使用Gson进行解析,那么您可以像这样创建POJO:

<强> CompanyDtl.class

    public class CompanyDtl {

@SerializedName("nComRefNo")
@Expose
private Integer nComRefNo;
@SerializedName("cName")
@Expose
private String cName;
@SerializedName("cSHName")
@Expose
private String cSHName;
@SerializedName("cAddress1")
@Expose
private String cAddress1;
@SerializedName("cAddress2")
@Expose
private String cAddress2;
@SerializedName("cAddress3")
@Expose
private String cAddress3;
@SerializedName("nCreatedBy")
@Expose
private Integer nCreatedBy;
@SerializedName("dCreatedDate")
@Expose
private String dCreatedDate;
@SerializedName("nModifiedBy")
@Expose
private Integer nModifiedBy;
@SerializedName("dModifiedDate")
@Expose
private String dModifiedDate;
@SerializedName("bCancelled")
@Expose
private String bCancelled;
@SerializedName("bActive")
@Expose
private Boolean bActive;
@SerializedName("cLTNO")
@Expose
private String cLTNO;
@SerializedName("cSTNo")
@Expose
private String cSTNo;
@SerializedName("cPhoneNo")
@Expose
private String cPhoneNo;
@SerializedName("cTIN")
@Expose
private String cTIN;

/**
*
* @return
* The nComRefNo
*/
public Integer getNComRefNo() {
return nComRefNo;
}

/**
*
* @param nComRefNo
* The nComRefNo
*/
public void setNComRefNo(Integer nComRefNo) {
this.nComRefNo = nComRefNo;
}

/**
*
* @return
* The cName
*/
public String getCName() {
return cName;
}

/**
*
* @param cName
* The cName
*/
public void setCName(String cName) {
this.cName = cName;
}

/**
*
* @return
* The cSHName
*/
public String getCSHName() {
return cSHName;
}

/**
*
* @param cSHName
* The cSHName
*/
public void setCSHName(String cSHName) {
this.cSHName = cSHName;
}

/**
*
* @return
* The cAddress1
*/
public String getCAddress1() {
return cAddress1;
}

/**
*
* @param cAddress1
* The cAddress1
*/
public void setCAddress1(String cAddress1) {
this.cAddress1 = cAddress1;
}

/**
*
* @return
* The cAddress2
*/
public String getCAddress2() {
return cAddress2;
}

/**
*
* @param cAddress2
* The cAddress2
*/
public void setCAddress2(String cAddress2) {
this.cAddress2 = cAddress2;
}

/**
*
* @return
* The cAddress3
*/
public String getCAddress3() {
return cAddress3;
}

/**
*
* @param cAddress3
* The cAddress3
*/
public void setCAddress3(String cAddress3) {
this.cAddress3 = cAddress3;
}

/**
*
* @return
* The nCreatedBy
*/
public Integer getNCreatedBy() {
return nCreatedBy;
}

/**
*
* @param nCreatedBy
* The nCreatedBy
*/
public void setNCreatedBy(Integer nCreatedBy) {
this.nCreatedBy = nCreatedBy;
}

/**
*
* @return
* The dCreatedDate
*/
public String getDCreatedDate() {
return dCreatedDate;
}

/**
*
* @param dCreatedDate
* The dCreatedDate
*/
public void setDCreatedDate(String dCreatedDate) {
this.dCreatedDate = dCreatedDate;
}

/**
*
* @return
* The nModifiedBy
*/
public Integer getNModifiedBy() {
return nModifiedBy;
}

/**
*
* @param nModifiedBy
* The nModifiedBy
*/
public void setNModifiedBy(Integer nModifiedBy) {
this.nModifiedBy = nModifiedBy;
}

/**
*
* @return
* The dModifiedDate
*/
public String getDModifiedDate() {
return dModifiedDate;
}

/**
*
* @param dModifiedDate
* The dModifiedDate
*/
public void setDModifiedDate(String dModifiedDate) {
this.dModifiedDate = dModifiedDate;
}

/**
*
* @return
* The bCancelled
*/
public String getBCancelled() {
return bCancelled;
}

/**
*
* @param bCancelled
* The bCancelled
*/
public void setBCancelled(String bCancelled) {
this.bCancelled = bCancelled;
}

/**
*
* @return
* The bActive
*/
public Boolean getBActive() {
return bActive;
}

/**
*
* @param bActive
* The bActive
*/
public void setBActive(Boolean bActive) {
this.bActive = bActive;
}

/**
*
* @return
* The cLTNO
*/
public String getCLTNO() {
return cLTNO;
}

/**
*
* @param cLTNO
* The cLTNO
*/
public void setCLTNO(String cLTNO) {
this.cLTNO = cLTNO;
}

/**
*
* @return
* The cSTNo
*/
public String getCSTNo() {
return cSTNo;
}

/**
*
* @param cSTNo
* The cSTNo
*/
public void setCSTNo(String cSTNo) {
this.cSTNo = cSTNo;
}

/**
*
* @return
* The cPhoneNo
*/
public String getCPhoneNo() {
return cPhoneNo;
}

/**
*
* @param cPhoneNo
* The cPhoneNo
*/
public void setCPhoneNo(String cPhoneNo) {
this.cPhoneNo = cPhoneNo;
}

/**
*
* @return
* The cTIN
*/
public String getCTIN() {
return cTIN;
}

/**
*
* @param cTIN
* The cTIN
*/
public void setCTIN(String cTIN) {
this.cTIN = cTIN;
}

}

<强> MainPojo.class

    public class MainPojo {

@SerializedName("CompanyDtls")
@Expose
private List<CompanyDtl> CompanyDtls = new ArrayList<CompanyDtl>();
@SerializedName("ReportDtls")
@Expose
private List<ReportDtl> ReportDtls = new ArrayList<ReportDtl>();

/**
*
* @return
* The CompanyDtls
*/
public List<CompanyDtl> getCompanyDtls() {
return CompanyDtls;
}

/**
*
* @param CompanyDtls
* The CompanyDtls
*/
public void setCompanyDtls(List<CompanyDtl> CompanyDtls) {
this.CompanyDtls = CompanyDtls;
}

/**
*
* @return
* The ReportDtls
*/
public List<ReportDtl> getReportDtls() {
return ReportDtls;
}

/**
*
* @param ReportDtls
* The ReportDtls
*/
public void setReportDtls(List<ReportDtl> ReportDtls) {
this.ReportDtls = ReportDtls;
}

}

<强> ReportDtl.class

public class ReportDtl {

@SerializedName("Srl")
@Expose
private String Srl;
@SerializedName("Date")
@Expose
private String Date;
@SerializedName("BillFolio")
@Expose
private String BillFolio;
@SerializedName("GRCNo")
@Expose
private Integer GRCNo;
@SerializedName("Guest")
@Expose
private String Guest;
@SerializedName("Room")
@Expose
private String Room;
@SerializedName("Charge")
@Expose
private Double Charge;
@SerializedName("Refund")
@Expose
private Double Refund;
@SerializedName("Disc")
@Expose
private Double Disc;
@SerializedName("Tax")
@Expose
private Double Tax;
@SerializedName("Gross")
@Expose
private Double Gross;
@SerializedName("Advance")
@Expose
private Double Advance;
@SerializedName("Tips")
@Expose
private Double Tips;
@SerializedName("AddCharge")
@Expose
private Double AddCharge;
@SerializedName("RoundOff")
@Expose
private Double RoundOff;
@SerializedName("CashAmt")
@Expose
private Double CashAmt;
@SerializedName("CreditAmt")
@Expose
private Double CreditAmt;
@SerializedName("BillAmt")
@Expose
private Double BillAmt;
@SerializedName("User")
@Expose
private String User;
@SerializedName("nFinalBillId")
@Expose
private Integer nFinalBillId;

/**
*
* @return
* The Srl
*/
public String getSrl() {
return Srl;
}

/**
*
* @param Srl
* The Srl
*/
public void setSrl(String Srl) {
this.Srl = Srl;
}

/**
*
* @return
* The Date
*/
public String getDate() {
return Date;
}

/**
*
* @param Date
* The Date
*/
public void setDate(String Date) {
this.Date = Date;
}

/**
*
* @return
* The BillFolio
*/
public String getBillFolio() {
return BillFolio;
}

/**
*
* @param BillFolio
* The BillFolio
*/
public void setBillFolio(String BillFolio) {
this.BillFolio = BillFolio;
}

/**
*
* @return
* The GRCNo
*/
public Integer getGRCNo() {
return GRCNo;
}

/**
*
* @param GRCNo
* The GRCNo
*/
public void setGRCNo(Integer GRCNo) {
this.GRCNo = GRCNo;
}

/**
*
* @return
* The Guest
*/
public String getGuest() {
return Guest;
}

/**
*
* @param Guest
* The Guest
*/
public void setGuest(String Guest) {
this.Guest = Guest;
}

/**
*
* @return
* The Room
*/
public String getRoom() {
return Room;
}

/**
*
* @param Room
* The Room
*/
public void setRoom(String Room) {
this.Room = Room;
}

/**
*
* @return
* The Charge
*/
public Double getCharge() {
return Charge;
}

/**
*
* @param Charge
* The Charge
*/
public void setCharge(Double Charge) {
this.Charge = Charge;
}

/**
*
* @return
* The Refund
*/
public Double getRefund() {
return Refund;
}

/**
*
* @param Refund
* The Refund
*/
public void setRefund(Double Refund) {
this.Refund = Refund;
}

/**
*
* @return
* The Disc
*/
public Double getDisc() {
return Disc;
}

/**
*
* @param Disc
* The Disc
*/
public void setDisc(Double Disc) {
this.Disc = Disc;
}

/**
*
* @return
* The Tax
*/
public Double getTax() {
return Tax;
}

/**
*
* @param Tax
* The Tax
*/
public void setTax(Double Tax) {
this.Tax = Tax;
}

/**
*
* @return
* The Gross
*/
public Double getGross() {
return Gross;
}

/**
*
* @param Gross
* The Gross
*/
public void setGross(Double Gross) {
this.Gross = Gross;
}

/**
*
* @return
* The Advance
*/
public Double getAdvance() {
return Advance;
}

/**
*
* @param Advance
* The Advance
*/
public void setAdvance(Double Advance) {
this.Advance = Advance;
}

/**
*
* @return
* The Tips
*/
public Double getTips() {
return Tips;
}

/**
*
* @param Tips
* The Tips
*/
public void setTips(Double Tips) {
this.Tips = Tips;
}

/**
*
* @return
* The AddCharge
*/
public Double getAddCharge() {
return AddCharge;
}

/**
*
* @param AddCharge
* The AddCharge
*/
public void setAddCharge(Double AddCharge) {
this.AddCharge = AddCharge;
}

/**
*
* @return
* The RoundOff
*/
public Double getRoundOff() {
return RoundOff;
}

/**
*
* @param RoundOff
* The RoundOff
*/
public void setRoundOff(Double RoundOff) {
this.RoundOff = RoundOff;
}

/**
*
* @return
* The CashAmt
*/
public Double getCashAmt() {
return CashAmt;
}

/**
*
* @param CashAmt
* The CashAmt
*/
public void setCashAmt(Double CashAmt) {
this.CashAmt = CashAmt;
}

/**
*
* @return
* The CreditAmt
*/
public Double getCreditAmt() {
return CreditAmt;
}

/**
*
* @param CreditAmt
* The CreditAmt
*/
public void setCreditAmt(Double CreditAmt) {
this.CreditAmt = CreditAmt;
}

/**
*
* @return
* The BillAmt
*/
public Double getBillAmt() {
return BillAmt;
}

/**
*
* @param BillAmt
* The BillAmt
*/
public void setBillAmt(Double BillAmt) {
this.BillAmt = BillAmt;
}

/**
*
* @return
* The User
*/
public String getUser() {
return User;
}

/**
*
* @param User
* The User
*/
public void setUser(String User) {
this.User = User;
}

/**
*
* @return
* The nFinalBillId
*/
public Integer getNFinalBillId() {
return nFinalBillId;
}

/**
*
* @param nFinalBillId
* The nFinalBillId
*/
public void setNFinalBillId(Integer nFinalBillId) {
this.nFinalBillId = nFinalBillId;
}

}

然后您可以创建一个ArrayList并存储值并相应地使用。希望它有所帮助!