如何在类型安全的ConfigFactory中转义反斜杠?

时间:2017-04-06 05:52:50

标签: java

我需要从包含windows路径的json字符串创建一个com.typesafe.config.Config对象,其中包含反斜杠“\”。

以下是我的代码

private Config getReportNotificationStringForOrg(String eachOrgId,
        String type, String subType, String expiryDateTime, String title,
        String content, String contentType, long startDateTime,
        String description) {
    Config requestForQueryReplace = ConfigFactory
            .parseString("{ \"org_id\":\""
                    + eachOrgId
                    + "\",  \"type\": \""
                    + type
                    + "\",  \"subtype\": \""
                    + subType
                    + "\", \"expiry_datetime\": \""
                    + expiryDateTime
                    + "\", \"title\": \""
                    + title
                    + "\", \"content\": \""
                    + content
                    + "\", \"content_type\": \""
                    + contentType
                    + "\", \"start_datetime\": \""
                    + startDateTime
                    + "\", \"created_by\": \"machine_generated\", \"description\": \""
                    + description + "\"}");

    String notificationInput = eqp.getDataPopulatedQueryUsingType("notification.notification_insert_input", requestForQueryReplace);

    return ConfigFactory.parseString(notificationInput);
}
public static void main(String[] args) {
    GenerateNotificationStoreInESImpl obj = new GenerateNotificationStoreInESImpl();
    System.out.println(obj.getReportNotificationStringForOrg("LUNAAA", "", "", "", "", "", "", 1494374400000, "<Agent installation directory>\\jre\\lib"));

}

这会抛出以下错误

Exception in thread "main" com.typesafe.config.ConfigException$Parse: String: 1: in value for key 'description': backslash followed by 'j', this is not a valid escape sequence (quoted strings use JSON escaping, so use double-backslash \\ for literal backslash)
    at com.typesafe.config.impl.Parser$ParseContext.nextToken(Parser.java:179)
    at com.typesafe.config.impl.Parser$ParseContext.nextTokenIgnoringNewline(Parser.java:199)
    at com.typesafe.config.impl.Parser$ParseContext.consolidateValueTokens(Parser.java:277)
    at com.typesafe.config.impl.Parser$ParseContext.parseObject(Parser.java:653)
    at com.typesafe.config.impl.Parser$ParseContext.parseValue(Parser.java:408)
    at com.typesafe.config.impl.Parser$ParseContext.parse(Parser.java:818)
    at com.typesafe.config.impl.Parser.parse(Parser.java:34)
    at com.typesafe.config.impl.Parseable.rawParseValue(Parseable.java:199)
    at com.typesafe.config.impl.Parseable.rawParseValue(Parseable.java:187)
    at com.typesafe.config.impl.Parseable.parseValue(Parseable.java:171)
    at com.typesafe.config.impl.Parseable.parseValue(Parseable.java:165)
    at com.typesafe.config.impl.Parseable.parse(Parseable.java:204)
    at com.typesafe.config.ConfigFactory.parseString(ConfigFactory.java:790)
    at com.typesafe.config.ConfigFactory.parseString(ConfigFactory.java:794)
    at com.abc.xyz.event.action.notification.GenerateNotificationStoreInESImpl.getReportNotificationStringForOrg(GenerateNotificationStoreInESImpl.java:83)
    at com.abc.xyz.event.action.notification.GenerateNotificationStoreInESImpl.main(GenerateNotificationStoreInESImpl.java:110)

如何转义“\”反斜杠以与com.typesafe.config.ConfigFactory一起使用?

1 个答案:

答案 0 :(得分:1)

答案在错误消息中说明,请仔细阅读:quoted strings use JSON escaping, so use double-backslash \\ for literal backslash