The transaction response (string) looks like the below.
{
"errorId": "15eabcd5-30b3-479b-ae03-67bb351c07e6-00000092",
"errors": [
{
"code": "20000000",
"propertyName": "directDebitPaymentMethodSpecificInput.bankAccountBban.accountNumber",
"message": "PARAMETER_NOT_FOUND_IN_REQUEST"
}
]
}
I need to extract the code as 20000000 and the message as PARAMETER_NOT_FOUND_IN_REQUEST.
I do it with the below code snippet that works when the length of the code is 6 chars long.
Code snippet:
code = response2.getResponseBody().substring(response2.getResponseBody().indexOf("code")+9, 97).trim();
Response
{
"errorId": "1105db54-9c91-4a97-baa7-3c4182458047",
"errors": [ {
"code": "410110",
"requestId": "3927859",
"message": "UNKNOWN ORDER OR NOT PENDING"
} ]
}
Kindly advise on how to fetch the text contained after code and message irrespective of the length of text contained inside the next occurrence of quotes.
EDIT--
I understand that it's a Json response and hence I need to parse it. I am using the below code snippet
JSONObject obj = new JSONObject(response2.getResponseBody());
String code = obj.getJSONObject("errors").getString("code");
However, it ends up with the below exception
org.json.JSONException: JSONObject["errors"] not found.
Thanks in advance