0%

格式化Curl返回的Json字符

格式化Curl返回的Json字符


经常会用到curl调试接口,服务器返回的是json,不过这些json是没有格式化的,不方便阅读。

经过搜索和实验,发现下面2中方式比较方便。

示例:

1
curl https://news-at.zhihu.com/api/4/news/latest
2
{"date":"20171014","stories":[{"title":"这些有故事的 DOTA 职业选手外号(国外篇)","ga_prefix":"101417","images":["https:\/\/pic3.zhimg.com\/v2-471f6f1170fcb7d491ba54404acaf30a.jpg"],"multipic":true,"type":0,"id":9651211},{"images":["https:\/\/pic1.zhimg.com\/v2-16e9abb39a4fb4dd56994c9db9378110.jpg"],"type":0,"id":9649645,"ga_prefix":"101416","title":"被你的宠物捉弄的的时候,你想过「动物是否会骗人」吗?"},{"images":["https:\/\/pic3.zhimg.com\/v2-5ab5db73049a413a6810677ad3817602.jpg"],"type":0,"id":9651376,"ga_prefix":"101415","title":"想明白 iPhone X 的人脸识别是怎么工作的,先得了解这道「光」"},{"images":["https:\/\/pic1.zhimg.com\/v2-c20f4551e1725eef3ac7cd502e9ed71c.jpg"],"type":0,"id":9649434,"ga_prefix":"101414","title":"不靠专利,拿什么保护发明?世博会的数据会告诉你"},{"images":["https:\/\/pic1.zhimg.com\/v2-a0a033debd47042be4554a3450fe9478.jpg"],"type":0,"id":9650760,"ga_prefix":"101413","title":"回头看这项世界顶级比赛的历史,对它的存在意义愈发迷茫"},{"images":["https:\/\/pic4.zhimg.com\/v2-0df458ee4784b68befc40c03ddf5bc67.jpg"],"type":0,"id":9646702,"ga_prefix":"101412","title":"大误 · 一次咨询"},{"images":["https:\/\/pic2.zhimg.com\/v2-9d0caf3ba46c60a15826e53ea8473a5d.jpg"],"type":0,"id":9634763,"ga_prefix":"101411","title":"黄金钩焖五花肉,绵软浓香的口感,其他菜绝对没法比"},{"images":["https:\/\/pic4.zhimg.com\/v2-c4964723821a835fb42a4f70bfa8ce6f.jpg"],"type":0,"id":9650778,"ga_prefix":"101410","title":"Ta 的一生可以写 20 本书,至于是男是女,已经不重要了"},{"images":["https:\/\/pic1.zhimg.com\/v2-054e30b7cec2624a849af1e358528cd8.jpg"],"type":0,"id":9634507,"ga_prefix":"101409","title":"中国有哪些不出名,但值得一去的山?"},{"images":["https:\/\/pic4.zhimg.com\/v2-95d371314244ca1ce74bfbd22ba0938f.jpg"],"type":0,"id":9651324,"ga_prefix":"101408","title":"- 实在想不到怎么出国更炫酷了\r\n- 喏,自己开飞机去"},{"images":["https:\/\/pic2.zhimg.com\/v2-06dd1c008cac334147cd81abf0bfefe1.jpg"],"type":0,"id":9649625,"ga_prefix":"101407","title":"早起来一份「班尼迪克蛋」,做个逼格满满的早餐网红"},{"images":["https:\/\/pic4.zhimg.com\/v2-b0e9676d36b7fd4b1822ba8cb93bf4f7.jpg"],"type":0,"id":9651424,"ga_prefix":"101407","title":"丁俊晖:那个满脸青春痘的少年球手,几度大起大落已是而立之年"},{"images":["https:\/\/pic3.zhimg.com\/v2-a065e8278298efb317b13d92084275f6.jpg"],"type":0,"id":9650104,"ga_prefix":"101407","title":"在你读过的童话中,是不是一对姐妹里坏的那个总是姐姐?"},{"images":["https:\/\/pic3.zhimg.com\/v2-db3702f9cee08897c8f7a174000f0ca2.jpg"],"type":0,"id":9651366,"ga_prefix":"101406","title":"瞎扯 · 如何正确地吐槽"}],"top_stories":[{"image":"https:\/\/pic3.zhimg.com\/v2-e5dc45c4698771e7001e1ae1c27ba8b6.jpg","type":0,"id":9651376,"ga_prefix":"101415","title":"想明白 iPhone X 的人脸识别是怎么工作的,先得了解这道「光」"},{"image":"https:\/\/pic3.zhimg.com\/v2-ce790cb7a633fbc7aa8e2303e3c1ce16.jpg","type":0,"id":9651424,"ga_prefix":"101407","title":"丁俊晖:那个满脸青春痘的少年球手,几度大起大落已是而立之年"},{"image":"https:\/\/pic2.zhimg.com\/v2-36e6e13f557c2dd29e65d8c23fa9cec5.jpg","type":0,"id":9634507,"ga_prefix":"101409","title":"中国有哪些不出名,但值得一去的山?"},{"image":"https:\/\/pic4.zhimg.com\/v2-79056fe95d9834f9c8b30957980b0193.jpg","type":0,"id":9649434,"ga_prefix":"101414","title":"不靠专利,拿什么保护发明?世博会的数据会告诉你"},{"image":"https:\/\/pic4.zhimg.com\/v2-45e71d718938e0a4e292b12e52269a07.jpg","type":0,"id":9650104,"ga_prefix":"101407","title":"在你读过的童话中,是不是一对姐妹里坏的那个总是姐姐?"}]}

