如何在 IntelliJ/Android Studio 中格式化粘贴的 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41117918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:44:59  来源:igfitidea点击:

How to format pasted JSON in IntelliJ / Android Studio

jsonandroid-studiointellij-ideacode-formatting

提问by Daniel Wilson

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

我经常需要在编写代码时使用文本编辑器来粘贴随机注释,尤其是 JSON 响应,我使用插件(用于 Sublime)格式化它们。

I recently heard about the 'scratch file' feature in IntelliJ / Android Studio which does exactly what I need it to - except I can't make it format JSON I paste in nicely.

我最近听说 IntelliJ/Android Studio 中的“暂存文件”功能,它完全符合我的需要 - 除了我无法将它格式化为我很好地粘贴的 JSON。

How can I make Android Studio format JSON in a scratch buffer file?

如何在暂存缓冲区文件中制作 Android Studio 格式的 JSON?

回答by Jason D

I highlight the code and run CommandOptionL(a short-cut for the Code->Reformat Code menu).

我突出显示代码并运行CommandOptionL(代码->重新格式化代码菜单的快捷方式)。

Note that this only works if the code is well-formed JSON (clear any red squiggles).

请注意,这仅适用于代码格式良好的 JSON(清除任何红色波浪线)。

回答by Kamil Kubacki

You are asking about two seperate things: scratch files and scratch buffers.

您问的是两个不同的事情:暂存文件和暂存缓冲区。

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

在 IntelliJ 中创建临时文件时,您可以选择要创建的文件类型(例如 JSON)。根据文件的类型,IntelliJ 提供代码格式化(使用 Code->Reformat code)。

However, scratch buffers are just simple .txt files and the only formatting that can be used is the one associated to .txt format. So, if you put JSON into scratch buffer it won't get formatted with JSON type formatter.

但是,暂存缓冲区只是简单的 .txt 文件,唯一可以使用的格式是与 .txt 格式相关的格式。因此,如果您将 JSON 放入暂存缓冲区,则不会使用 JSON 类型格式化程序对其进行格式化。

I would encourage you to use scratch files instead of scratch buffers if you want JSON formatting.

如果您想要 JSON 格式,我鼓励您使用临时文件而不是临时缓冲区。

More information can be found at IntelliJ's official page https://www.jetbrains.com/help/idea/2016.2/scratches.html.

更多信息可以在 IntelliJ 的官方页面https://www.jetbrains.com/help/idea/2016.2/scratches.html 找到

回答by Dror

choose from the menu : code -> reformat code

从菜单中选择:代码 -> 重新格式化代码

hotkey On Windows -:

Windows 上的热键 -:

CTRL + Alt + L

Important note-if you have sensitive information in your JSON- NEVERuse online tools to parse or beautify it. because this will compromise your organization's and customer's information

重要说明 -如果您的 JSON 中有敏感信息 -切勿使用在线工具来解析或美化它。因为这会损害您组织和客户的信息

回答by Thanos

If you run the latest version I'd create a new Scratch File of type JSON. It's really easy, e.g. hit double shift, then search for new scratch file, select JSON as the language, paste your snippet and then use the shortcut that you usually use for formatting any file in intelliJ.

如果您运行最新版本,我将创建一个 JSON 类型的新 Scratch 文件。这真的很容易,例如点击双班,然后搜索新的临时文件,选择 JSON 作为语言,粘贴您的代码段,然后使用您通常用于在 intelliJ 中格式化任何文件的快捷方式。