gpt4 book ai didi

java - SyntaxError : JSON. 解析:预期的属性名称或 '}'

转载 作者:行者123 更新时间:2023-11-29 04:46:26 26 4
gpt4 key购买 nike

我正在使用以下代码,尝试将 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”但没有数据的包:

enter image description here

这是怎么回事?谢谢!

编辑:当我试图只写 json 时,我看到了,但有一个错误:

enter image description here

最佳答案

因为您已经在变量 json 中有了 json 字符串,只需将此字符串打印到响应中即可:

response.getWriter().print(json);

另外json字符串要用双引号

"{\"nombre\":\"Donell\", \"dni\":\"351351P\"}"

(当您解析旧的无效字符串时,您得到一个 null JSONObject,因此您在打印对象时在响应中看到一个 null)。

关于java - SyntaxError : JSON. 解析:预期的属性名称或 '}',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36932388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com