JSON数组上的多个变量

时间:2018-02-22 03:58:09

标签: android arrays json

我有像这样的JSON数组

"Data":[  
   {  
      "Amount":"000000069840",
      "Denda":"00002500",
      "kubikasi":"00002084-00000030",
      "Periode":"201711"
   },
   {  
      "Amount":"000000060321",
      "Denda":"00002500",
      "kubikasi":"00002111-00000027",
      "Periode":"201712"
   },
   {  
      "Amount":"000000057148",
      "Denda":"00002500",
      "kubikasi":"00002137-00000026",
      "Periode":"201801"
   },
   {  
      "Amount":"000000031021",
      "Denda":"00000000",
      "kubikasi":"00002154-00000017",
      "Periode":"201802"
   }
]

但在某些情况下我解析时我喜欢这个

"Data":[  
   {  
      "Amount":"000000018226",
      "Denda":"00002500",
      "kubikasi":"00003855-00000012",
      "Periode":"201710"
   },
   {  
      "Amount":"000000012225",
      "Denda":"00002500",
      "kubikasi":"00003864-00000009",
      "Periode":"201711"
   },
   {  
      "Amount":"000000025315",
      "Denda":"00002500",
      "kubikasi":"00003879-00000015",
      "Periode":"201712"
   },
   {  
      "Amount":"000000037130",
      "Denda":"00002500",
      "kubikasi":"00003899-00000020",
      "Periode":"201801"
   },
   {  
      "Amount":"000000013500",
      "Denda":"00000000",
      "kubikasi":"00003909-00000010",
      "Periode":"201802"
   }
],

有些情况我在数组“Data”上有单个数组。

我如何获得该值并显示它?

1 个答案:

答案 0 :(得分:0)

使用Google gson库,在gradle依赖项中添加以下行

compile 'com.google.code.gson:gson:2.8.2'

然后创建一个POJO

class Data{
  @SerializedName("Amount")
  private String amount;
  @SerializedName("Denda")
  private String denda;
  @SerializedName("kubikasi")
  private String kubikasi;
  @SerializedName("Periode")
  private String periode;
  // getters and setters
}

class Response{
  @SerializedName("Data")
  private ArrayList<Data> data;
}

现在,

String responseString  = [your json array];
Response response = new Gson().fromJson(responseString,Response.class);