0

I got to call a restful service owned by a thrid party. They are expecting a specific entry as array. I checked some questions here and wrote code to create the array of just 1 element. Even after that the service is complaining about the same issue. If I don't pass that array it responds correctly. But not the expected response. Say it recommends me something. I expect approved. I get back investigate if i don't pass the array. If I pass the array i am getting back exception where as i am expecting "approved"

{
"orgCode": "XXX",
"modelCode": "XXX",
"event": {
"contacts": [
    {
        "name": "XXX",
        "@id": "941689716",
        "company": "XXX"
    }
    ],.............................

So. I tried 2 ways and both are throwing the same exception

Method 1: Create JSON Array and populate it directly

   JSONObject info = new JSONObject();
    info.put("@id", "941689716");
    info.put("name", "XXX");
    info.put("company", "XX");
    JSONArray contactsArray = new JSONArray();
    contactsArray.add(info);
    event.put("contacts", contactsArray.toString());

Method 2: Create arraylist, add it to JSON Array

   JSONObject info = new JSONObject();
    info.put("@id", "941689716");
    info.put("name", "XXX");
    info.put("company", "XX");
    List<JSONObject> objects = new ArrayList<JSONObject>();
    objects.add(info);
    JSONArray contactsArray = new JSONArray();
    contactsArray.addAll(objects);
    event.put("contacts", contactsArray.toString());

The reason i tried method 2 was because, in the exception I see that they are expecting arraylist

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token\n at [Source: java.io.ByteArrayInputStream@52d91fcc; line: 1, column: 50] (through reference chain: com.xx.xx.xx.xx[\"event\"]->com.xx.xx.xx.Event[\"contacts\"])"})

I am using java 6 on IBM RAD and the JSON objects are from com.ibm.json.java package. I cannot use any open source code due to regulations

So here is my question

Please correct me if i am wrong

Or

Can this be a server side issue? I read in another thread that if the arraylist has just 1 element they got to handle it, otherwise it might throw this error.

pnuts
  • 58,317
  • 11
  • 87
  • 139
juniorbansal
  • 1,249
  • 8
  • 31
  • 51

1 Answers1

0

I want to thank Sotirios-delimanolis. His comment made me to remove the .toString() at the end of the array while setting it in the root JSON object and hey!!!!! that worked and I got approved response back.

Community
  • 1
  • 1
juniorbansal
  • 1,249
  • 8
  • 31
  • 51