我正在使用以下代码,尝试将 JSON 行发送到客户端,以进行测试:
String json = "{'nombre':'Donell', 'dni':'351351P'}";
response.setContentType("application/json");
PrintWriter out = response.getWriter();
JSONObject jsonObj = (JSONObject) JSONValue.parse(json);
out.print(jsonObj);
我正在使用库 json-simple,但这不起作用。当我访问我的测试 servlet“/Prueba”时,我收到一个包含 200 代码类型“json”但没有数据的包:
这是怎么回事?谢谢!
编辑:当我试图只写 json 时,我看到了,但有一个错误:
最佳答案
因为您已经在变量 json
中有了 json 字符串,只需将此字符串打印到响应中即可:
response.getWriter().print(json);
另外json字符串要用双引号
"{\"nombre\":\"Donell\", \"dni\":\"351351P\"}"
(当您解析旧的无效字符串时,您得到一个 null
JSONObject,因此您在打印对象时在响应中看到一个 null
)。
关于java - SyntaxError : JSON. 解析:预期的属性名称或 '}',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36932388/