3 回答

TA贡献1862条经验 获得超6个赞
我完全按照你的建议(返回a String
)。
您可以考虑设置MIME类型以指示您正在返回JSON(根据此其他stackoverflow发布它的“application / json”)。

TA贡献1911条经验 获得超7个赞
将JSON对象写入响应对象的输出流。
您还应该按如下方式设置内容类型,该类型将指定您要返回的内容:
response.setContentType("application/json");
// Get the printwriter object from response to write the required json object to the output stream
PrintWriter out = response.getWriter();
// Assuming your json object is **jsonObject**, perform the following, it will return your json object
out.print(jsonObject);
out.flush();

TA贡献1735条经验 获得超5个赞
首先将JSON对象转换为String
。然后将其写入响应编写器以及application/json
UTF-8的内容类型和字符编码。
这是一个假设您使用Google Gson将Java对象转换为JSON字符串的示例:
protected void doXxx(HttpServletRequest request, HttpServletResponse response) { // ... String json = new Gson().toJson(someObject); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(json);}
就这样。
添加回答
举报