简体   繁体   English

使用MessageFormat的转义双引号

[英]Escaped Double Quotes with MessageFormat

I have the following String in JSON format: 我有以下JSON格式的字符串:

String message = "{ \"message\": \"Hello World!\" }";

But I want to set it up to use MessageFormat: 但我想将其设置为使用MessageFormat:

String message = MessageFormat.format("{ \"message\": \"Hello {0}!\" }", "World");

I know MessageFormat uses single quotes vs backslash to escape characters, but I am not seeing a way around using a backslash here since I need the double quotes in the message and without the backslash, the unescaped double quotes break the string. 我知道MessageFormat使用单引号和反斜杠来转义字符,但是我在这里看不到使用反斜杠的方法,因为我需要消息中的双引号并且没有反斜杠,未转义的双引号会破坏字符串。

Any idea how to get this to work? 任何想法如何使它工作?

您应该将大括号转义,所以将{}放在单引号中:

 String message = MessageFormat.format("'{' \"message\": \"Hello {0}!\" '}'", "World");

使用两个转义字符作为转义字符,并使用转义的引号作为引号。

String message = MessageFormat.format("{ \\\"message\\\": \\\"Hello {0}!\\\" }", "World");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM