How to format JSON insert in IntelliJ / Android Studio

I often have to use a text editor when writing code to insert random notes, but especially JSON responses, where I format them using a plugin (for Sublime).

I recently heard about the "scratch file" function in IntelliJ / Android Studio, which does exactly what I need - except that I cannot format it. Json i inserted in nicely.

How can I create the JSON format for Android Studio in a scratch buffer file?

+30
source share
4 answers

You ask about two different things: files with scratches and buffers from scratch.

When creating a scratch file in IntelliJ, you can choose the type of file (for example, JSON) that you want to create. Based on the file type, IntelliJ provides code formatting (use Code-> Reformat code).

However, buffers from scratch are just plain .txt files, and the only format you can use is the one that is associated with the .txt format. So, if you put JSON in the buffer from scratch, it will not be formatted with formatting like JSON.

I would recommend using scratch files instead of buffers from scratch if you want to format JSON.

Further information can be found on the official IntelliJ page at https://www.jetbrains.com/help/idea/2016.2/scratches.html .

+40
source

I select the code and run Command Option L (shortcut for the Code-> Reformat Code menu).

On my Mac keyboard connected to Mac, this is Command Shift L

Please note that this only works if the code is correctly formed JSON (clear all red squiggles).

+37
source

If you run the latest version, I will create a new Scratch file of type JSON. It is very easy, for example. press double shift, then find the new file from scratch, select JSON as the language, insert your fragment, and then use the shortcut that you usually use to format any file in intelliJ.

+3
source

choose from the menu: code → reformat code

Hotkey on Windows -:

CTRL + Alt + L 
+1
source

Source: https://habr.com/ru/post/1261181/


All Articles