24

I am developing Java using intellij idea. And I want to assign a json like format to a Java String. But I am tired with escape characters that I have to deal with.

Is there any easy tool to format without escape characters?

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
User4506837
  • 317
  • 1
  • 2
  • 7

2 Answers2

78

You can use inject language feature.

Ex:

String myJsonString = "";
  • Place cursor between " and ".
  • Alt + Enter -> Inject language or reference -> select json from the dropdown appears,
  • Now you have injected json to that String. Again Alt + Enter -> Edit Json Fragment.
  • Now you can give your json inside pop-up tool as a normal json, Intellij will convert it as a String with escape characters for you. :))

enter image description here

Refer official documentation for more info.

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
0

For anyone that is using STS (Spring Tool Suite) and generally Eclipse the escape feature can be enabled by checking the checkbox under:

Preferences -> Java -> Editor -> Typing -> Escape text when pasting into a string literal

after this just define a String and paste the json content and the IDE will take care of the escaping.

NiVeR
  • 9,644
  • 4
  • 30
  • 35