Emacs 模式编辑 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/435847/
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
Emacs mode to edit JSON
提问by Ryszard Szopa
Does anybody know a good Emacs mode to edit JSON? An app I am working on uses a JSON based communication protocol and having the data nicely indented and syntax-highlighted would help me a lot in the process of figuring it out.
有人知道编辑 JSON 的良好 Emacs 模式吗?我正在开发的一个应用程序使用基于 JSON 的通信协议,并且将数据很好地缩进并突出显示语法将在我弄清楚它的过程中帮助我很多。
采纳答案by jfs
Have you tried Steve Yegge's js2-mode for Emacs?
你试过 Steve Yegge 的Emacs js2-mode吗?
回答by jstevenco
+1 for Josh's json-mode-- works well for me. I added
+1 为 Josh 的json-mode- 对我来说效果很好。我加了
(defun beautify-json ()
(interactive)
(let ((b (if mark-active (min (point) (mark)) (point-min)))
(e (if mark-active (max (point) (mark)) (point-max))))
(shell-command-on-region b e
"python -m json.tool" (current-buffer) t)))
and
和
(define-key json-mode-map (kbd "C-c C-f") 'beautify-json)
to json-mode.el to make the shell command invocation easier.
到 json-mode.el 使 shell 命令调用更容易。
UPDATE:For those of you with a need/desire to do this with unicode, see my question here. The upshot is rather than using:
更新:对于那些需要/希望使用 unicode 执行此操作的人,请在此处查看我的问题。结果是而不是使用:
python -m json.tool
you will want to use
你会想要使用
python -c 'import sys,json; data=json.loads(sys.stdin.read()); print json.dumps(data,sort_keys=True,indent=4).decode("unicode_escape").encode("utf8","replace")'
This both beautifies the JSON as well as preserving the original Unicode content.
这既美化了 JSON 又保留了原始的 Unicode 内容。
回答by Steve
js-mode supports syntax highlighting and indentation for json files.
js-mode 支持 json 文件的语法高亮和缩进。
This is as of Emacs 23.2, when espresso-mode was incorporated into Emacs and renamed js-mode.
这是从 Emacs 23.2 开始的,当时 espresso-mode 被合并到 Emacs 并重命名为 js-mode。
Check it out: http://www.nongnu.org/espresso/
回答by Josh
If you want something lightweight try this major-mode I hacked together: https://github.com/joshwnj/json-mode
如果你想要一些轻量级的东西,试试我一起破解的这个主要模式:https: //github.com/joshwnj/json-mode
It's actually no more than some extra syntax highlighting on top of javascript-mode, but for my purposes I've found it to work quite well.
它实际上只不过是在 javascript-mode 之上的一些额外的语法突出显示,但就我的目的而言,我发现它工作得很好。
Another common use-case is auto-formatting a JSON file (eg. if it's whitespace-compressed and you want more readability). To do this I'm just piping the buffer through a command-line script: C-u M-|
另一个常见的用例是自动格式化 JSON 文件(例如,如果它是空格压缩的并且您想要更高的可读性)。为此,我只是通过命令行脚本管道缓冲区:Cu M-|
回答by Mariusz Nowak
I've prepared a workaround for js2-modeso it parses json files without errors. You can find it in my comment: http://code.google.com/p/js2-mode/issues/detail?id=50#c7
我已经为js2-mode准备了一个解决方法,因此它可以正确解析 json 文件。您可以在我的评论中找到它:http: //code.google.com/p/js2-mode/issues/detail?id=50#c7
(I wanted to post it as a comment do J.F. Sebastian solution, but it seems I'm not allowed to do so (no 'add comment' link))
(我想将其发布为 JF Sebastian 解决方案的评论,但似乎我不允许这样做(没有“添加评论”链接))
回答by gavenkoa
json.elby Edward O'Connor is part of GNU Emacs since 23.1 (2008).
json.el由 Edward O'Connor 自 23.1 (2008) 起成为 GNU Emacs 的一部分。
While it isn't a syntax highlighter, it has a useful function to format JSON:
虽然它不是语法高亮器,但它具有格式化 JSON 的有用功能:
M-x json-pretty-print-buffer RET
So, if you have a recent version of Emacs, there is no need for jqor python -m json.tool.
因此,如果您有最新版本的 Emacs,则不需要jq或python -m json.tool。
回答by Eric O Lebigot
Since JSON is a subset of YAML, yaml-modeworks too (I don't know how it compares to js-modeand json-mode, though).
由于 JSON 是 YAML 的子集,因此yaml-mode也可以使用(不过我不知道它与js-mode和相比如何json-mode)。
Install (from emacs): M-x package-install yaml-mode.
安装(来自 emacs):M-x package-install yaml-mode.
Association of yaml-modewith YAML and JSON files, in ~/.emacs.d/init.el:
yaml-mode与 YAML 和 JSON 文件的关联,在~/.emacs.d/init.el:
(add-to-list 'auto-mode-alist '("\.yaml$" . yaml-mode))
(add-to-list 'auto-mode-alist '("\.json$" . yaml-mode))
回答by djb
I will also second Josh's json-mode, but also recommend flymake-json as an addition. It helps highlight syntax errors.
我还将第二个 Josh 的 json-mode,但也推荐 flymake-json 作为补充。它有助于突出显示语法错误。
I don't like using python -mjson.toolbecause it reorders items in JSON objects.
I find (prog-indent-sexp)works just fine to reindent, and using jsonlintinstead of python -mjson.toolworks for pretty printing/reformatting in beautify-json
我不喜欢使用,python -mjson.tool因为它会重新排序 JSON 对象中的项目。我发现(prog-indent-sexp)可以很好地重新缩进,并且使用jsonlint而不是python -mjson.tool用于漂亮的打印/重新格式化beautify-json
(eval-after-load "json-mode"
'(progn
(require 'flymake-json)
;; flymake-cursor displays error in minibuffer message area instead of requiring hover
(require 'flymake-cursor)
(add-hook 'json-mode-hook 'flymake-json-load)
(define-key json-mode-map "\C-c\C-n" (function flymake-goto-next-error))
)
)
回答by Aaron Miller
I've expanded on Mariusz Nowak's workaround, to make it usable as a major mode in its own right. Little modification was required beyond simply deriving the mode; the only change Nowak's work actually needed was the ability to recognize buffers not associated with files, or associated with files whose names don't end in .json, as JSON, which we accomplish with a buffer-local variable.
我已经扩展了 Mariusz Nowak 的解决方法,使其本身可以用作主要模式。除了简单地导出模式之外,几乎不需要修改;Nowak 的工作实际需要的唯一更改是能够识别与文件无关的缓冲区,或者与名称不.json以 .
Here's the augmented workaround:
这是增强的解决方法:
(make-variable-buffer-local 'js2-parse-as-json)
(defadvice js2-reparse (before json)
(setq js2-buffer-file-name buffer-file-name))
(ad-activate 'js2-reparse)
(defadvice js2-parse-statement (around json)
(if (and (= tt js2-LC)
js2-buffer-file-name
(or js2-parse-as-json
(string-equal (substring js2-buffer-file-name -5) ".json"))
(eq (+ (save-excursion
(goto-char (point-min))
(back-to-indentation)
(while (eolp)
(next-line)
(back-to-indentation))
(point)) 1) js2-ts-cursor))
(setq ad-return-value (js2-parse-assign-expr))
ad-do-it))
(ad-activate 'js2-parse-statement)
(define-derived-mode json-mode js2-mode "JSON"
"Major mode for editing JSON data."
:group 'json
(setq js2-parse-as-json t)
(js2-reparse t))
(add-to-list 'auto-mode-alist '("\.json$" . json-mode))
If you already use js2-mode, this may be a better option than js-modeplus flymake-jsonbecause you need not install anything new (js2-mode already does syntax checking, no need for an external tool), and because this mode will inherit your js2-mode configuration, which js-mode will not.
如果你已经使用 js2-mode,这可能是一个比js-modeplus更好的选择,flymake-json因为你不需要安装任何新的东西(js2-mode 已经做了语法检查,不需要外部工具),并且因为这个模式会继承你的 js2-mode配置,哪个 js-mode 不会。
回答by Gang Chen
js3-mode:https://github.com/thomblake/js3-mode
js3 模式:https: //github.com/thomblake/js3-mode
js3-mode is an improved js2-mode
js3-mode 是一种改进的 js2-mode
This package can be installed by package-list-packages command
这个包可以通过 package-list-packages 命令安装

