2017-04-06 29 views
0

我需要创建一个com.typesafe.config.Config对象从包含反斜杠“\”的Windows路径的JSON字符串。如何在类型安全的ConfigFactory中避免反斜线?

下面是我的代码

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使用?

+0

您是否试过使用\\? –

+0

@Paolo:我已经在使用'\\'参见主要方法 – Ricky

+0

双击反斜杠,正如@Ivan Pronin在答案中所说的那样。甚至3在某些情况下 –

回答

0

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

+0

我已经使用'\ \'看主要方法 – Ricky

+0

总共你应该用3 \\\来转义一个\ –

+0

如果我尝试给出3个反斜杠'\\\ jre ...'程序没有编译。 lang.Error:未解决的问题编译:无效的转义序列(有效问卷是\ b \牛逼\ n \˚F\ r \” \” \\)' – Ricky

相关问题