[英]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. 我有一个RestService,我在其中返回一个JSON响应,如下所示。
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: 下面是我在点击Restful Service后得到的实际JSON响应:
{
"response": "{\"ABC\":100,\"PQR\":\"40\"}\n",
}
And this is what it gets printed out from System.out as shown above: 这就是从System.out打印出来的内容,如上所示:
{"ABC":100,"PQR":"40"}
Here is my DataResponse
class: 这是我的
DataResponse
类:
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. 如您所见,我的
response
字符串在上面的JSON中被转义。 And I am not sure why? 而且我不确定为什么? Can anyone help me understand why this is happening and how to fix it?
任何人都可以帮助我理解为什么会发生这种情况以及如何解决它?
Use Response class: 使用Response类:
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. 问题是您尝试返回
JSON
字符串作为响应。 Let the webservice build the JSON
for you like so (don't build the JSON
string yourself): 让webservice为你构建
JSON
(不要自己构建JSON
字符串):
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: 上面得到的
JSON
应该是这样的:
{
"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
. 该程序正在按预期工作,它正在将您的
DataResponse
类更改为JSON
但由于您将JSON
存储在它正在转换的字符串中,因此它是Javascript
转义字符。
{
"response": "{\"ABC\":100,\"PQR\":\"40\"}\n",
}
Your javascript object will be like this: 你的javascript对象将是这样的:
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: Javascript端将
String
转换为Object
示例:
var test = JSON.parse("{\"ABC\":100,\"PQR\":\"40\"}");
window.alert(test.ABC);
window.alert(test.PQR);
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.
如果没有转义它们,该值将无法解析,例如,您的字符串看起来像“{”ABC“:100,”PQR“:”40“} \\ n”,这根本不是有效的字符串值。 To include any character that has special meaning in a string, it has to be escaped.
要包含字符串中具有特殊含义的任何字符,必须对其进行转义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.