[英]How to detect and indent json substrings inside longer non-json text?
[英]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
我用一個單行有 3,612,683 個字符的 JSON 文件對此進行了測試。 BBEdit 打開此文件並重新格式化,但未顯示 SPOD。
我剛剛在捆綁包中更正了這個問題,對於 2.0 用戶,捆綁包應該在 24 小時內更新並進行更正。
解決方案 1:使用Python
這個答案類似於這個答案,除了我使用python file
來做 JSON 格式。
bbedit
應用程序打開,則退出它,~/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())
要進行測試,請在 BBEdit 中打開一個 JSON 文件。
選擇Text
--> Apply Text Filter
--> pretty-json.py
如果您遇到格式錯誤等任何問題,則上述腳本將在新文件中添加錯誤,並且不會更改原始 JSON。
這個答案不是這種情況
參考:https ://gist.github.com/brokaw/95ade1358954cd97d0f2c8e992e14b08
有關更多信息:請參閱此
上述過濾器適用於較小的 JSON 文件,但如果 JSON 文件很大(約 40MB),則格式化會很慢。
要解決此問題,請使用以下解決方案
解決方案2:使用jq
為了更快的 json 格式,
brew install jq
fast-json-pretty.sh
文件#!/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。
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 => 按:
這是一個簡單地使用 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")
~/Library/Application\\ Support/BBEdit/Text\\ Filters/
創建一個腳本文件fast-json-pretty.sh
#!/usr/bin/env bash
/usr/local/bin/jq .
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.