With Python 2.6+ you can just do:
(使用Python 2.6+,您可以执行以下操作:)
echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool
or, if the JSON is in a file, you can do:
(或者,如果JSON在文件中,则可以执行以下操作:)
python -m json.tool my_json.json
if the JSON is from an internet source such as an API, you can use
(如果JSON来自互联网来源(例如API),则可以使用)
curl http://my_url/ | python -m json.tool
For convenience in all of these cases you can make an alias:
(为方便起见,在所有这些情况下都可以使用别名:)
alias prettyjson='python -m json.tool'
For even more convenience with a bit more typing to get it ready:
(为了方便起见,请进行更多输入以使其就绪:)
prettyjson_s() {
echo "$1" | python -m json.tool
}
prettyjson_f() {
python -m json.tool "$1"
}
prettyjson_w() {
curl "$1" | python -m json.tool
}
for all the above cases.
(对于上述所有情况。)
You can put this in .bashrc
and it will be available every time in shell. (您可以将其放在.bashrc
并且每次在Shell中都可用。)
Invoke it like prettyjson_s '{"foo": "lorem", "bar": "ipsum"}'
. (像prettyjson_s '{"foo": "lorem", "bar": "ipsum"}'
一样调用它。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…