Java JSON backslash added to JSON response
Why is the backslash happening and how to remove them when the browser displays json data to the client?
The answer to o / p json appears to be valid if those backslashes (es) were not present
testbookdata.xml
<Users>
<User>
<Name>Unni</Name>
<Books>
<Book>book1</Book>
<Book>book2</Book>
<Book>book3</Book>
</Books>
</User>
<User>
<Name>Ammu</Name>
<Books>
<Book>book1</Book>
<Book>book2</Book>
<Book>book4</Book>
</Books>
</User>
</Users>
This xml is converted to JSONObject library via org.json
org.json.JSONObject xmlJSONObj = XML.toJSONObject(booksXMLString);
Finally, I have a class that says that everything will be converted to JSON upon a specific user request,
Class property:
@JsonInclude(Include.NON_NULL)
@JsonProperty(value = "jsondata")
public String getJson() {
return json.toString();
}
If I just print out the data,
json data : ...
{"Users":{"User":[{"Name":"Unni","Books":{"Book":["book1","book2","book3"]}},{"Name":"Ammu","Books":{"Book":["book1","book2","book4"]}}]}}
Finally, the webservice controller method, which is annotated, public @ResponseBody
calls the service and returns an object with @Json annotations
Problem:
When the result is displayed by the browser, it looks like this:
"jsondata": "{\"Users\":{\"User\":[{\"Name\":\"Unni\",\"Books\":{\"Book\":[\"book1\",\"book2\",\"book3\"]}},{\"Name\":\"Ammu\",\"Books\":{\"Book\":[\"book1\",\"book2\",\"book4\"]}}]}}"
}
How to overcome this problem?
Thank!
note: I added spring-mvc tag because it @ResponseBody
is part of spring-web
Update 1:
Tried again what @Jon Skeet mentioned, however this gives an error,
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON:
No serializer found for class org.json.JSONObject and no properties discovered to create
BeanSerializer (to avoid exception, disable SerializationConfig.SerializationFeature.FAIL_ON_EMPTY_BEANS) )
(through reference chain: com.ht.Result["jsondata"]);
nested exception is com.fasterxml.jackson.databind.JsonMappingException:
No serializer found for class org.json.JSONObject and no properties discovered
to create BeanSerializer (to avoid exception, disable SerializationConfig.SerializationFeature
.FAIL_ON_EMPTY_BEANS) )
source to share
From what I understand you have
class SomePojo {
public JSONObject json;
@JsonInclude(Include.NON_NULL)
@JsonProperty(value = "jsondata")
public String getJson() {
return json.toString();
}
}
and
@ResponseBody
@RequestMapping(..)
public SomePojo getPojo() {
SomePojo pojo = ...;
return pojo;
}
The above model says that you have a JSON object that contains a name-value pair. Name jsondata
, and value is a JSON string. Since your value String
contains characters that are not acceptable in the JSON string, they must be escaped in the serialized value.
But you seem to want a JSON object that contains a name-value pair where name jsondata
is another JSON object.
You probably want to have
@JsonRawValue
@JsonInclude(Include.NON_NULL)
@JsonProperty(value = "jsondata")
public String getJson() {
return json.toString();
}
So, the value is String
used as it is, not converted to a JSON string.
source to share