簡體   English   中英

如何在 TextMate、Emacs、BBEdit 或 Sublime Text 2 中縮進 JSON 數據?

[英]How to indent JSON data inside of TextMate, Emacs, BBEdit, or Sublime Text 2?

[更新:這個問題發布 8 小時后, JSON 包的作者收到了這個問題的通知,他修復了它。]

我在文件application.json中有以下 JSON 數據,顯示在本文末尾,我使用 TextMate 和JSON bundle 、Emacs、BBEdit 和 Sublime Text 2 來正確地縮進它,但一切似乎都無法做到't。

TextMate 和 Sublime Text 2 都堅持第一個{不應該縮進,第一個主要問題是"child": { TextMate 和 Sublime Text 2 都拒絕將}對齊到"child": { Emacs 繼續為每一行進一步縮進,BBEdit 似乎根本沒有重新縮進 function(這可能是嗎?)。

有沒有辦法正確縮進文件,或者 TextMate 和 Sublime Text 2 是否都對 JSON 數據做正確的事情?

[ 
{
    "settings": [ "master" ],
    "appPort": "8666",
    "specs": {
        "frame" : {
            "type" : "HTMLFrameMojit",

            "config": {
                "deploy": true,
                "child": {
                    "type" : "HelloWorldMojit"
                    },
                    "assets": {
                        "top": {
                            "css": [
                            "/static/HelloWorldMojit/assets/index.css"
                            ]
                        }
                    }
                }
            }
        }
        },
        {
            "settings": [ "environment:development" ],
            "staticHandling": {
                "forceUpdate": true
            }
        }
        ]

找到了一個簡單且運行良好的 BBEdit 解決方案。

將以下腳本放入
~/Library/Application Support/BBEdit/Text Filters/FormatJSON.sh

#!/bin/bash
python -m json.tool
  1. 在 BBEdit 中打開一個 JSON 文件。 無需重新啟動 BBEdit,因為 BBEdit 非常棒!
  2. 選擇文本 > 應用文本過濾器 > FormatJSON

我用一個單行有 3,612,683 個字符的 JSON 文件對此進行了測試。 BBEdit 打開此文件並重新格式化,但未顯示 SPOD。

我剛剛在捆綁包中更正了這個問題,對於 2.0 用戶,捆綁包應該在 24 小時內更新並進行更正。

解決方案 1:使用Python

這個答案類似於這個答案,除了我使用python file來做 JSON 格式。

  1. 如果bbedit應用程序打開,則退出它,
  2. 將以下腳本pretty-json.py放在~/Library/Application\\ Support/BBEdit/Text\\ Filters/路徑中
#!/usr/bin/env python
# You can change above she-bang line depending on your Mac configuration

import sys
import json

def main():
    input = sys.stdin.read()
    try:
        obj = json.loads(input)
    except Exception as e:
        print input + "\n\nERROR: " + str(e)
        return 1
    print(json.dumps(obj, indent=2))
    return 0

if __name__ == '__main__':
    sys.exit(main())
  1. 要進行測試,請在 BBEdit 中打開一個 JSON 文件。

  2. 選擇Text --> Apply Text Filter --> pretty-json.py

  3. 如果您遇到格式錯誤等任何問題,則上述腳本將在新文件中添加錯誤,並且不會更改原始 JSON。
    這個答案不是這種情況

參考:https ://gist.github.com/brokaw/95ade1358954cd97d0f2c8e992e14b08

有關更多信息:請參閱

上述過濾器適用於較小的 JSON 文件,但如果 JSON 文件很大(約 40MB),則格式化會很慢。

要解決此問題,請使用以下解決方案

解決方案2:使用jq

為了更快的 json 格式,

  1. 安裝jq brew install jq
  2. 在與上述文件位置相同的位置添加fast-json-pretty.sh文件
  3. 重啟bbedit。
#!/bin/bash
 jq

在 BBEdit 14.0 及更高版本中,文本菜單上的“Reformat Document”將重排 JSON。您還可以使用支持重新格式化的 JSON 語言服務器支持內置語言服務器。

根據http://jsonprettyprint.com/ Textmate 和 Sublime 沒有做正確的事情。

你用的是什么版本的Emacs?

在 24.2.1 中,您的 JSON blob 在js-mode (Emac 的默認 javascript 主要模式)中完美縮進,沒有問題。


如果您進行任何重要的 Javascript 開發,我建議您簽出 js2-mode https://github.com/mooz/js2-mode ,它將 Emacs 變成一個很棒的 JS IDE。

崇高漂亮的 JSON

Sublime Pretty JSON將第一個{縮進。 這就是我得到的:

[
  {
    "settings": [
      "master"
    ],
    "appPort": "8666",
    "specs": {
      "frame": {
        "type": "HTMLFrameMojit",
        "config": {
          "deploy": true,
          "child": {
            "type": "HelloWorldMojit"
          },
          "assets": {
            "top": {
              "css": [
                "/static/HelloWorldMojit/assets/index.css"
              ]
            }
          }
        }
      }
    }
  },
  {
    "settings": [
      "environment:development"
    ],
    "staticHandling": {
      "forceUpdate": true
    }
  }
]

安裝

在 Sublime Text 2 中:Preference => Package Control => Install Package => "Pretty Json" => Restart Sublime => 選擇 JSON Text => 按:

  • Linux:ctrl+alt+j
  • 視窗:ctrl+alt+j
  • OS X:cmd+ctrl+j

這是一個簡單地使用 Google Chrome 或 NodeJS 本身的解決方案,方法如下:

只需打開 Google Chrome 開發工具或 NodeJS 提示符,然后輸入

obj = 

並將對象復制並粘貼到其中,所以它會像

obj = [
      { 
        // etc

現在,在谷歌瀏覽器中,只需輸入

JSON.stringify(obj, null, 4)

您將擁有格式化的 JSON。 在 NodeJS 中,由於它會逐字打印出\\n ,因此您可以使用

console.log(JSON.stringify(obj, null, 4))

如果您希望縮進只有 2 個空格,只需使用2而不是4

如果您希望縮進是制表符而不是空格,只需使用

JSON.stringify(obj, null, "\t")
  1. ~/Library/Application\\ Support/BBEdit/Text\\ Filters/創建一個腳本文件fast-json-pretty.sh
  2. 插入以下腳本:
#!/usr/bin/env bash
/usr/local/bin/jq .
  1. 按如下方式應用文本過濾器: Menu bar --> Text --> Apply Text Filter --> fast-json-pretty

這是他的 JQ 腳本的 DKB 版本的重新設計版本,因為它,至少對我來說,需要完整路徑和末尾的點 (.) 才能完成這項工作。

只需檢查安裝了哪個版本的 python:

僅當 python 解釋器安裝在被調用的 shell 中時,使用 json_pretty 才有效。 例如,我的 json_pretty 有效,我在 Ventura 上對其進行了如下修改:

#!/bin/zsh
python3 -m json.tool

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM