當我必須給出一個JSONObject.fromObject
這裏彈出一個例外'true'='true'pq是另一個單引號內的單引號,有人知道某種類型的轉義字符API?Escape for net.sf.json.JSONObject
public static void main(String[] args) {
String json = "{gA:[ {c:{f:'C#',o:'=',v1:' ('true' = 'true') ' }}]}";
final Map<String, Object> map = new HashMap<String, Object>();
try {
JsonConfig cfg = new JsonConfig();
cfg.setRootClass(LinkedHashMap.class);
cfg.setArrayMode(JsonConfig.MODE_OBJECT_ARRAY);
cfg.setHandleJettisonSingleElementArray(false);
JSONObject jsonObjeto = JSONObject.fromObject(json, cfg);
System.out.println(jsonObjeto.toString());
} catch (final JSONException e) {
e.printStackTrace();
}
}
例外:
net.sf.json.JSONException: Expected a ',' or '}' at character net.sf.json.JSONException: Expected a ',' or '}' at character 31 of {gA:[ {c:{f:'C#',o:'=',v1:' ('true' = 'true') ' }}]}
at net.sf.json.util.JSONTokener.syntaxError(JSONTokener.java:499)
at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:1099)
at net.sf.json.JSONObject.fromObject(JSONObject.java:159)
at net.sf.json.util.JSONTokener.nextValue(JSONTokener.java:348)
at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:1008)
at net.sf.json.JSONObject.fromObject(JSONObject.java:159)
at net.sf.json.util.JSONTokener.nextValue(JSONTokener.java:348)
at net.sf.json.JSONArray._fromJSONTokener(JSONArray.java:1131)
at net.sf.json.JSONArray.fromObject(JSONArray.java:125)
at net.sf.json.util.JSONTokener.nextValue(JSONTokener.java:351)
at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:1008)
at net.sf.json.JSONObject._fromString(JSONObject.java:1201)
at net.sf.json.JSONObject.fromObject(JSONObject.java:165)
at br.com.michel.json.JsonTest.main(JsonTest.java:28)
,我想要的JSON的輸出:
{ 「GA」:[{ 「C」:{ 「F」, 「C#」, 「o」:「=」,「v1」:「('true'='true')」}}]}
感謝哥們。 昨晚我也找到了這個解決方案,我犯的錯誤只是反斜槓而不是兩個。 關於我把錯誤的退出,期望的輸出是相同的。 {「ga」:[{「c」:{「f」,「C#」,「o」:「=」,「v1」,「('true'='true')」}}]} 謝謝你的朋友。 –