首页 > 解决方案 > 在终端中自动将回显结果显示为漂亮的打印 JSON

问题描述

我正在处理一个 PHP 脚本,我正在返回一个 JSON 数组的结果。通常,一旦我在终端中得到结果,我就会手动输入:

echo '{the result}' | python -m json.tool

在我的代码中有什么方法可以做到这一点,所以当它运行时它会自动打印返回结构?

这是代码:

$data = json_encode($data);                                                                   
   $ch = curl_init($url);                                                                        
   curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                              
   curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                                                  
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                               
   curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                   
       'Content-Type: application/json',                                                         
       'Content-Length: ' . strlen($data),
       'Expect: '
   ));                                                                                           
                                                                                                 
   $result = curl_exec($ch);                                                                     
   curl_close($ch);                                                                              
                                                                                                 
   echo $result;  

$result 在代码运行后返回如下内容:

{"result":{"opportunity":[{"id":"6579732483","ownerID":"313505310","dealStageID":"434077699","accountID":"10363064323","campaignID":"808040451","opportunityName":"Opp 1","probability":"10","amount":"10000","isClosed":"0","isWon":"0","closeDate":"2020-06-30 00:00:00","createTimestamp":"2020-06-08 16:11:37","originatingLeadID":"719186417667","isActive":"1","primaryLeadID":"721297804291","opportunity_field_5ed6d251540e0":null,"opp_field_2_5ee13dbbe9394":null,"image_link_1_5ee1648e3764a":null,"image_link_2_5ee164a2e1929":null,"image_link_3_5ee164b251037":null}]},"error":null,"id":"","callCount":"12","queryLimit":"50000"}

我已经尝试将 $result 包装在 JSON_PRETTY_PRINT 中,但这会在返回中返回 / 字符,这不会在终端中打印我想要的方式。

根据建议,我已将响应编码为 JSON,然后像这样传递 JSON_PRETTY_PRINT:

$result = json_encode(curl_exec($ch),JSON_PRETTY_PRINT);

但是,这似乎只是添加了 / 字符,这些字符不会显示为格式化的新行:

"{\"result\":{\"opportunity\":[{\"id\":\"6579732483\",\"ownerID\":\"313505310\",\"dealStageID\":\"434077699\",\"accountID\":\"10363064323\",\"campaignID\":\"808040451\",\"opportunityName\":\"Opp 1\",\"probability\":\"10\",\"amount\":\"10000\",\"isClosed\":\"0\",\"isWon\":\"0\",\"closeDate\":\"2020-06-30 00:00:00\",\"createTimestamp\":\"2020-06-08 16:11:37\",\"originatingLeadID\":\"719186417667\",\"isActive\":\"1\",\"primaryLeadID\":\"721297804291\",\"opportunity_field_5ed6d251540e0\":null,\"opp_field_2_5ee13dbbe9394\":null,\"image_link_1_5ee1648e3764a\":null,\"image_link_2_5ee164a2e1929\":null,\"image_link_3_5ee164b251037\":null}]},\"error\":null,\"id\":\"\",\"callCount\":\"25\",\"queryLimit\":\"50000\"}"

有没有办法像下面这样自动打印:

{
    "callCount": "26",
    "error": null,
    "id": "",
    "queryLimit": "50000",
    "result": {
        "opportunity": [
            {
                "accountID": "10363064323",
                "amount": "10000",
                "campaignID": "808040451",
                "closeDate": "2020-06-30 00:00:00",
                "createTimestamp": "2020-06-08 16:11:37",
                "dealStageID": "434077699",
                "id": "6579732483",
                "image_link_1_5ee1648e3764a": null,
                "image_link_2_5ee164a2e1929": null,
                "image_link_3_5ee164b251037": null,
                "isActive": "1",
                "isClosed": "0",
                "isWon": "0",
                "opp_field_2_5ee13dbbe9394": null,
                "opportunityName": "Opp 1",
                "opportunity_field_5ed6d251540e0": null,
                "originatingLeadID": "719186417667",
                "ownerID": "313505310",
                "primaryLeadID": "721297804291",
                "probability": "10"
            }
        ]
    }
}

谢谢你的帮助!

标签: jsonmacosterminal

解决方案


推荐阅读