Emacs模式编辑JSON

Rys*_*opa 74 unicode emacs json

有人知道一个好的Emacs模式来编辑JSON吗?我正在开发的一个应用程序使用基于JSON的通信协议,并且数据很好地缩进和语法突出显示将帮助我解决它的过程.

jst*_*nco 31

Josh的json模式 +1 - 对我来说效果很好.我补充道

(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)))
Run Code Online (Sandbox Code Playgroud)

(define-key json-mode-map (kbd "C-c C-f") 'beautify-json)
Run Code Online (Sandbox Code Playgroud)

到json-mode.el使shell命令调用更容易.

更新: 对于那些需要/希望使用unicode执行此操作的人,请在此处查看我的问题.结果不是使用:

python -m json.tool
Run Code Online (Sandbox Code Playgroud)

你会想要使用

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")'
Run Code Online (Sandbox Code Playgroud)

这既可以美化JSON,也可以保留原始的unicode内容.


Ste*_*eve 28

js-mode支持json文件的语法高亮和缩进.

这是Emacs 23.2,当espresso模式被合并到Emacs并重命名为js-mode时.

看看:http: //www.nongnu.org/espresso/

  • 太好了!我把`(setq auto-mode-alist(cons'("\\.json \\'".js-mode)auto-mode-alist))`放在我的`.emacs`中 (6认同)

jfs*_*jfs 16

你有没有尝试过Steve Yegge 用于Emacsjs2模式

  • 但是,js2模式并不真正支持JSON.http://code.google.com/p/js2-mode/issues/detail?id=50 (15认同)

小智 15

如果你想要轻量级的东西尝试这个主要模式我一起攻击:https://github.com/joshwnj/json-mode

它实际上只是在javascript-mode之上突出显示的一些额外语法,但出于我的目的,我发现它工作得很好.

另一个常见的用例是自动格式化JSON文件(例如,如果它是空格压缩的,并且您希望更具可读性).为此,我只是通过命令行脚本管道缓冲区:Cu M- |

  • fwiw,我分叉了这个仓库并添加了jstevenco的beautify-json函数,它位于https://github.com/chad3814/json-mode - 也发送了一个pull请求. (2认同)

Mar*_*wak 9

我为js2-mode准备了一个解决方法,因此它可以无错误地解析json文件.您可以在我的评论中找到它:http://code.google.com/p/js2-mode/issues/detail?id = 50#c7

(我想发布它作为评论做JF塞巴斯蒂安解决方案,但似乎我不允许这样做(没有'添加评论'链接))


gav*_*koa 5

json.el 由 Edward O'Connor 自 23.1 (2008) 起成为 GNU Emacs 的一部分。

虽然它不是语法高亮器,但它具有格式化 JSON 的有用功能:

M-x json-pretty-print-buffer RET
Run Code Online (Sandbox Code Playgroud)

因此,如果您有最新版本的 Emacs,则不需要jqpython -m json.tool