JSON无法识别的字符转义':'

时间:2016-05-09 12:42:11

标签: java json parsing exception

美好的一天,

我正在执行json属性的POST,如下所示:

{"db.URL":"jdbc\:derby\:C\:\\Users\\Sasha\\unicentaopos-database;create\=true"}

获得例外:Unrecognized character escape ':' (code 58)\n at [Source: java.io.StringReader@358b85c8; line: 1, column: 480]

现在,我想我需要将\替换为\\,但每当我这样做时:

jsonString = jsonString.replaceAll("\\", "\\\\");

我得到另一个例外:

Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
 ^
    at java.util.regex.Pattern.error(Pattern.java:1955)
    at java.util.regex.Pattern.compile(Pattern.java:1702)
    at java.util.regex.Pattern.<init>(Pattern.java:1351)
    at java.util.regex.Pattern.compile(Pattern.java:1028)
    at java.lang.String.replaceAll(String.java:2223)
    at com.openbravo.pos.forms.AppConfig.convertConfigToJson(AppConfig.java:332)
    at com.openbravo.pos.forms.AppConfig.load(AppConfig.java:221)
    at com.openbravo.pos.forms.AppConfig.init(AppConfig.java:141)
    at com.openbravo.pos.forms.AppConfig.<init>(AppConfig.java:70)
    at com.openbravo.pos.forms.AppConfig.GetAppConfig(AppConfig.java:78)
    at com.openbravo.pos.forms.StartPOS$1.run(StartPOS.java:99)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我该如何处理? 任何帮助都非常感谢。

1 个答案:

答案 0 :(得分:0)

Properties个文件中,您必须转义=:,但在原始Java中您不需要。

正确的逃避(我相信)应该是:

"jdbc:derby:C:\\Users\\Sasha\\unicentaopos-database;create=true"

即。您只需要\ {和"(如果有的话)。