格式化curl返回的JSON数据

在开发的过程中会使用CURL命令请求API接口,而一般情况下API接口也是返回JSON数据,但是如果直接使用 curl -X GET https://news-at.zhihu.com/api/4/news/latest 这种方式访问API接口的话,你会发现终端输出的是一行JSON数据,如果可以美化一下JSON数据在终端输出岂不美哉~

二、格式化CURL返回的JSON数据有两种方式

分别是 PYTHON格式化NODEJS格式化

2.1、PYTHON格式化

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

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
$ curl -X GET https://news-at.zhihu.com/api/4/news/latest | python -m json.tool
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                   Dload  Upload   Total   Spent    Left  Speed
  100  3376  100  3376    0     0  39255      0 --:--:-- --:--:-- --:--:-- 39255
  {
      "date": "20210314",
      "stories": [
          {
              "ga_prefix": "031407",
              "hint": "VOL.1280",
              "id": 9734064,
              "image_hue": "0x7d99b3",
              "images": [
                  "https://pic4.zhimg.com/v2-20daa5159cfc755c20f5f3551ce0d973.jpg?source=8673f162"
              ],
              "title": "\u5c0f\u4e8b \u00b7 \u5e78\u597d\uff0c\u8fd8\u6709\u300c\u89e3\u8bed\u82b1\u300d",
              "type": 0,
              "url": "https://daily.zhihu.com/story/9734064"
          },
          {
              "ga_prefix": "031407",
              "hint": "\u4e01\u9999\u533b\u751f \u00b7 2 \u5206\u949f\u9605\u8bfb",
              "id": 9734000,
              "image_hue": "0xb3887d",
              "images": [
                  "https://pic2.zhimg.com/v2-957091b824ded683e0cba948a6b71eb8.jpg?source=8673f162"
              ],
              "title": "\u4eba\u7684\u773c\u76ae\u4e3a\u4ec0\u4e48\u4f1a\u8df3\uff1f",
              "type": 0,
              "url": "https://daily.zhihu.com/story/9734000"
          },
          {
              "ga_prefix": "031407",
              "hint": "\u77e5\u4e4e\u7528\u6237 \u00b7 4 \u5206\u949f\u9605\u8bfb",
              "id": 9734009,
              "image_hue": "0xa19170",
              "images": [
                  "https://pic4.zhimg.com/v2-a74d3680fc11fcf18bc6646f44f07135.jpg?source=8673f162"
              ],
              "title": "\u4e3a\u4ec0\u4e48\u79d2\u7684\u5b9a\u4e49\u8fd9\u4e48\u590d\u6742\uff1f",
              "type": 0,
              "url": "https://daily.zhihu.com/story/9734009"
          },
          {
              "ga_prefix": "031407",
              "hint": "\u8d75\u5fb7\u4f4f \u00b7 1 \u5206\u949f\u9605\u8bfb",
              "id": 9733995,
              "image_hue": "0x523c31",
              "images": [
                  "https://pic1.zhimg.com/v2-5ee75baffaac2e06099a5ddef02df5d0.jpg?source=8673f162"
              ],
              "title": "\u6709\u54ea\u4e9b\u4ee5\u524d\u5f88\u706b\uff0c\u73b0\u5728\u5374\u88ab\u53d1\u73b0\u662f\u5f88\u5371\u9669\u7684\u53d1\u660e\uff1f",
              "type": 0,
              "url": "https://daily.zhihu.com/story/9733995"
          },
          {
              "ga_prefix": "031407",
              "hint": "\u80e1\u8000\u5b87 \u00b7 4 \u5206\u949f\u9605\u8bfb",
              "id": 9733994,
              "image_hue": "0xa5846c",
              "images": [
                  "https://pic1.zhimg.com/v2-e44b65ecfdda76e21df0aa0d5f16d50e.jpg?source=8673f162"
              ],
              "title": "\u73b0\u4ee3\u4eba\u7684\u56f4\u68cb\u6280\u827a\u4e0e\u53e4\u4eba\u76f8\u6bd4\u662f\u5426\u6709\u63d0\u9ad8\uff1f",
              "type": 0,
              "url": "https://daily.zhihu.com/story/9733994"
          },
          {
              "ga_prefix": "031407",
              "hint": "\u8fd0\u52a8\u5206\u5b50 \u00b7 2 \u5206\u949f\u9605\u8bfb",
              "id": 9733993,
              "image_hue": "0x453830",
              "images": [
                  "https://pic2.zhimg.com/v2-a0e70b2062517f0e6cfa28dfdfd17126.jpg?source=8673f162"
              ],
              "title": "\u5965\u8fd0\u4f1a\u5c04\u51fb\u662f\u5426\u5141\u8bb8\u72d9\u51fb\u624b\u53c2\u8d5b\uff1f",
              "type": 0,
              "url": "https://daily.zhihu.com/story/9733993"
          }
      ],
      "top_stories": [
          {
              "ga_prefix": "030707",
              "hint": "\u4f5c\u8005 / \u53f8\u9a6c\u671b\u7530",
              "id": 9733765,
              "image": "https://pic2.zhimg.com/v2-24a178e99daa4833b6c2e5bb8b318366.jpg?source=8673f162",
              "image_hue": "0xb3847d",
              "title": "\u5730\u94c1\u5217\u8f66\u665a\u4e0a\u90fd\u505c\u5728\u54ea\u91cc\u5462\uff1f",
              "type": 0,
              "url": "https://daily.zhihu.com/story/9733765"
          },
          {
              "ga_prefix": "030507",
              "hint": "\u4f5c\u8005 / \u963f\u6e90\u8001\u5e08",
              "id": 9733704,
              "image": "https://pic4.zhimg.com/v2-11697abd3c557ac1394c829506379789.jpg?source=8673f162",
              "image_hue": "0x29303b",
              "title": "\u4e3a\u4ec0\u4e48\u6709\u4eba\u7ecf\u5e38\u751f\u75c5\uff0c\u6709\u4eba\u5374\u5f88\u5065\u5eb7\uff1f",
              "type": 0,
              "url": "https://daily.zhihu.com/story/9733704"
          },
          {
              "ga_prefix": "022807",
              "hint": "\u4f5c\u8005 / \u751c\u8349\u8393",
              "id": 9733550,
              "image": "https://pic1.zhimg.com/v2-25ed43abad9026b203746e9c22d4a35d.jpg?source=8673f162",
              "image_hue": "0x253625",
              "title": "\u6ca1\u6709\u4fe1\u53f7\u7684\u65f6\u5019\u628a\u624b\u673a\u4e3e\u9ad8\u9ad8\u6709\u7528\u5417\uff1f",
              "type": 0,
              "url": "https://daily.zhihu.com/story/9733550"
          },
          {
              "ga_prefix": "022607",
              "hint": "\u4f5c\u8005 / \u662f\u963f\u5f6c\u4e0d\u662f\u963f\u5bbe",
              "id": 9733459,
              "image": "https://pic1.zhimg.com/v2-b67669750f46868af39cafce3a0cd278.jpg?source=8673f162",
              "image_hue": "0x3c3b19",
              "title": "\u53e4\u4ee3\u4eba\u53d1\u73b0\u8fc7\u6050\u9f99\u5316\u77f3\u5417\uff1f",
              "type": 0,
              "url": "https://daily.zhihu.com/story/9733459"
          },
          {
              "ga_prefix": "022407",
              "hint": "\u4f5c\u8005 / \u4e94\u83b2\u82b1\u5f00",
              "id": 9733383,
              "image": "https://pic4.zhimg.com/v2-35592f1482474f7515f6e0b585839f9b.jpg?source=8673f162",
              "image_hue": "0x7d95b3",
              "title": "\u732b\u4ece\u9ad8\u5904\u843d\u4e0b\uff0c\u4e3a\u4ec0\u4e48\u603b\u80fd\u56db\u811a\u7740\u5730\uff1f",
              "type": 0,
              "url": "https://daily.zhihu.com/story/9733383"
          }
      ]
  }

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

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
$ curl -X GET https://news-at.zhihu.com/api/4/news/latest -s | python -m json.tool
{
    "date": "20210314",
    "stories": [
        {
            "ga_prefix": "031407",
            "hint": "VOL.1280",
            "id": 9734064,
            "image_hue": "0x7d99b3",
            "images": [
                "https://pic4.zhimg.com/v2-20daa5159cfc755c20f5f3551ce0d973.jpg?source=8673f162"
            ],
            "title": "\u5c0f\u4e8b \u00b7 \u5e78\u597d\uff0c\u8fd8\u6709\u300c\u89e3\u8bed\u82b1\u300d",
            "type": 0,
            "url": "https://daily.zhihu.com/story/9734064"
        },
        {
            "ga_prefix": "031407",
            "hint": "\u4e01\u9999\u533b\u751f \u00b7 2 \u5206\u949f\u9605\u8bfb",
            "id": 9734000,
            "image_hue": "0xb3887d",
            "images": [
                "https://pic2.zhimg.com/v2-957091b824ded683e0cba948a6b71eb8.jpg?source=8673f162"
            ],
            "title": "\u4eba\u7684\u773c\u76ae\u4e3a\u4ec0\u4e48\u4f1a\u8df3\uff1f",
            "type": 0,
            "url": "https://daily.zhihu.com/story/9734000"
        },
        {
            "ga_prefix": "031407",
            "hint": "\u77e5\u4e4e\u7528\u6237 \u00b7 4 \u5206\u949f\u9605\u8bfb",
            "id": 9734009,
            "image_hue": "0xa19170",
            "images": [
                "https://pic4.zhimg.com/v2-a74d3680fc11fcf18bc6646f44f07135.jpg?source=8673f162"
            ],
            "title": "\u4e3a\u4ec0\u4e48\u79d2\u7684\u5b9a\u4e49\u8fd9\u4e48\u590d\u6742\uff1f",
            "type": 0,
            "url": "https://daily.zhihu.com/story/9734009"
        },
        {
            "ga_prefix": "031407",
            "hint": "\u8d75\u5fb7\u4f4f \u00b7 1 \u5206\u949f\u9605\u8bfb",
            "id": 9733995,
            "image_hue": "0x523c31",
            "images": [
                "https://pic1.zhimg.com/v2-5ee75baffaac2e06099a5ddef02df5d0.jpg?source=8673f162"
            ],
            "title": "\u6709\u54ea\u4e9b\u4ee5\u524d\u5f88\u706b\uff0c\u73b0\u5728\u5374\u88ab\u53d1\u73b0\u662f\u5f88\u5371\u9669\u7684\u53d1\u660e\uff1f",
            "type": 0,
            "url": "https://daily.zhihu.com/story/9733995"
        },
        {
            "ga_prefix": "031407",
            "hint": "\u80e1\u8000\u5b87 \u00b7 4 \u5206\u949f\u9605\u8bfb",
            "id": 9733994,
            "image_hue": "0xa5846c",
            "images": [
                "https://pic1.zhimg.com/v2-e44b65ecfdda76e21df0aa0d5f16d50e.jpg?source=8673f162"
            ],
            "title": "\u73b0\u4ee3\u4eba\u7684\u56f4\u68cb\u6280\u827a\u4e0e\u53e4\u4eba\u76f8\u6bd4\u662f\u5426\u6709\u63d0\u9ad8\uff1f",
            "type": 0,
            "url": "https://daily.zhihu.com/story/9733994"
        },
        {
            "ga_prefix": "031407",
            "hint": "\u8fd0\u52a8\u5206\u5b50 \u00b7 2 \u5206\u949f\u9605\u8bfb",
            "id": 9733993,
            "image_hue": "0x453830",
            "images": [
                "https://pic2.zhimg.com/v2-a0e70b2062517f0e6cfa28dfdfd17126.jpg?source=8673f162"
            ],
            "title": "\u5965\u8fd0\u4f1a\u5c04\u51fb\u662f\u5426\u5141\u8bb8\u72d9\u51fb\u624b\u53c2\u8d5b\uff1f",
            "type": 0,
            "url": "https://daily.zhihu.com/story/9733993"
        }
    ],
    "top_stories": [
        {
            "ga_prefix": "030707",
            "hint": "\u4f5c\u8005 / \u53f8\u9a6c\u671b\u7530",
            "id": 9733765,
            "image": "https://pic2.zhimg.com/v2-24a178e99daa4833b6c2e5bb8b318366.jpg?source=8673f162",
            "image_hue": "0xb3847d",
            "title": "\u5730\u94c1\u5217\u8f66\u665a\u4e0a\u90fd\u505c\u5728\u54ea\u91cc\u5462\uff1f",
            "type": 0,
            "url": "https://daily.zhihu.com/story/9733765"
        },
        {
            "ga_prefix": "030507",
            "hint": "\u4f5c\u8005 / \u963f\u6e90\u8001\u5e08",
            "id": 9733704,
            "image": "https://pic4.zhimg.com/v2-11697abd3c557ac1394c829506379789.jpg?source=8673f162",
            "image_hue": "0x29303b",
            "title": "\u4e3a\u4ec0\u4e48\u6709\u4eba\u7ecf\u5e38\u751f\u75c5\uff0c\u6709\u4eba\u5374\u5f88\u5065\u5eb7\uff1f",
            "type": 0,
            "url": "https://daily.zhihu.com/story/9733704"
        },
        {
            "ga_prefix": "022807",
            "hint": "\u4f5c\u8005 / \u751c\u8349\u8393",
            "id": 9733550,
            "image": "https://pic1.zhimg.com/v2-25ed43abad9026b203746e9c22d4a35d.jpg?source=8673f162",
            "image_hue": "0x253625",
            "title": "\u6ca1\u6709\u4fe1\u53f7\u7684\u65f6\u5019\u628a\u624b\u673a\u4e3e\u9ad8\u9ad8\u6709\u7528\u5417\uff1f",
            "type": 0,
            "url": "https://daily.zhihu.com/story/9733550"
        },
        {
            "ga_prefix": "022607",
            "hint": "\u4f5c\u8005 / \u662f\u963f\u5f6c\u4e0d\u662f\u963f\u5bbe",
            "id": 9733459,
            "image": "https://pic1.zhimg.com/v2-b67669750f46868af39cafce3a0cd278.jpg?source=8673f162",
            "image_hue": "0x3c3b19",
            "title": "\u53e4\u4ee3\u4eba\u53d1\u73b0\u8fc7\u6050\u9f99\u5316\u77f3\u5417\uff1f",
            "type": 0,
            "url": "https://daily.zhihu.com/story/9733459"
        },
        {
            "ga_prefix": "022407",
            "hint": "\u4f5c\u8005 / \u4e94\u83b2\u82b1\u5f00",
            "id": 9733383,
            "image": "https://pic4.zhimg.com/v2-35592f1482474f7515f6e0b585839f9b.jpg?source=8673f162",
            "image_hue": "0x7d95b3",
            "title": "\u732b\u4ece\u9ad8\u5904\u843d\u4e0b\uff0c\u4e3a\u4ec0\u4e48\u603b\u80fd\u56db\u811a\u7740\u5730\uff1f",
            "type": 0,
            "url": "https://daily.zhihu.com/story/9733383"
        }
    ]
}

