Visual Studio 2012 IDE 中的 JSON 编辑器(突出显示、折叠、验证)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14335294/
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
JSON editor (highlight, collapse, validate) in Visual Studio 2012 IDE
提问by Sergey Novikov
It is strange, but I've been searching a while with no acceptable result for finding a tool to highlight, validate and collapse JSON file data to edit manually in visual studio. I'm not even dreaming about IntelliSense yet. It is so popular format and no chance to edit it IDE? No plugins or native support. Trying to wire scripteditior to json had no effect.
这很奇怪,但我一直在搜索一段时间没有找到可接受的结果来找到一个工具来突出显示、验证和折叠 JSON 文件数据以在 Visual Studio 中手动编辑。我什至还没有梦想过智能感知。它是如此流行的格式,却没有机会在 IDE 中对其进行编辑?没有插件或本机支持。尝试将脚本编辑器连接到 json 没有效果。
The closest thing I've got to be able to edit json manually in convenient way is Google Chrome extension for http://jsoneditoronline.org/that allows me to open and save files from local disk.
我必须能够以方便的方式手动编辑 json 的最接近的事情是http://jsoneditoronline.org/ 的Google Chrome 扩展程序,它允许我从本地磁盘打开和保存文件。
I've also tried to search for solutions for Visual Studio 2010 - but could not find any for this version either.
我还尝试搜索 Visual Studio 2010 的解决方案 - 但也找不到适用于该版本的任何解决方案。
Anybody know how I can have this functionality in VS IDE?
有人知道我如何在 VS IDE 中拥有此功能吗?
采纳答案by Dmitry Pavlov
Great news!
好消息!
In CTP2 of Visual Studio 2013 Update 2that was added a New JSON project item and editor
在 Visual Studio 2013 Update 2 的 CTP2 中添加了一个新的 JSON 项目项和编辑器


回答by Frederic Torres
One option is to use the Text Highlighterextension for Visual Studio 2012, which offers syntax highlighting for the following text formats:
一种选择是使用Visual Studio 2012的文本突出显示扩展,它为以下文本格式提供语法突出显示:
- .json (also offer syntax validation)
- .bat
- .cmd
- .ini
- .txt
- .log
- .json(还提供语法验证)
- 。蝙蝠
- .cmd
- .ini
- 。文本
- 。日志
回答by Dmitry Pavlov
There is another possible workaround - Web Essentials extension for Visual Studio.
还有另一种可能的解决方法 - Visual Studio 的 Web Essentials 扩展。
I have a project where I keep my data as JSON in the text files. I need to keep it well JSON formatted(no extra or missed commas, etc..) and also I need to be able to expand / collapseJS objects (nodes) in editor.
我有一个项目,我将数据作为 JSON 保存在文本文件中。我需要保持良好的 JSON 格式(没有额外或遗漏的逗号等),并且我还需要能够在编辑器中展开/折叠JS 对象(节点)。
To achieve that I used a Visual Studio extension - Web Essentials. You could also install it from VS Gallery via Package Manager within your IDE. This extension provides the features I need.
为了实现这一点,我使用了 Visual Studio 扩展 - Web Essentials。您也可以通过 IDE 中的包管理器从 VS Gallery 安装它。这个扩展提供了我需要的功能。
I renamed my file.json to file.js (after that Web Essentials starts regoignizing it as JS file).
我将我的 file.json 重命名为 file.js(之后 Web Essentials 开始将其重新定义为 JS 文件)。
The only little trick is to add a pseudo variable 'var z =' to make my file JS valid.
唯一的小技巧是添加一个伪变量 'var z =' 以使我的文件 JS 有效。


Initially my JSON data was:
最初我的 JSON 数据是:
{
"company": "ABC Company",
"employees":
[
{ "firstName": "John", "lastName": "Doe" },
{ "firstName": "Anna", "lastName": "Smith" },
{ "firstName": "Peter", "lastName": "Jones" }
]
}
After adding 'var z ='variable:
添加'var z ='变量后:
var z = {
"company": "ABC Company",
"employees":
[
{ "firstName": "John", "lastName": "Doe" },
{ "firstName": "Anna", "lastName": "Smith" },
{ "firstName": "Peter", "lastName": "Jones" }
]
};
As I read JSON file on server side - all I need is to remove 'var z =' prefix before sending JSON content to browser.
当我在服务器端读取 JSON 文件时 - 我需要的是在将 JSON 内容发送到浏览器之前删除 'var z =' 前缀。
Hope this helps!
希望这可以帮助!

