eclipse 如何将 javascript 格式应用于 .json 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7541823/
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
How to apply javascript formatting to .json files?
提问by Jader Dias
The Ctrl+ Shift+ Fhotkey in Eclipse can format a file. It doesn't work for .json
files. How to make it work?
本Ctrl+ Shift+F在Eclipse热键可以格式的文件。它不适用于.json
文件。如何使它工作?
采纳答案by OldWolf
You'll want to get the JSON Editor plugin if you don't already have it. You can find it here
如果您还没有 JSON 编辑器插件,您将需要它。你可以在这里找到
The JSON Editor is a simple plugin for the Eclipse IDE that provides: - Color text highlighting - An Outline Tree view - JSON validation - Text formatting - Text folding for the JSON data format.
JSON 编辑器是 Eclipse IDE 的一个简单插件,它提供: - 颜色文本突出显示 - 大纲树视图 - JSON 验证 - 文本格式 - JSON 数据格式的文本折叠。
If the hot keys still don't work. Take a look under the menu as shown in the picture from their site here
如果热键仍然不起作用。就拿菜单下看看如图片所示,从他们的网站在这里
Also, I see that there has been at least one issue with what looks to be the current versions formatting feature in the past. From their discussion site:
此外,我发现过去似乎是当前版本格式的功能至少存在一个问题。从他们的讨论网站:
rlespinola
2010-07-15 00:18:05 UTC
Using version 0.9.4, I do not see the option to "Format Text". Also, when I open a .json file, the outline view says "An outline is not available".
jdschulteis
2010-12-27 16:59:24 UTC
Using 0.9.4 on Helios, I also had "An outline is not available". I went to Window->Preferences->General->Editors->File Associations, selected '*.json' in the 'File types:' list, selected 'Json Editor' in the 'Associated editors:' list, and clicked 'Default'. I now get an outline, though it can be somewhat slow (6K line file).
Edit:
编辑:
There are several ways to add a plugin. Eclipse recommends using the update manager. Try help > software updates > find and install
有几种方法可以添加插件。Eclipse 建议使用更新管理器。尝试帮助 > 软件更新 > 查找并安装
You can look herefor some alternative methods.
您可以在此处查看一些替代方法。
回答by Xerxiz
There are two options I figured out using Eclipse Luna (4.4.0).
我使用 Eclipse Luna (4.4.0) 找到了两个选项。
Use a JSON Editor Plugin and define shortcuts
使用 JSON 编辑器插件并定义快捷方式
- Download and install the JSON Editor Plugin from sourceforge manually or use the Eclipse marketplace (Help -> Eclipse marketplace) to find and install the plugin
- Go to Window -> Preferences -> General -> Keys and filter for "format text".
- Choose "Format Text" and set the "When:" value to "Editing Text" (sadly there is no explicit condition for JSON editing, but the format event for the JSON Editor is different to the other editors, so "Editing Text" will work aswell)
- Set the "Binding:" to Ctrl+ Shift+ F
- 手动从 sourceforge 下载并安装 JSON Editor Plugin 或使用 Eclipse 市场(帮助 -> Eclipse 市场)查找并安装插件
- 转到窗口 -> 首选项 -> 常规 -> 键并过滤“格式文本”。
- 选择“格式化文本”并将“何时:”值设置为“编辑文本”(遗憾的是没有明确的 JSON 编辑条件,但 JSON 编辑器的格式事件与其他编辑器不同,因此“编辑文本”将也工作)
- 将“绑定”到Ctrl+ Shift+F
Use the Javascript Development Plugin with an ugly and anoying workaround
使用带有丑陋和烦人的解决方法的 Javascript 开发插件
- Get the plugin using Help -> Install New Software -> Work with: "http://download.eclipse.org/releases/luna" -> Programming Languages -> JavaScript Development Tools
- Associate *.json files with the JavaScript Editor (Window -> Preferences -> General -> Editors -> File Associations)
- You can now create files with "json" extension and edit them in Eclipse using the JavaScript Editor, but formatting with Ctrl+ Shift+ Fwill not work directly with the following unformatted example:
- 使用帮助获取插件 -> 安装新软件 -> 使用:“ http://download.eclipse.org/releases/luna” -> 编程语言 -> JavaScript 开发工具
- 将 *.json 文件与 JavaScript 编辑器关联(窗口 -> 首选项 -> 常规 -> 编辑器 -> 文件关联)
- 现在,您可以创建一个“JSON”的延伸和使用JavaScript编辑器进行编辑在Eclipse中的文件,但格式Ctrl+ Shift+F不会与下面的例子格式化直接工作:
{"addressbook": {"name": "John Doe",
"address": {
"street": "5 Main Street", "city": "San Diego, CA", "zip": 91912
},
"phoneNumbers": [
"619 555-3452",
"664 555-4667"
]
}
}
- The hack is to create a valid JavaScript variable from the object description like this:
- hack 是从对象描述中创建一个有效的 JavaScript 变量,如下所示:
var obj = {"addressbook": {"name": "John Doe",
"address": {
"street": "5 Main Street", "city": "San Diego, CA", "zip": 91912
},
"phoneNumbers": [
"619 555-3452",
"664 555-4667"
]
}
}
- Using Ctrl+ Shift+ Fwill now work
- In the end you will have to remove the "var obj =" hack to make the JSON file valid again
- 使用Ctrl+ Shift+F现在工作
- 最后,您必须删除“var obj =" hack 才能使 JSON 文件再次有效
回答by Marcel St?r
You could use the JavaScript editor that ships with Eclipse.
您可以使用 Eclipse 附带的 JavaScript 编辑器。