1

I have a RestService in which I am returning a JSON response as shown below.

Below is my code:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@GET
@Path("/json/lime")
@Produces(MediaType.APPLICATION_JSON)
public DataResponse getData(@Context DataInfo info) {

    // some code here
    System.out.println(resp.getResponse());
    return new DataResponse(resp.getResponse());
}

Below is the actual JSON response I get back after hitting my Restful Service:

{
    "response": "{\"ABC\":100,\"PQR\":\"40\"}\n",
}

And this is what it gets printed out from System.out as shown above:

{"ABC":100,"PQR":"40"}

Here is my DataResponse class:

public class DataResponse {

    private String response;

    public DataResponse(String response) {
        this.response = response;
    }

    public String getResponse() {
        return this.response;
    }
}

As you can see, my response string is getting escaped in the above JSON. And I am not sure why? Can anyone help me understand why this is happening and how to fix it?

john
  • 11,311
  • 40
  • 131
  • 251
  • What is `resp` I don't see it defined anywhere? – brso05 Jun 19 '15 at 19:35
  • @brso05 Does it matter here? `resp` is an object from which I get a String which has JSON response: So `resp.getResponse()` returns back a JSON response without any escaping at all. Updated the question with more details. – john Jun 19 '15 at 19:54
  • You need to give us more like `DataResponse` class and what is `resp.getResponse()` returning. – brso05 Jun 19 '15 at 20:03
  • Just updated my question. – john Jun 19 '15 at 20:05
  • I just posted an answer I know what your problem is if you have any question just ask... – brso05 Jun 19 '15 at 20:16

3 Answers3

2

Use Response class:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

    @GET
    @Path("/json/lime")
    @Produces(MediaType.APPLICATION_JSON)
    public ResponsegetData(@Context DataInfo info) {
        ...
        return Response.status(200).entity(resp.getResponse()).build();
    }
Yashar
  • 133
  • 3
1

The problem is you are trying to return a JSON string as the response. Let the webservice build the JSON for you like so (don't build the JSON string yourself):

public class DataResponse {

    private int ABC;
    private String PQR;

    public DataResponse()
    {

    }

    public DataResponse(int ABC, String PQR) {
        this.ABC = 100;
        this.PQR = "40";
    }
    //getters and setters here
}

The above is just an example I don't expect you to hard code values.

The resulting JSON of the above should be something like:

{
    "ABC":100,
    "PQR":"40"
}

The program is working as expected it is changing your DataResponse class into JSON but since you have JSON stored in the string that it is converting it is escaping characters in Javascript.

{
    "response": "{\"ABC\":100,\"PQR\":\"40\"}\n",
}

Your javascript object will be like this:

window.alert(data.response);

Should print:

{"ABC":100,"PQR":"40"} //same as system.out.print

I hope you understand what I am saying if not just ask...

***************************************************UPDATE********************************************************

@GET
@Path("/json/lime")
@Produces(MediaType.TEXT_PLAIN)
public String getData(@Context DataInfo info) {

    // some code here
    System.out.println(resp.getResponse());
    return resp.getResponse();
}

Javascript side convert String to Object example:

var test = JSON.parse("{\"ABC\":100,\"PQR\":\"40\"}");
window.alert(test.ABC);
window.alert(test.PQR);

String to JSON reference here

Community
  • 1
  • 1
brso05
  • 13,142
  • 2
  • 21
  • 40
  • In my case, my pojo is not general. JSON response can be in any POJO so that's why it is coming as string in DataResponse. – john Jun 19 '15 at 20:22
  • @david then instead of returning an `object` as `JSON` return a `String` as plain text then on the `javascript` side convert that `string` to an object... – brso05 Jun 19 '15 at 20:23
  • @david since it is already in `JSON` format you don't need the web service to convert it... – brso05 Jun 19 '15 at 20:25
0

Your response contains double quotes in it. Without escaping them, the value wouldn't be parsable, for example, your string would look like "{"ABC":100,"PQR":"40"}\n", which isn't a valid string value at all. To include any character that has special meaning in a string, it has to be escaped.

Sasikanth Bharadwaj
  • 1,457
  • 1
  • 11
  • 11