I'm building REST service on Jersey
and using Jackson
to produce JSON from java classes of my model. Model with absolutely simple values, I think this is the most typical case. But I get strange result:
[{\"name\":\"Nick\",\"role\":\"admin\",\"age\":\"32\",\"rating\":47}]
My expecting result:
[{"name":"Nick","role":"admin","age":"32","rating":47}]
My source values of fields does NOT contains any special characters. These are simple words.
There're my Java classes. Entity:
public class User { private String name; private String role; private String age; private Integer rating;
Class of rest service:
@ServiceConfig(contextName = "myContext") @Path("/myrest") public class MyRestService { private static final String JSON_CONTENT_TYPE = MediaType.APPLICATION_JSON + ";charset=UTF-8"; @Context protected HttpServletResponse response; @GET @Path("/users") @OpenTransaction @Produces({MediaType.APPLICATION_JSON}) public String findUsers(@QueryParam("department") String department) { response.setContentType(JSON_CONTENT_TYPE); PDTResponse.status(response).sendStatus(Response.Status.OK.getStatusCode()); List<User> users = new ArrayList<>(); users.add(new User("Nick", "admin", "32", 47)); String jsonInString; ObjectMapper mapper = new ObjectMapper(); try { jsonInString = mapper.writeValueAsString(users); } catch (JsonProcessingException ex) { jsonInString = "thrown exception: " + ex.getMessage(); } return jsonInString; }
I've tried to use annotation @JsonRawValue
for string properties:
@JsonRawValue private String name;
But result in this case was:
[{\"name\":Nick,\"role\":admin,\"age\":32,\"rating\":47}]
And I expect:
[{"name":"Nick","role":"admin","age":"32","rating":47}]
It's obvious that Jackson somehow escapes the quotes in result json of response. But why does it do it, and most importantly how to avoid that? By themselves they are just strings! Without any quotes or special characters.
I use Java 7
and Jackson 2.6.1
. And Postman
to test result. Any ideas for fix of my problem?
Those backslashes are escape characters. They are escaping the special characters inside of the string associated with JSON response. You have to use JSON. parse to parse that JSON string into a JSON object.
String test = "{\"FirstName\":\"John \",\"LastName\":cena,\"salary\":7500,\"skills\":[\"java\",\"python\"]}"; System. out. println(StringEscapeUtils. unescapeJava(test));
replaceAll("\\","");
databind. ObjectMapper ) is the simplest way to parse JSON with Jackson. The Jackson ObjectMapper can parse JSON from a string, stream or file, and create a Java object or object graph representing the parsed JSON. Parsing JSON into Java objects is also referred to as to deserialize Java objects from JSON.
You can configure the ObjectMapper:
final ObjectMapper mapper = new ObjectMapper(); mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false); mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); String jsonUsers = mapper.writeValueAsString(users);
more info here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With