简体   繁体   中英

How to pass raw JSON as a response from Restful service?

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?

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();
    }

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM