0

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

user2967948
  • 529
  • 2
  • 8
  • 23

2 Answers2

1

That response is in JSON format. Instead of using substrings, you probably want to use a JSON parser so that you can interact with your response as structured data instead of just as a string.

You can find additional information on the JSON format here: http://www.json.org/

The two most common JSON parsing libraries (at least in my experience) are:

Additionally, the SO question on JSON parsing (How to parse JSON in Java) has further answers you might find useful.

Community
  • 1
  • 1
Aurand
  • 5,487
  • 1
  • 25
  • 35
  • Thanks Aurand. I tried using the below code snippet, but ending up with the value as Null and org.json.JSONException: JSONObject["errors"] is not a JSONObject. I tried using JSONObject obj = new JSONObject(response2.getResponseBody()); code = obj.getJSONObject("errors").getString("code"); – user2967948 Jan 30 '16 at 20:29
  • @user2967948 Are you coding for Android? `org.json.JSONException` looks like an android class. Anyway, "errors" is not a JSON Object, it's a JSON Array. You want the method getJSONArray. You probably then want to interact with the first item in that array. – Aurand Jan 30 '16 at 20:42
  • No, it's not for Android. The JSON response is from third party and errors is a part of that response. "errors": [ ...I just need to fetch the "code": "20000000", and message from this response. Sorry, I'm a complete newbie in JSON, but would love to learn. – user2967948 Jan 31 '16 at 14:33
  • Can I also use gson? I can see that the third party vendor provided us gson jar. Kindly help me on how to fetch code and message using gson. Thanks – user2967948 Jan 31 '16 at 14:53
0

Thanks everyone for the guidance. The below code snippet achieves what I was looking for. Posting for others benefit.

JSONObject obj = null;
            try {
                obj = new JSONObject(response2.getResponseBody());
            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                List<String> list = new ArrayList<String>();
                for(int i = 0; i < obj.getJSONArray("errors").length(); i++){
                    list.add(obj.getJSONArray("errors").getJSONObject(i).getString("code"));
                    list.add(obj.getJSONArray("errors").getJSONObject(i).getString("message"));
                }
                code = list.get(0); //gives me 20000000
                description = list.get(1); //gives me PARAMETER_NOT_FOUND_IN_REQUEST
user2967948
  • 529
  • 2
  • 8
  • 23