你好,在我的程序中,我喜欢这样的Json
{"AgentTrxID":"201802090013",
"Data":
[{ "Amount":"000006000.00",
"Denda":"00000000",
"kubikasi":"00103000-00000000",
"Periode":"201710"}],
"Header":
[{ "Total":6000,
"AdminFee":2500,
"IDPelanggan":"0416610037",
"Biller":"PDAM TIRTANADI",
"Alamat":"JAGUNG Gg COMP BGR",
"GolonganTarif":"RT.3",
"NomerReffTrans":"",
"Nama":"NADHIRAH OKTARI"}],
"AgentStoreID":"01010101",
"message":"SUCCESS",
"code":"00"
}
内部json中的有2个数组,我怎么能得到对象内部数组,那个对象谢谢
答案 0 :(得分:2)
将json数据作为带有名称文本的字符串传递给下面的代码:
JSONObject outerObject = new JSONObject(text);
JSONArray dataArray = outerObject.getJSONArray("Data");
JSONObject objectInsideData = dataArray.getJSONObject(0);
JSONArray headerArray = outerObject.getJSONArray("Header");
JSONObject objectInsideHeader = headerArray.getJSONObject(0);
答案 1 :(得分:0)
取决于你使用的是什么,我正在使用Gson将这个JSON解析为一些POJO。
如果您选择使用Android上可用的默认Json Api,那么您可以执行类似的操作。
public void parseJson(JsonObject rootObj){
if(rootObj.has("Data")){
JSONArray array = rootObj.getJSONArray("Data");
for(int count = 0; count<array.length(); count++){
JSONObject inner = array.getJSONObject(count);
//use inner object
}
}
}
Gson方式
public void parseJson(JsonObject rootObj){
Gson gson = new Gson();
Example exp = gson.fromJson(rootObj,Example.class);
//your json is parsed use pojo to access the array inside
}
Pojo就是这样的
public class Example {
@SerializedName("AgentTrxID")
@Expose
private String agentTrxID;
@SerializedName("Data")
@Expose
private List<Datum> data = null;
@SerializedName("Header")
@Expose
private List<Header> header = null;
@SerializedName("AgentStoreID")
@Expose
private String agentStoreID;
@SerializedName("message")
@Expose
private String message;
@SerializedName("code")
@Expose
private String code;
public String getAgentTrxID() {
return agentTrxID;
}
public void setAgentTrxID(String agentTrxID) {
this.agentTrxID = agentTrxID;
}
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
public List<Header> getHeader() {
return header;
}
public void setHeader(List<Header> header) {
this.header = header;
}
public String getAgentStoreID() {
return agentStoreID;
}
public void setAgentStoreID(String agentStoreID) {
this.agentStoreID = agentStoreID;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
标题
public class Header {
@SerializedName("Total")
@Expose
private Integer total;
@SerializedName("AdminFee")
@Expose
private Integer adminFee;
@SerializedName("IDPelanggan")
@Expose
private String iDPelanggan;
@SerializedName("Biller")
@Expose
private String biller;
@SerializedName("Alamat")
@Expose
private String alamat;
@SerializedName("GolonganTarif")
@Expose
private String golonganTarif;
@SerializedName("NomerReffTrans")
@Expose
private String nomerReffTrans;
@SerializedName("Nama")
@Expose
private String nama;
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public Integer getAdminFee() {
return adminFee;
}
public void setAdminFee(Integer adminFee) {
this.adminFee = adminFee;
}
public String getIDPelanggan() {
return iDPelanggan;
}
public void setIDPelanggan(String iDPelanggan) {
this.iDPelanggan = iDPelanggan;
}
public String getBiller() {
return biller;
}
public void setBiller(String biller) {
this.biller = biller;
}
public String getAlamat() {
return alamat;
}
public void setAlamat(String alamat) {
this.alamat = alamat;
}
public String getGolonganTarif() {
return golonganTarif;
}
public void setGolonganTarif(String golonganTarif) {
this.golonganTarif = golonganTarif;
}
public String getNomerReffTrans() {
return nomerReffTrans;
}
public void setNomerReffTrans(String nomerReffTrans) {
this.nomerReffTrans = nomerReffTrans;
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
}
基准
public class Datum {
@SerializedName("Amount")
@Expose
private String amount;
@SerializedName("Denda")
@Expose
private String denda;
@SerializedName("kubikasi")
@Expose
private String kubikasi;
@SerializedName("Periode")
@Expose
private String periode;
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getDenda() {
return denda;
}
public void setDenda(String denda) {
this.denda = denda;
}
public String getKubikasi() {
return kubikasi;
}
public void setKubikasi(String kubikasi) {
this.kubikasi = kubikasi;
}
public String getPeriode() {
return periode;
}
public void setPeriode(String periode) {
this.periode = periode;
}
}
答案 2 :(得分:0)
它的PHP代码:
首先在字符串或文件中取这个。
$json = json_decode(file_get_contents($target_file),TRUE);
访问您的First Array您可以在php中使用以下代码。
foreach ($json['Data'] as $key => $value)
{ echo $value['Amount'];
}
要访问第二个数组,请使用以下代码:
foreach ($json['Header'] as $key => $value)
{ echo $value['Total'];
}