2.2、NODEJS格式化

使用NODEJS格式化需要使用NPM安装一个JSON处理库。执行 npm install -g json 后,在CURL命令后面添加 | json 即可。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
curl -X GET https://news-at.zhihu.com/api/4/news/latest -s | json
{
    "date":"20210314",
    "stories":[
        {
            "image_hue":"0x7d99b3",
            "title":"小事 · 幸好,还有「解语花」",
            "url":"https://daily.zhihu.com/story/9734064",
            "hint":"VOL.1280",
            "ga_prefix":"031407",
            "images":[
                "https://pic4.zhimg.com/v2-20daa5159cfc755c20f5f3551ce0d973.jpg?source=8673f162"
            ],
            "type":0,
            "id":9734064
        },
        {
            "image_hue":"0xb3887d",
            "title":"人的眼皮为什么会跳?",
            "url":"https://daily.zhihu.com/story/9734000",
            "hint":"丁香医生 · 2 分钟阅读",
            "ga_prefix":"031407",
            "images":[
                "https://pic2.zhimg.com/v2-957091b824ded683e0cba948a6b71eb8.jpg?source=8673f162"
            ],
            "type":0,
            "id":9734000
        },
        {
            "image_hue":"0xa19170",
            "title":"为什么秒的定义这么复杂?",
            "url":"https://daily.zhihu.com/story/9734009",
            "hint":"知乎用户 · 4 分钟阅读",
            "ga_prefix":"031407",
            "images":[
                "https://pic4.zhimg.com/v2-a74d3680fc11fcf18bc6646f44f07135.jpg?source=8673f162"
            ],
            "type":0,
            "id":9734009
        },
        {
            "image_hue":"0x523c31",
            "title":"有哪些以前很火,现在却被发现是很危险的发明?",
            "url":"https://daily.zhihu.com/story/9733995",
            "hint":"赵德住 · 1 分钟阅读",
            "ga_prefix":"031407",
            "images":[
                "https://pic1.zhimg.com/v2-5ee75baffaac2e06099a5ddef02df5d0.jpg?source=8673f162"
            ],
            "type":0,
            "id":9733995
        },
        {
            "image_hue":"0xa5846c",
            "title":"现代人的围棋技艺与古人相比是否有提高?",
            "url":"https://daily.zhihu.com/story/9733994",
            "hint":"胡耀宇 · 4 分钟阅读",
            "ga_prefix":"031407",
            "images":[
                "https://pic1.zhimg.com/v2-e44b65ecfdda76e21df0aa0d5f16d50e.jpg?source=8673f162"
            ],
            "type":0,
            "id":9733994
        },
        {
            "image_hue":"0x453830",
            "title":"奥运会射击是否允许狙击手参赛?",
            "url":"https://daily.zhihu.com/story/9733993",
            "hint":"运动分子 · 2 分钟阅读",
            "ga_prefix":"031407",
            "images":[
                "https://pic2.zhimg.com/v2-a0e70b2062517f0e6cfa28dfdfd17126.jpg?source=8673f162"
            ],
            "type":0,
            "id":9733993
        }
    ],
    "top_stories":[
        {
            "image_hue":"0xb3847d",
            "hint":"作者 / 司马望田",
            "url":"https://daily.zhihu.com/story/9733765",
            "image":"https://pic2.zhimg.com/v2-24a178e99daa4833b6c2e5bb8b318366.jpg?source=8673f162",
            "title":"地铁列车晚上都停在哪里呢?",
            "ga_prefix":"030707",
            "type":0,
            "id":9733765
        },
        {
            "image_hue":"0x29303b",
            "hint":"作者 / 阿源老师",
            "url":"https://daily.zhihu.com/story/9733704",
            "image":"https://pic4.zhimg.com/v2-11697abd3c557ac1394c829506379789.jpg?source=8673f162",
            "title":"为什么有人经常生病,有人却很健康?",
            "ga_prefix":"030507",
            "type":0,
            "id":9733704
        },
        {
            "image_hue":"0x253625",
            "hint":"作者 / 甜草莓",
            "url":"https://daily.zhihu.com/story/9733550",
            "image":"https://pic1.zhimg.com/v2-25ed43abad9026b203746e9c22d4a35d.jpg?source=8673f162",
            "title":"没有信号的时候把手机举高高有用吗?",
            "ga_prefix":"022807",
            "type":0,
            "id":9733550
        },
        {
            "image_hue":"0x3c3b19",
            "hint":"作者 / 是阿彬不是阿宾",
            "url":"https://daily.zhihu.com/story/9733459",
            "image":"https://pic1.zhimg.com/v2-b67669750f46868af39cafce3a0cd278.jpg?source=8673f162",
            "title":"古代人发现过恐龙化石吗?",
            "ga_prefix":"022607",
            "type":0,
            "id":9733459
        },
        {
            "image_hue":"0x7d95b3",
            "hint":"作者 / 五莲花开",
            "url":"https://daily.zhihu.com/story/9733383",
            "image":"https://pic4.zhimg.com/v2-35592f1482474f7515f6e0b585839f9b.jpg?source=8673f162",
            "title":"猫从高处落下,为什么总能四脚着地?",
            "ga_prefix":"022407",
            "type":0,
            "id":9733383
        }
    ]
}

2.3、PYTHON格式化与NODEJS格式化的区别

PYTHON格式化,有于 json.tool 实现问题不支持处理UTF-8字符集,自然也就无法处理中文。NODEJS格式化使用的JSON库可以完美支持处理UTF-8字符集。

三、更多可以参考:CSDN | 格式化Curl返回的Json字符

comments powered by Disqus