Wolverine Wolverine - 26 days ago 9
Java Question

Cannot retrieve a JSON array inside another json array that comes in soap response

Hi I have a json data in which I have an array , and inside that array I have another array . I want to access the second array can anybody suggest me a solution ? I have tried some coding but I am not getting the second array.

giving my json below



{ "fdAccountTransactions": [
{
"id": 8418231,
"branch": {
"id": 1004,
"code": "1003"
},
"totalTransferAmount": 100004,
"voucher": {
"id": 840372,
"voucherType": "RECEIPT",
"voucherNo": "1001150005",
,
"totalAmount": 106660,
"branchCode": "100",
"isCancelled": false,
"transactions": [
{
"id": 84169,
"accountCode": "1136",
"accountName": "Cash on Hand",
"accountGroupCode": "G033",
"amount": 10000,
"creditDebit": "DEBIT",
"destinationUnit": "100",
"destinationUnitName": "London (UK)",
"narration": "By CASH"
},
{
"id": 84170,
"accountCode": "30606",
"accountName": "FD A/C",
"accountGroupCode": "G012",
"amount": 10000,
"creditDebit": "CREDIT",
"destinationUnit": "100",
"destinationUnitName": "London (UK)",
"narration": "To FD A/C"
}
]
},
"description": "FD collection from teller",
"dateOfTransaction": "Jun 2, 2015 11:59:59 PM",
"valueDate": "Jun 2, 2015 11:59:59 PM",
"ledgerItems": [
{
"id": 84265,
"creditDebit": "CREDIT",
"transferAmount": 10000,
"typeOfTransfer": "CASH",
"description": "To FD A/C"
}
],
"depositOperationType": "FD Collection",
"balanceAfterTransaction": 10000,
"isCancelled": false,
"tdsAmount": 0
}
]
}

I was trying with this code

JSONObject json=new JSONObject(result+"");
JSONObject arry=json.getJSONObject("deposit");
Log.w("deposit", arry.toString());
mTransctn=json.getJSONArray(TAG_FDTRANSACTION);

//JSONObject arr=new JSONObject(mTransctn);
JSONObject voucher=json.getJSONObject("voucher");
Log.w("voucher", voucher.toString());
//JSONObject vouchers=voucher.getJSONObject("transactions");
JSONArray array =new JSONArray();
array=voucher.getJSONArray(TAG_TRANSACTION);


I am getting null value in voucher.

Answer Source

Well I have come up with a solution for my question I have used two for loops to solve the problem and it is working very much fine now .

JSONObject json = new JSONObject(result + "");

            double balance = json.getDouble("actualBalance");

            Log.w("actualBalance", "" + curBalance);
            mTransctn = json.getJSONArray(TAG_FDTRANSACTION);

            for (int i = 0; i <mTransctn.length() ; i++) {
                JSONObject jOb = mTransctn.getJSONObject(i);



                JSONObject jVoucher = jOb.getJSONObject("voucher");
                JSONArray array = new JSONArray();
                array = jVoucher.getJSONArray(TAG_TRANSACTION);
                 String date=jVoucher.getString("date");
                Log.w("arraylength", "" + array.length());

                for (int j = 0; j <array.length() ; j++) {
                    JSONObject c = array.getJSONObject(j);

                    // gets the content of each tag

                    String amount = c.getString(TAG_MONEY);
                    String type = c.getString(TAG_TYPE);
                    String descrptn = c.getString(TAG_DESCRIPTION);

                    }

                }