0

I am trying to call the REST API of my web service. It is a synchronous call and in response I expect a Json Array of some Object. For ex :-

[{"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter","lastName":"Jones"}]

The response is being read by me is using somthing like below -

InputStream in = address.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder result = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
    result.append(line);
}
System.out.println(result.toString());

Now the problem statement is - I am getting a json like this -

!!com.company.Employee {"firstName":"John", "lastName":"Doe"}, !!com.company.Employee {"firstName":"Anna", "lastName":"Smith"}, !!com.company.Employee {"firstName":"Peter","lastName":"Jones"}

So when I try to pass this string in my JSONArray's constructor it is throwing Exception.

JSONArray jsonArray = new JSONArray(responseJsonString);

The exception is invalid json array it should start with "["

How this can be solved? Kindly share your views, I've already spent 3 hours on it.

saurabh kedia
  • 321
  • 2
  • 11
  • Please show the code of the web service – OneCricketeer Aug 13 '16 at 04:40
  • Are you sure there are **!!** In the JSON String. That sounds like a problem to me. – soufrk Aug 13 '16 at 04:43
  • @cricket_007 web service is return array of objects. Ex - Employee[] which contains 3 Employee Objects – saurabh kedia Aug 13 '16 at 04:43
  • @soufrk That's the issue. – saurabh kedia Aug 13 '16 at 04:45
  • Your server needs to return a JSON string, not an array of objects. I assume you are using some serialization library to convert that array? – OneCricketeer Aug 13 '16 at 04:46
  • @cricket_007 The object is serializable and i am using resteasy-jackson-provider as a dependency in my maven pom to convert the Response objects as json – saurabh kedia Aug 13 '16 at 05:09
  • 1
    The problem clearly is that what you get is not JSON. The server needs to be fixed. – Henry Aug 13 '16 at 05:17
  • @Henry Even I think the same, though when I try to access some other module in the same server which is having the same Object array return type. It is working fine. Can that be a dependency issue? But as of my past experiences any dependency issue of jackson library will result in exception on the server side rather than malformed json – saurabh kedia Aug 13 '16 at 05:28
  • If one rest endpoint works, but another doesn't for the same object type, then you'll have to compare the code that builds that response output. Set a breakpoint in the server, if you have to – OneCricketeer Aug 13 '16 at 05:44
  • Either your server uses some proprietary serialization protocol, or there is a mistake in the implementation/declaration of the API. If the latter is true, we'll need to see the server-side code to help. BTW, paste the URL you are trying to access into the browser's address bar and see what you get to make sure it's not a client issue. – vempo Aug 13 '16 at 06:48
  • Is your problem similar to this http://stackoverflow.com/q/10849526/3503018 ?? – soufrk Aug 13 '16 at 07:45
  • @cricket_007 The output is being converted to json using resteasy jackson provider which will convert the object to json by specifying the return media type as application/json. So we cannot put a breakpoint in the conversion code – saurabh kedia Aug 13 '16 at 10:18
  • @soufrk Nope that is completely different problem. – saurabh kedia Aug 13 '16 at 10:18
  • You can set a breakpoint before the Response is returned or log the output of manually converting the array with Jackson before resteasy conversion happens – OneCricketeer Aug 13 '16 at 14:14
  • @cricket_007 did that, still got nothing – saurabh kedia Aug 13 '16 at 14:49

0 Answers0