Search code examples
javajsonmessageformat

Escaped Double Quotes with MessageFormat


I have the following String in JSON format:

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

But I want to set it up to use 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.

Any idea how to get this to work?


Solution

  • You should escape outer braces so put { and } in single quotes :

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