19

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
  • 10,994
  • 9
  • 49
  • 82
User4506837
  • 247
  • 1
  • 2
  • 7

2 Answers2

67

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
  • 10,994
  • 9
  • 49
  • 82
  • I cannot see `json` in dropdown. Can I somehow add it? I'm using Community Edition of IDEA. – ASten Jan 11 '20 at 09:52
  • 1
    Now see it. It was unavailable within quotes as argument of function (`..someFunc("{\"jsonHere\"}")`). When I've copied escaped json into assignment (`String t = "here"`), it appears. Thank you! – ASten Jan 11 '20 at 09:58
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,280
  • 4
  • 29
  • 34