如何将字符串化的 JSON 从无缩进和转义反斜杠转换为精美缩进且无转义字符的 JSON?
我正在使用 Common Lisp、SBCL 和一个名为 Dexador 的著名库。以下 s-exp:
CL-USER> (dex:post "https://html2json.com/api/v1"
:content (dex:get "https://ambrevar.xyz"))
返回:
"{\"error\":false,\"code\":null,\"message\":null,\"data\":{\"head\":{\"title\":\"\\u200e\",\"meta\":[{\"charset\":\"utf-8\"},{\"name\":\"viewport\",\"content\":\"width=device-width, initial-scale=1\"},{\"name\":\"generator\",\"content\":\"Org mode\"},{\"name\":\"author\",\"content\":\"root\"}],\"link\":[{\"rel\":\"stylesheet\",\"type\":\"text\\/css\",\"href\":\"..\\/dark.css\"},{\"rel\":\"icon\",\"type\":\"image\\/x-icon\",\"href\":\"..\\/logo.png\"}],\"script\":[]},\"headers\":[{\"tagName\":\"h2\",\"textContent\":\"Contact\",\"attributes\":{\"id\":\"org4051da4\"}}],\"jsonLd\":[],\"iframes\":[],\"embeds\":[],\"imgs\":[],\"links\":[{\"text\":\"[email protected]\",\"href\":\"mailto:[email protected]\",\"attr\":{\"href\":\"mailto:[email protected]\"}},{\"text\":\"0x9BDCF497A4BBCC7F\",\"href\":\"ambrevar.asc\",\"attr\":{\"href\":\"ambrevar.asc\"}}],\"forms\":[],\"tables\":[],\"content\":\"Contact\\nEmail: [email protected]\\nPGP: 0x9BDCF497A4BBCC7F\"}}"
200
#<HASH-TABLE :TEST EQUAL :COUNT 17 {10039E4303}>
#<QURI.URI.HTTP:URI-HTTPS https://html2json.com/api/v1>
#<CL+SSL::SSL-STREAM for #<FD-STREAM for "socket 172.20.10.5:34050, peer: 185.94.230.235:443" {10038A5B13}>>
第一个结果(双字节字符串化 JSON)接近我想要的结果。
由于 JSON 输出将打印给最终用户,因此我需要 (i) 删除反斜杠 (\
) 和 (ii) 缩进 JSON。
然而,反斜杠的存在是由于它们在引号上具有转义性质(以避免字符串过早结束),我不确定如何处理它们。我也不知道如何漂亮地打印它。
我怎样才能达到预期的结果?有没有一个库可以解决这个特定问题?如果是这样,怎么办?
谢谢
I am using Common Lisp, SBCL, and a famous library called Dexador. The following s-exp:
CL-USER> (dex:post "https://html2json.com/api/v1"
:content (dex:get "https://ambrevar.xyz"))
Returns:
"{\"error\":false,\"code\":null,\"message\":null,\"data\":{\"head\":{\"title\":\"\\u200e\",\"meta\":[{\"charset\":\"utf-8\"},{\"name\":\"viewport\",\"content\":\"width=device-width, initial-scale=1\"},{\"name\":\"generator\",\"content\":\"Org mode\"},{\"name\":\"author\",\"content\":\"root\"}],\"link\":[{\"rel\":\"stylesheet\",\"type\":\"text\\/css\",\"href\":\"..\\/dark.css\"},{\"rel\":\"icon\",\"type\":\"image\\/x-icon\",\"href\":\"..\\/logo.png\"}],\"script\":[]},\"headers\":[{\"tagName\":\"h2\",\"textContent\":\"Contact\",\"attributes\":{\"id\":\"org4051da4\"}}],\"jsonLd\":[],\"iframes\":[],\"embeds\":[],\"imgs\":[],\"links\":[{\"text\":\"[email protected]\",\"href\":\"mailto:[email protected]\",\"attr\":{\"href\":\"mailto:[email protected]\"}},{\"text\":\"0x9BDCF497A4BBCC7F\",\"href\":\"ambrevar.asc\",\"attr\":{\"href\":\"ambrevar.asc\"}}],\"forms\":[],\"tables\":[],\"content\":\"Contact\\nEmail: [email protected]\\nPGP: 0x9BDCF497A4BBCC7F\"}}"
200
#<HASH-TABLE :TEST EQUAL :COUNT 17 {10039E4303}>
#<QURI.URI.HTTP:URI-HTTPS https://html2json.com/api/v1>
#<CL+SSL::SSL-STREAM for #<FD-STREAM for "socket 172.20.10.5:34050, peer: 185.94.230.235:443" {10038A5B13}>>
The first result (of a brig stringfied JSON) is close to what I want.
Since the JSON output is going to be printed to end users, I need (i) to remove backslash (\
) and (ii) indent JSON.
However, the backslashes exist due to the escape nature they have on quotes (to avoid premature closing of the string) and I am not sure how to handle them. I am also clueless on how to pretty print it.
How can I achieve the desired result? Is there a library to solve this specific problem? If so, how?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有!但它不能快速加载。你必须从github下载它。
然而,最方便的设置方法是使用 roswell(它可以安装 github 存储库维护的 Common Lisp 包以及可快速加载的包)。
我曾经写过一篇关于为 Common Lisp 开发环境设置 Roswell 的文章 - 请参阅:https://towardsdatascience.com/how-to-set-up-common-lisp-ide-in-2021-5be70d88975b
。
如果您随后安装了 Roswell,则设置过程非常简单:
您就可以开始了!
jsown:pprint-json
正是您正在搜索的函数!上面的代码输出:
如果没有 Roswell,您必须遵循 jsown-util 的 github 自述文件说明:
基本上,这意味着进入您的
quicklisp
的local-projects
文件夹并 git clone那里有 jsown-utils 的 github 存储库。因为,它对于
ql:quickload
是可见的,它总是先检查其local-projects
文件夹,然后再在官方的 Quicklisp 存储库中搜索在线包。There is! But it is not quickload-able. You have to download it from github.
The most convenient way to set it up however is to use roswell (which can install you github repository maintained Common Lisp packages as well as quickload-able packages).
I wrote an article once about setting up Roswell for a Development environment of Common Lisp - see: https://towardsdatascience.com/how-to-set-up-common-lisp-ide-in-2021-5be70d88975b
.
If you have then installed Roswell, setting up is as straight forward as:
And you are ready to go!
jsown:pprint-json
is exactly the function you are searching for!The code above outputs:
without Roswell, you have to follow jsown-util's github Readme instructions:
Basically it means to go into your
quicklisp
'slocal-projects
folder and git clone the github repository of jsown-utils there.Because, then it is visible for
ql:quickload
which always checks in itslocal-projects
folder first, before it searches for the package in the official quicklisp repo online.