2016-01-14 124 views
0
{ 
    "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,它有兩個這樣的數組節點?我通過一些例子,但找不到我在找什麼。解析json與多個陣列節點

在此先感謝。

+1

顯示什麼ü試圖 –

+0

JSON有2個陣列'CompanyDtls'和'ReportDtls'。第一個有1個元素,第二個有2個,你可以像訪問數組元素一樣訪問每個元素。 –

+0

http://stackoverflow.com/a/9606629 – Sree

回答

1

那麼我會指導你如何做到這一點。基本上這就是我解析json的方式。

首先我檢查json是否有效,藉助於JSONEditor鉻合金應用程序。 Link JSONEditor

然後我複製此結構並將此json轉換爲使用另一個在線工具站點json到pojo的pojos。

然後我在我的android項目中映射模型。然後,當我得到JSON作爲一個字符串,然後我使用gson libaray將json轉換成jave對象。

Link站點

Link爲GSON

這就是我如何做到這一點。但我會建議首先你應該嘗試使用任何libaray json到Java對象。只需映射模型並設置值,然後獲取值。簡單!好運

-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和存儲的值,並相應地使用。希望能幫助到你!

2

我想你們兩個JSONArray「CompanyDtls」和「ReportDtls」如果這樣的話內有一個JSONObject:

創建模型類來保存從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); 
}