为什么这些反斜杠会出现,以及如何在浏览器向客户端显示json数据时将其删除?
如果这些反斜杠不存在,则o / p json响应似乎有效
testbookdata.xml
Unni
book1
book2
book3
Ammu
book1
book2
book4
该xml通过org.json库转换为JSONObject
org.json.JSONObject xmlJSONObj = XML.toJSONObject(booksXMLString);
最后,我有一个类,该类告诉特定用户请求将所有内容转换为JSON,
该类的一个属性:
@JsonInclude(Include.NON_NULL)
@JsonProperty(value = "jsondata")
public String getJson() {
return json.toString();
}
如果我只是打印数据,
json data : ...
{"Users":{"User":[{"Name":"Unni","Books":{"Book":["book1","book2","book3"]}},{"Name":"Ammu","Books":{"Book":["book1","book2","book4"]}}]}}
最后,具有公共@ResponseBody批注的webservice控制器方法
调用服务并返回具有@Json批注的对象
问题:
当浏览器显示结果时,它是这样的-
"jsondata": "{\"Users\":{\"User\":[{\"Name\":\"Unni\",\"Books\":{\"Book\":[\"book1\",\"book2\",\"book3\"]}},{\"Name\":\"Ammu\",\"Books\":{\"Book\":[\"book1\",\"book2\",\"book4\"]}}]}}"
}
如何克服这个问题?
谢谢!
注意:我添加了spring-mvc标记,因为@ResponseBody是spring-web的一部分
更新1:
再次尝试了@Jon Skeet提到的内容,但这给出了错误,
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) )