如何强制JSON-lib的JSONObject.put(..)转义包含JSON的字符串? [英] How to force JSON-lib's JSONObject.put(..) to escape a string containing JSON?
问题描述
使用JSON-lib的 JSONObject
时,如何停止 put
方法存储字符串它包含JSON作为JSON而不是转义字符串?
When using JSON-lib's JSONObject
, how can I stop the put
method from storing a String which contains JSON as JSON rather than as an escaped string?
例如:
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"));
打印:
{"jsonStringValue":{"hello":"world"},"naturalStringValue":"\"hello world\""}
{"hello":"world"}
"hello world"
我希望它打印出来:
{"jsonStringValue":"{\"hello\":\"world\"}","naturalStringValue":"\"hello world\""}
{"hello":"world"}
"hello world"
是的,我意识到这是令人讨厌的。但是,这支持JSON序列化管道,为了互操作性,这是预期的行为。在某些情况下,我们会序列化可能包含有效JSON的用户输入。我们不希望用户输入成为我们将所述输入序列化的JSON对象的一部分。
Yes, I realize this is obnoxious. However, this is in support of a JSON serialization pipeline for which, for interoperability's sake, this is the expected behavior. There are cases in which we would be serializing user input which may be/contain valid JSON. We wouldn't want the user input to become a part of the JSON object that we're serializing said input to.
手动转义不起作用,因为它导致JSON -lib用于转义 \
字符:
Manual escaping doesn't work because it causes JSON-lib to escape the \
characters:
JSONObject obj = new JSONObject();
obj.put("naturalJSONValue","{\"hello\":\"world\"}");
obj.put("escapedJSONValue", "{\\\"hello\\\":\\\"world\\\"}");
System.out.println(obj.toString());
System.out.println(obj.getString("naturalJSONValue"));
System.out.println(obj.getString("escapedJSONValue"));
输出:
{"naturalJSONValue":{"hello":"world"},"escapedJSONValue":"{\\\"hello\\\":\\\"world\\\"}"}
{"hello":"world"}
{\"hello\":\"world\"}
此时,任何启用手动选择性转义复杂JSON对象的变通方法都会完全否定使用JSON-lib的价值第一名。
At this point, any workarounds to enable manual selective escaping of a complex JSON object would completely negate the value of using JSON-lib in the first place.
Also, I understand that this question has been asked before, but unfortunately I cannot accept its answer so easily. JSON-lib is a heavily-used dependency in many areas of my project and swapping it out would be a big undertaking. I need to be absolutely sure that there's no way to achieve this goal with JSON-lib before I can entertain a swap to Jackson, simple-json, or Gson.
推荐答案
使用单引号引用字符串。来自文档:
Use single quotes to quote the string. From the documentation:
字符串可以用'(单引号)引用。
Strings may be quoted with ' (single quote).
如果字符串不引用则根本不需要引用以引号或单引号开头,如果它们不包含前导或尾随空格,并且它们不包含任何这些字符:{} [] / \:,=; #并且如果它们看起来不像数字,并且它们不是保留字true,false或null。
Strings do not need to be quoted at all if they do not begin with a quote or single quote, and if they do not contain leading or trailing spaces, and if they do not contain any of these characters: { } [ ] / \ : , = ; # and if they do not look like numbers and if they are not the reserved words true, false, or 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中引用。
Note how quotedJsonStringValue
has been treated as a string value and not JSON, and appears quoted in the output JSON.
这篇关于如何强制JSON-lib的JSONObject.put(..)转义包含JSON的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!