json - 在终端中自动将回显结果显示为漂亮的打印 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"
}
]
}
}
谢谢你的帮助!
解决方案
推荐阅读
- python - 寻找一种更好的方法来处理python中numpy数组或列表的周期性边界条件
- java - 如何在 JavaFX 的文本中显示数据库中的数据(列),以便在单击按钮后从列中获取下一行?
- javascript - 反应在加载时不采用默认页面
- android - Flutter 闪屏 - 构建失败可能是由于插件中的 AndroidX 不兼容
- xml - 如何将长 xml 文件拆分为单独的子目录中的多个单独的 xml 文件
- hl7-fhir - FHIR 扩展显示 ?gen-e1? 在IG
- html - 滚动时,关键帧元素位于固定 div 之上
- amazon-web-services - 对于 Terraform,aws_cloudwatch_alarm_metric 资源的有效维度列表在哪里?
- r - 向 geom_hline 添加点
- javascript - JavaScript - 重新加载图像。Mapbox GL JS 静态地图无法加载,错误 404