User4506837
User4506837

Reputation: 367

Format Json Strings intellij?

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?

Upvotes: 29

Views: 40001

Answers (2)

Supun Wijerathne
Supun Wijerathne

Reputation: 12948

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.

Upvotes: 87

NiVeR
NiVeR

Reputation: 9786

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.

Upvotes: 0

Related Questions