从JSON获取内部数据

时间:2017-12-06 07:07:28

标签: java json java-ee

我想从JSON

获取内部数据

我有JSON为

{
"data": [{
    "id": 1,
    "cusname": "KFC Inc.",
    "accno": "31926819",
    "billStatus": "Pending",
    "billID1": {
        "ID": "BILL232244",
        "month": "Aug",
        "amount": "400"
    },
    "billID2": {
        "ID": "BILL232244",
        "month": "Aug",
        "amount": "400"
    },
    "billID1Amount": "Edinburgh",
    "extn": "4/Nov/2107"
}, {
    "id": 2,
    "cusname": "KFC Inc.",
    "accno": "31926819",
    "billStatus": "Pending",
    "billID1": {
        "ID": "BILL232244",
        "month": "Aug",
        "amount": "400"
    },
    "billID2": {
        "ID": "BILL232244",
        "month": "Aug",
        "amount": "400"
    },
    "billID1Amount": "Edinburgh",
    "extn": "4/Nov/2107"
}]
}

我想为数据jsonObject中的所有id提取所有billID1或billID2的ID的所有值 例如:我想获得BillID1& BILL232244 BILL232244对于“id”的billID2:1和“id”相同:2

请建议逻辑。

1 个答案:

答案 0 :(得分:2)

JSONObject root = new JSONObject(yourJsonString);
JSONArray jsonArray = root.getJSONArray("data");

 for(int i=0; i<jsonArray.length(); i++){
// now loop the element:
JSONObject obj = jsonArray.getJSONObject(i);

String idval = obj.getString("id"); 
String billid1 = obj.getString("billID1");
String billid2 = obj.getString("billID2"); 
}