gpt4 book ai didi

php - 在 php 中不使用 json_decode 的 JSON pretty-print

转载 作者:行者123 更新时间:2023-12-02 03:47:31 25 4
gpt4 key购买 nike

例如,我有这样的 json 字符串(从一开始)。而且它没有格式化。

{"data":[{"id":"14","memo_kondisi":"Kekurangan pekerjaan","total_row":"5","nilai_temuan":"1.000.000","data_sebab":[{"id":"15","id_sebab":"","id_sub_sebab":"","memo_sebab":"coba","data_rekomendasi":[{"id":"25","id_rekomendasi":"10","id_sub_rekomendasi":"","id_s_sub_rekomendasi":"","nilai_rekomendasi":"0"},{"id":"26","id_rekomendasi":"10","id_sub_rekomendasi":"","id_s_sub_rekomendasi":"","nilai_rekomendasi":"0"},{"id":"31","id_rekomendasi":"10","id_sub_rekomendasi":"","id_s_sub_rekomendasi":"","nilai_rekomendasi":"0"}]},{"id":"16","id_sebab":"","id_sub_sebab":"","memo_sebab":"coba","data_rekomendasi":[{"id":"34","id_rekomendasi":"10","id_sub_rekomendasi":"","id_s_sub_rekomendasi":"","nilai_rekomendasi":"0"},{"id":"35","id_rekomendasi":"10","id_sub_rekomendasi":"","id_s_sub_rekomendasi":"","nilai_rekomendasi":"0"}]}]},{"id":"15","memo_kondisi":"Kekurangan pekerjaan","total_row":"2","nilai_temuan":"1.000.000","data_sebab":[{"id":"5","id_sebab":"","id_sub_sebab":"","memo_sebab":"coba","data_rekomendasi":[]},{"id":"10","id_sebab":"","id_sub_sebab":"","memo_sebab":"coba","data_rekomendasi":[]}]},{"id":"16","memo_kondisi":"","total_row":"2","nilai_temuan":"0","data_sebab":[{"id":"9","id_sebab":"","id_sub_sebab":"","memo_sebab":"coba","data_rekomendasi":[]},{"id":"12","id_sebab":"","id_sub_sebab":"","memo_sebab":"coba","data_rekomendasi":[]}]}]}

我看到一些类似的问题,您必须使用 json_decode 并且我必须再次编码并使用 json_encode($json,JSON_PRETTY_PRINT)

有没有一种方法可以使 json 可读,而无需先解码 JSON,然后在 PHP 中再次编码?

注意:我希望结果仍然是 JSON

最佳答案

不是真的。使用其他人的解析器库不会有任何区别,因为他们也会调用 json_decode()

您可以创建一个可以调用的小函数:

function prettify($json)
{
$array = json_decode($json, true);
$json = json_encode($array, JSON_PRETTY_PRINT);
return $json;
}

然后 echo prettify($jsonString); 会比不断解码和重新编码更容易。看到这里https://3v4l.org/CcJlf

关于php - 在 php 中不使用 json_decode 的 JSON pretty-print ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46322933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com