使用单引号将字符串引起来。从文档中:
字符串可以用’(单引号)引起来。
如果字符串不以引号或单引号开头,并且不包含前导或尾随空格,并且不包含以下任何字符,则根本不需要引号:{} [] / : ,=;
#,如果它们看起来不像数字,并且不是保留字true,false或null。
因此,修改您的示例:
net.sf.json.JSonObject obj = new net.sf.json.JSonObject();obj.put("jsonStringValue","{"hello":"world"}");obj.put("quotedJsonStringValue","'{"hello":"world"}'");obj.put("naturalStringValue", ""hello world"");System.out.println(obj.toString());System.out.println(obj.getString("jsonStringValue"));System.out.println(obj.getString("quotedJsonStringValue"));System.out.println(obj.getString("naturalStringValue"));产生:
{"jsonStringValue":{"hello":"world"},"quotedJsonStringValue":"{"hello":"world"}","naturalStringValue":""hello world""}{"hello":"world"}{"hello":"world"}"hello world"请注意,如何
quotedJsonStringValue将其视为字符串值而非JSON,并在输出JSON中用引号引起来。



