0

I need to accept a string from the user and put it as-is into a JSONObject. The documentation says strings may be quoted with ' but it seems obvious I've misunderstood.

Is this sufficient or am I missing something?

jsonObject.put("name", "'" + userInput + "'");

I stepped through the put function but it doesn't seem to care about the string at all! There's a quote function but it adds another set of double quotes around the string which seems incorrect.

Monstieur
  • 7,992
  • 10
  • 51
  • 77

1 Answers1

2

You seem to be quoting this part of the Javadoc

Strings may be quoted with ' (single quote).

which is preceded by

The texts produced by the toString methods strictly conform to the JSON syntax rules. The constructors are more forgiving in the texts they will accept:

JSON strings are wrapped in double quotes ". so JSONObject#toString will produce a JSON value where JSON strings will be syntactically correct. However, the JSONObject constructor can accept a JSON value (as text) where JSON strings are surrounded with single quotes instead of double quotes.

For example

JSONObject object = new JSONObject("{'bad':'json'}"); // not valid JSON
System.out.println(object);

produces the valid

{"bad":"json"}

The put method is completely unrelated here. You don't need (and shouldn't) use single quotes around your specified string.


From your comments

JSONObject obj = new JSONObject();
obj.put("jsonStringValue","{\"hello\":\"world\"}");
obj.put("naturalStringValue", "\"hello world\"");
System.out.println(obj.toString());
System.out.println(obj.getString("jsonStringValue"));
System.out.println(obj.getString("naturalStringValue"));

prints

{"jsonStringValue":"{\"hello\":\"world\"}","naturalStringValue":"\"hello world\""}
{"hello":"world"}
"hello world"
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • But according to http://stackoverflow.com/questions/6155560/how-to-force-json-libs-jsonobject-put-to-escape-a-string-containing-json if the string is valid JSON the `put` function embeds it as a nested JSON object! The answer there says to wrap it in single quotes. – Monstieur Feb 12 '15 at 17:01
  • @Locutus What are you trying to do? `JSONObject#put` takes the second argument and converts it to an appropriate JSON value. If it's a String, it will convert it to a JSON string (escaping characters that need it). It it's a number, it will convert it to a JSON number. If it's an array, a JSON array. If it's an object, a JSON object. If you're trying to insert JSON into JSON, create a `JSONObject` from the `userInput` and `put` that. – Sotirios Delimanolis Feb 12 '15 at 17:08
  • @Locutus Obviously, if you don't know if the `userInput` is a JSON value, then `JSONObject` correctly does provide a way to write it to the JSON text raw. If the `userInput` was not valid JSON, it would break the final JSON value. – Sotirios Delimanolis Feb 12 '15 at 17:11
  • I want to treat it purely as a string. But in that question, even when providing `put` with a string (and not a JSONObject), it's turning the string into a nested JSONObject if the string happens to be valid JSON! Look at his example code, specifically the `jsonStringValue` part. – Monstieur Feb 12 '15 at 17:20
  • @Locutus They must either be using an old (buggy) version of the library or misrepresenting their results. If you `put` a Java `String` using `JSONObject#put`, the corresponding value of the JSON key-value pair will be a JSON String where all necessary characters will be escaped. For example, a double quote `"` must and will be escaped. – Sotirios Delimanolis Feb 12 '15 at 17:24