0

I have errors coming up with many things.

The biggest errors I have are with backslashes, greater than and less than operators, and quotes. I've done these operations on my text:

text.replaceAll("\"", "\\\"")
text.replaceAll("[^\\x00-\\x7F]", "")

None of these help. Is there anyone one method I can call to format a text into a JSON friendly text?

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
Shatr
  • 31
  • 1
  • 4

1 Answers1

0

There is no such thing as “JSON-friendly text”, since JSON can encode all Unicode strings.

To convert an arbitrary string into JSON format, use a JSON library like Jackson:

@Test
public void json() throws JsonProcessingException {
  TextNode textNode = new TextNode("quote\"quote");
  String jsonString = new ObjectMapper().writeValueAsString(textNode);
  assertEquals("\"quote\\\"quote\"", jsonString);
}
Roland Illig
  • 417
  • 1
  • 5
  • 15