0

I'm having a problem reading JSON Array because inside my array, there's another array and I'm confused.Please see my code.

    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
    JSONObject obj = JSONFactoryUtil.createJSONObject();
    JSONObject jsonObjectReturn =  JSONFactoryUtil.createJSONObject();

    //JSONOBJECT RETURN
    JSONArray array = JSONFactoryUtil.createJSONArray();



        for (int i = 0; i < jsonArray.length(); i++) {
            obj = jsonArray.getJSONObject(i);

            callableStatement = (CallableStatement) conn
                    .prepareCall("{call TaxpaymentSPv2(?,?,?,?,?,?,?,?)}");


            callableStatement.setString(1, obj.getString("rdoCode"));
            callableStatement.setString(2, obj.getString("rcoCode"));
            callableStatement.setString(3, obj.getString("tpTin"));
            callableStatement.setString(4,  obj.getString("tpName")); 
            callableStatement.setString(5, obj.getString("tpAddress"));
            callableStatement.setString(6, obj.getString("receiptType"));
            callableStatement.registerOutParameter(7, Types.VARCHAR);
            callableStatement.registerOutParameter(8, Types.VARCHAR);

            callableStatement.executeUpdate();

            String rNo = callableStatement.getString(7);
            String date = callableStatement.getString(8);


            //---->This is my second Array inside my JSON where im having an error <------
            String checkArray = obj.getString("checkArray");
            JSONArray jsonArrayCheck = JSONFactoryUtil.createJSONArray(stringArray);


            JSONArray jsonArray2 = jsonObject.getJSONArray("checkArray");
            System.out.println("..........." + jsonArray2);

            jsonObjectReturn =  JSONFactoryUtil.createJSONObject();
            jsonObjectReturn.put("rNo", rNo);
            jsonObjectReturn.put("date", date);
            array.put(jsonObjectReturn);
        }

        return array;

Here's my JSON input:

{
"dataArray": [{

"rdoCode": "001",
"rcoCode": "002911",
"tpTin": "200746409",
"tpName": "JOHN DOE",
"tpAddress": "LA CALIFORNIA",
"receiptType":"ROR",
"receiptMode":"AUTO",
"manualReceiptNo":"",
"checkArray":[{
"ptchkNumber": 14546,
"ptchkDate": 2014-01-01,
"ptchkAmount": 5332,
"ptchkStatus": ""
}]
}]
}

I can't parse the "checkArray" object that gives me this error. com.liferay.portal.kernel.json.JSONException: org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]. Can someone tell me what to do? Thanks.

6
  • What language is this? Commented Oct 24, 2016 at 1:29
  • @user2182349 i'm using java Commented Oct 24, 2016 at 1:29
  • I believe "ptchkDate": 2014-01-01 will be treated as a number and your field ptchkDate will have value 2012. Commented Oct 24, 2016 at 1:32
  • it will be much better if you can trim down your code, and make one small and self-executable code that demonstrate your JSON problem. Quoting all those JDBC code is not going to help Commented Oct 24, 2016 at 1:34
  • okay i'll edit my question. Commented Oct 24, 2016 at 1:34

1 Answer 1

4

You need to change code:

String checkArray = obj.getString("checkArray");

to following code:

JSONArray checkArray = obj.getJSONArray("checkArray");
JSONObject checkObj = checkArray.getJSONObject(0);
int ptchkNumber = checkObj.getInt("ptchkNumber");

because key checkArray is array inside JSON, so you cannot using getString() but getJSONArray() instead.

Update:

To get each object inside array, you can iterate and access each object:

JSONArray checkArray = obj.getJSONArray("checkArray");
JSONObject checkObj;
for (int itemIndex=0, totalObject = checkArray.length(); itemIndex < totalObject; itemIndex++) {     
    checkObj = checkArray.getJSONObject(itemIndex);
    int ptchkNumber = checkObj.getInt("ptchkNumber");
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Zamrony. This works for me. I have a follow up question. What if I have multiple entries inside my checkArray key? For example, another set of ptchkDate,ptchNumber, and so on.. How should I handle this? Thanks.
checkArray have length() method which returns how many object inside array. You can iterate to get each object using something like checkArray.getJONObject(itemIndex)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.