Zerok Zerok - 7 months ago 33
Java Question

SyntaxError: JSON.parse: expected property name or '}'

I'm using the following code, trying to send the JSON line to the client, for testing purposes:

String json = "{'nombre':'Donell', 'dni':'351351P'}";
response.setContentType("application/json");
PrintWriter out = response.getWriter();
JSONObject jsonObj = (JSONObject) JSONValue.parse(json);
out.print(jsonObj);


I'm using the library json-simple, but this isn't working. When I access to "/Prueba", which is my test servlet, I receive a package with 200 code type "json", but without data:

enter image description here

What's wrong here? Thank you!

edit: When trying to just write the json, I see it, but with an error:

enter image description here

Answer

Since you already have the json string in variable json simply print this string to the response:

response.getWriter().print(json);

Also the json string should use double quotes

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

(When you parsed the old invalid string, you got a null JSONObject and therefore you saw a null in the response, when printing the object).