Python 格式化

在curl命令后面添加 | python -m json.tool 即可。

如下所示

1
curl https://news-at.zhihu.com/api/4/news/latest | python -m json.tool
2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
3
                                 Dload  Upload   Total   Spent    Left  Speed
4
100  3901  100  3901    0     0  33333      0 --:--:-- --:--:-- --:--:-- 33629
5
{
6
    "date": "20171014",
7
    "stories": [
8
        {
9
            "ga_prefix": "101417",
10
            "id": 9651211,
11
            "images": [
12
                "https://pic3.zhimg.com/v2-471f6f1170fcb7d491ba54404acaf30a.jpg"
13
            ],
14
            "multipic": true,
15
            "title": "\u8fd9\u4e9b\u6709\u6545\u4e8b\u7684 DOTA \u804c\u4e1a\u9009\u624b\u5916\u53f7\uff08\u56fd\u5916\u7bc7\uff09",
16
            "type": 0
17
        },
18
        {
19
            "ga_prefix": "101416",
20
            "id": 9649645,
21
            "images": [
22
                "https://pic1.zhimg.com/v2-16e9abb39a4fb4dd56994c9db9378110.jpg"
23
            ],
24
            "title": "\u88ab\u4f60\u7684\u5ba0\u7269\u6349\u5f04\u7684\u7684\u65f6\u5019\uff0c\u4f60\u60f3\u8fc7\u300c\u52a8\u7269\u662f\u5426\u4f1a\u9a97\u4eba\u300d\u5417\uff1f",
25
            "type": 0
26
        },
27
        {
28
            "ga_prefix": "101415",
29
            "id": 9651376,
30
            "images": [
31
                "https://pic3.zhimg.com/v2-5ab5db73049a413a6810677ad3817602.jpg"
32
            ],
33
            "title": "\u60f3\u660e\u767d iPhone X \u7684\u4eba\u8138\u8bc6\u522b\u662f\u600e\u4e48\u5de5\u4f5c\u7684\uff0c\u5148\u5f97\u4e86\u89e3\u8fd9\u9053\u300c\u5149\u300d",
34
            "type": 0
35
        },
36
        {
37
            "ga_prefix": "101414",
38
            "id": 9649434,
39
            "images": [
40
                "https://pic1.zhimg.com/v2-c20f4551e1725eef3ac7cd502e9ed71c.jpg"
41
            ],
42
            "title": "\u4e0d\u9760\u4e13\u5229\uff0c\u62ff\u4ec0\u4e48\u4fdd\u62a4\u53d1\u660e\uff1f\u4e16\u535a\u4f1a\u7684\u6570\u636e\u4f1a\u544a\u8bc9\u4f60",
43
            "type": 0
44
        },
45
        {
46
            "ga_prefix": "101413",
47
            "id": 9650760,
48
            "images": [
49
                "https://pic1.zhimg.com/v2-a0a033debd47042be4554a3450fe9478.jpg"
50
            ],
51
            "title": "\u56de\u5934\u770b\u8fd9\u9879\u4e16\u754c\u9876\u7ea7\u6bd4\u8d5b\u7684\u5386\u53f2\uff0c\u5bf9\u5b83\u7684\u5b58\u5728\u610f\u4e49\u6108\u53d1\u8ff7\u832b",
52
            "type": 0
53
        },
54
        {
55
            "ga_prefix": "101412",
56
            "id": 9646702,
57
            "images": [
58
                "https://pic4.zhimg.com/v2-0df458ee4784b68befc40c03ddf5bc67.jpg"
59
            ],
60
            "title": "\u5927\u8bef \u00b7 \u4e00\u6b21\u54a8\u8be2",
61
            "type": 0
62
        },
63
        {
64
            "ga_prefix": "101411",
65
            "id": 9634763,
66
            "images": [
67
                "https://pic2.zhimg.com/v2-9d0caf3ba46c60a15826e53ea8473a5d.jpg"
68
            ],
69
            "title": "\u9ec4\u91d1\u94a9\u7116\u4e94\u82b1\u8089\uff0c\u7ef5\u8f6f\u6d53\u9999\u7684\u53e3\u611f\uff0c\u5176\u4ed6\u83dc\u7edd\u5bf9\u6ca1\u6cd5\u6bd4",
70
            "type": 0
71
        },
72
        {
73
            "ga_prefix": "101410",
74
            "id": 9650778,
75
            "images": [
76
                "https://pic4.zhimg.com/v2-c4964723821a835fb42a4f70bfa8ce6f.jpg"
77
            ],
78
            "title": "Ta \u7684\u4e00\u751f\u53ef\u4ee5\u5199 20 \u672c\u4e66\uff0c\u81f3\u4e8e\u662f\u7537\u662f\u5973\uff0c\u5df2\u7ecf\u4e0d\u91cd\u8981\u4e86",
79
            "type": 0
80
        },
81
        {
82
            "ga_prefix": "101409",
83
            "id": 9634507,
84
            "images": [
85
                "https://pic1.zhimg.com/v2-054e30b7cec2624a849af1e358528cd8.jpg"
86
            ],
87
            "title": "\u4e2d\u56fd\u6709\u54ea\u4e9b\u4e0d\u51fa\u540d\uff0c\u4f46\u503c\u5f97\u4e00\u53bb\u7684\u5c71\uff1f",
88
            "type": 0
89
        },
90
        {
91
            "ga_prefix": "101408",
92
            "id": 9651324,
93
            "images": [
94
                "https://pic4.zhimg.com/v2-95d371314244ca1ce74bfbd22ba0938f.jpg"
95
            ],
96
            "title": "- \u5b9e\u5728\u60f3\u4e0d\u5230\u600e\u4e48\u51fa\u56fd\u66f4\u70ab\u9177\u4e86\r\n- \u558f\uff0c\u81ea\u5df1\u5f00\u98de\u673a\u53bb",
97
            "type": 0
98
        },
99
        {
100
            "ga_prefix": "101407",
101
            "id": 9649625,
102
            "images": [
103
                "https://pic2.zhimg.com/v2-06dd1c008cac334147cd81abf0bfefe1.jpg"
104
            ],
105
            "title": "\u65e9\u8d77\u6765\u4e00\u4efd\u300c\u73ed\u5c3c\u8fea\u514b\u86cb\u300d\uff0c\u505a\u4e2a\u903c\u683c\u6ee1\u6ee1\u7684\u65e9\u9910\u7f51\u7ea2",
106
            "type": 0
107
        },
108
        {
109
            "ga_prefix": "101407",
110
            "id": 9651424,
111
            "images": [
112
                "https://pic4.zhimg.com/v2-b0e9676d36b7fd4b1822ba8cb93bf4f7.jpg"
113
            ],
114
            "title": "\u4e01\u4fca\u6656\uff1a\u90a3\u4e2a\u6ee1\u8138\u9752\u6625\u75d8\u7684\u5c11\u5e74\u7403\u624b\uff0c\u51e0\u5ea6\u5927\u8d77\u5927\u843d\u5df2\u662f\u800c\u7acb\u4e4b\u5e74",
115
            "type": 0
116
        },
117
        {
118
            "ga_prefix": "101407",
119
            "id": 9650104,
120
            "images": [
121
                "https://pic3.zhimg.com/v2-a065e8278298efb317b13d92084275f6.jpg"
122
            ],
123
            "title": "\u5728\u4f60\u8bfb\u8fc7\u7684\u7ae5\u8bdd\u4e2d\uff0c\u662f\u4e0d\u662f\u4e00\u5bf9\u59d0\u59b9\u91cc\u574f\u7684\u90a3\u4e2a\u603b\u662f\u59d0\u59d0\uff1f",
124
            "type": 0
125
        },
126
        {
127
            "ga_prefix": "101406",
128
            "id": 9651366,
129
            "images": [
130
                "https://pic3.zhimg.com/v2-db3702f9cee08897c8f7a174000f0ca2.jpg"
131
            ],
132
            "title": "\u778e\u626f \u00b7 \u5982\u4f55\u6b63\u786e\u5730\u5410\u69fd",
133
            "type": 0
134
        }
135
    ],
136
    "top_stories": [
137
        {
138
            "ga_prefix": "101415",
139
            "id": 9651376,
140
            "image": "https://pic3.zhimg.com/v2-e5dc45c4698771e7001e1ae1c27ba8b6.jpg",
141
            "title": "\u60f3\u660e\u767d iPhone X \u7684\u4eba\u8138\u8bc6\u522b\u662f\u600e\u4e48\u5de5\u4f5c\u7684\uff0c\u5148\u5f97\u4e86\u89e3\u8fd9\u9053\u300c\u5149\u300d",
142
            "type": 0
143
        },
144
        {
145
            "ga_prefix": "101407",
146
            "id": 9651424,
147
            "image": "https://pic3.zhimg.com/v2-ce790cb7a633fbc7aa8e2303e3c1ce16.jpg",
148
            "title": "\u4e01\u4fca\u6656\uff1a\u90a3\u4e2a\u6ee1\u8138\u9752\u6625\u75d8\u7684\u5c11\u5e74\u7403\u624b\uff0c\u51e0\u5ea6\u5927\u8d77\u5927\u843d\u5df2\u662f\u800c\u7acb\u4e4b\u5e74",
149
            "type": 0
150
        },
151
        {
152
            "ga_prefix": "101409",
153
            "id": 9634507,
154
            "image": "https://pic2.zhimg.com/v2-36e6e13f557c2dd29e65d8c23fa9cec5.jpg",
155
            "title": "\u4e2d\u56fd\u6709\u54ea\u4e9b\u4e0d\u51fa\u540d\uff0c\u4f46\u503c\u5f97\u4e00\u53bb\u7684\u5c71\uff1f",
156
            "type": 0
157
        },
158
        {
159
            "ga_prefix": "101414",
160
            "id": 9649434,
161
            "image": "https://pic4.zhimg.com/v2-79056fe95d9834f9c8b30957980b0193.jpg",
162
            "title": "\u4e0d\u9760\u4e13\u5229\uff0c\u62ff\u4ec0\u4e48\u4fdd\u62a4\u53d1\u660e\uff1f\u4e16\u535a\u4f1a\u7684\u6570\u636e\u4f1a\u544a\u8bc9\u4f60",
163
            "type": 0
164
        },
165
        {
166
            "ga_prefix": "101407",
167
            "id": 9650104,
168
            "image": "https://pic4.zhimg.com/v2-45e71d718938e0a4e292b12e52269a07.jpg",
169
            "title": "\u5728\u4f60\u8bfb\u8fc7\u7684\u7ae5\u8bdd\u4e2d\uff0c\u662f\u4e0d\u662f\u4e00\u5bf9\u59d0\u59b9\u91cc\u574f\u7684\u90a3\u4e2a\u603b\u662f\u59d0\u59d0\uff1f",
170
            "type": 0
171
        }
172
    ]
173
}

如果不想显示curl的统计信息,可以参考这篇文章,添加 -s参数即可。

1
curl https://news-at.zhihu.com/api/4/news/latest  -s | python -m json.tool

Nodejs 格式化

用nvm安装一个json库,这里是库的地址 文档。

安装json命令

1
npm install -g json

在curl命令后面添加 | json 即可。

如下所示:

1
curl https://news-at.zhihu.com/api/4/news/latest -s | json
2
{
3
  "date": "20171014",
4
  "stories": [
5
    {
6
      "title": "这些有故事的 DOTA 职业选手外号(国外篇)",
7
      "ga_prefix": "101417",
8
      "images": [
9
        "https://pic3.zhimg.com/v2-471f6f1170fcb7d491ba54404acaf30a.jpg"
10
      ],
11
      "multipic": true,
12
      "type": 0,
13
      "id": 9651211
14
    },
15
    {
16
      "images": [
17
        "https://pic1.zhimg.com/v2-16e9abb39a4fb4dd56994c9db9378110.jpg"
18
      ],
19
      "type": 0,
20
      "id": 9649645,
21
      "ga_prefix": "101416",
22
      "title": "被你的宠物捉弄的的时候,你想过「动物是否会骗人」吗?"
23
    },
24
    {
25
      "images": [
26
        "https://pic3.zhimg.com/v2-5ab5db73049a413a6810677ad3817602.jpg"
27
      ],
28
      "type": 0,
29
      "id": 9651376,
30
      "ga_prefix": "101415",
31
      "title": "想明白 iPhone X 的人脸识别是怎么工作的,先得了解这道「光」"
32
    },
33
    {
34
      "images": [
35
        "https://pic1.zhimg.com/v2-c20f4551e1725eef3ac7cd502e9ed71c.jpg"
36
      ],
37
      "type": 0,
38
      "id": 9649434,
39
      "ga_prefix": "101414",
40
      "title": "不靠专利,拿什么保护发明?世博会的数据会告诉你"
41
    },
42
    {
43
      "images": [
44
        "https://pic1.zhimg.com/v2-a0a033debd47042be4554a3450fe9478.jpg"
45
      ],
46
      "type": 0,
47
      "id": 9650760,
48
      "ga_prefix": "101413",
49
      "title": "回头看这项世界顶级比赛的历史,对它的存在意义愈发迷茫"
50
    },
51
    {
52
      "images": [
53
        "https://pic4.zhimg.com/v2-0df458ee4784b68befc40c03ddf5bc67.jpg"
54
      ],
55
      "type": 0,
56
      "id": 9646702,
57
      "ga_prefix": "101412",
58
      "title": "大误 · 一次咨询"
59
    },
60
    {
61
      "images": [
62
        "https://pic2.zhimg.com/v2-9d0caf3ba46c60a15826e53ea8473a5d.jpg"
63
      ],
64
      "type": 0,
65
      "id": 9634763,
66
      "ga_prefix": "101411",
67
      "title": "黄金钩焖五花肉,绵软浓香的口感,其他菜绝对没法比"
68
    },
69
    {
70
      "images": [
71
        "https://pic4.zhimg.com/v2-c4964723821a835fb42a4f70bfa8ce6f.jpg"
72
      ],
73
      "type": 0,
74
      "id": 9650778,
75
      "ga_prefix": "101410",
76
      "title": "Ta 的一生可以写 20 本书,至于是男是女,已经不重要了"
77
    },
78
    {
79
      "images": [
80
        "https://pic1.zhimg.com/v2-054e30b7cec2624a849af1e358528cd8.jpg"
81
      ],
82
      "type": 0,
83
      "id": 9634507,
84
      "ga_prefix": "101409",
85
      "title": "中国有哪些不出名,但值得一去的山?"
86
    },
87
    {
88
      "images": [
89
        "https://pic4.zhimg.com/v2-95d371314244ca1ce74bfbd22ba0938f.jpg"
90
      ],
91
      "type": 0,
92
      "id": 9651324,
93
      "ga_prefix": "101408",
94
      "title": "- 实在想不到怎么出国更炫酷了\r\n- 喏,自己开飞机去"
95
    },
96
    {
97
      "images": [
98
        "https://pic2.zhimg.com/v2-06dd1c008cac334147cd81abf0bfefe1.jpg"
99
      ],
100
      "type": 0,
101
      "id": 9649625,
102
      "ga_prefix": "101407",
103
      "title": "早起来一份「班尼迪克蛋」,做个逼格满满的早餐网红"
104
    },
105
    {
106
      "images": [
107
        "https://pic4.zhimg.com/v2-b0e9676d36b7fd4b1822ba8cb93bf4f7.jpg"
108
      ],
109
      "type": 0,
110
      "id": 9651424,
111
      "ga_prefix": "101407",
112
      "title": "丁俊晖:那个满脸青春痘的少年球手,几度大起大落已是而立之年"
113
    },
114
    {
115
      "images": [
116
        "https://pic3.zhimg.com/v2-a065e8278298efb317b13d92084275f6.jpg"
117
      ],
118
      "type": 0,
119
      "id": 9650104,
120
      "ga_prefix": "101407",
121
      "title": "在你读过的童话中,是不是一对姐妹里坏的那个总是姐姐?"
122
    },
123
    {
124
      "images": [
125
        "https://pic3.zhimg.com/v2-db3702f9cee08897c8f7a174000f0ca2.jpg"
126
      ],
127
      "type": 0,
128
      "id": 9651366,
129
      "ga_prefix": "101406",
130
      "title": "瞎扯 · 如何正确地吐槽"
131
    }
132
  ],
133
  "top_stories": [
134
    {
135
      "image": "https://pic3.zhimg.com/v2-e5dc45c4698771e7001e1ae1c27ba8b6.jpg",
136
      "type": 0,
137
      "id": 9651376,
138
      "ga_prefix": "101415",
139
      "title": "想明白 iPhone X 的人脸识别是怎么工作的,先得了解这道「光」"
140
    },
141
    {
142
      "image": "https://pic3.zhimg.com/v2-ce790cb7a633fbc7aa8e2303e3c1ce16.jpg",
143
      "type": 0,
144
      "id": 9651424,
145
      "ga_prefix": "101407",
146
      "title": "丁俊晖:那个满脸青春痘的少年球手,几度大起大落已是而立之年"
147
    },
148
    {
149
      "image": "https://pic2.zhimg.com/v2-36e6e13f557c2dd29e65d8c23fa9cec5.jpg",
150
      "type": 0,
151
      "id": 9634507,
152
      "ga_prefix": "101409",
153
      "title": "中国有哪些不出名,但值得一去的山?"
154
    },
155
    {
156
      "image": "https://pic4.zhimg.com/v2-79056fe95d9834f9c8b30957980b0193.jpg",
157
      "type": 0,
158
      "id": 9649434,
159
      "ga_prefix": "101414",
160
      "title": "不靠专利,拿什么保护发明?世博会的数据会告诉你"
161
    },
162
    {
163
      "image": "https://pic4.zhimg.com/v2-45e71d718938e0a4e292b12e52269a07.jpg",
164
      "type": 0,
165
      "id": 9650104,
166
      "ga_prefix": "101407",
167
      "title": "在你读过的童话中,是不是一对姐妹里坏的那个总是姐姐?"
168
    }
169
  ]
170
}
坚持原创技术分享,您的支持将鼓励我继续创作!