如何将 stringfied JSON 从无缩进和转义反斜杠转换为漂亮的缩进且无转义字符 JSON?

How to convert a stringfied JSON from a no-indentation and escape backslashes to a beautifully indented and free of escape characters JSON?

我正在使用 Common Lisp、SBCL 和一个名为 Dexador 的著名库。以下 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\":\"mail@ambrevar.xyz\",\"href\":\"mailto:mail@ambrevar.xyz\",\"attr\":{\"href\":\"mailto:mail@ambrevar.xyz\"}},{\"text\":\"0x9BDCF497A4BBCC7F\",\"href\":\"ambrevar.asc\",\"attr\":{\"href\":\"ambrevar.asc\"}}],\"forms\":[],\"tables\":[],\"content\":\"Contact\nEmail: mail@ambrevar.xyz\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}>>

第一个结果(一个 brig stringfied JSON)接近我想要的结果。

由于 JSON 输出将打印给最终用户,我需要 (i) 删除反斜杠 (\) 和 (ii) 缩进 JSON.

但是,由于 escape 反斜杠在引号上的性质(以避免过早关闭字符串)而存在,我不确定如何处理它们。我也不知道如何漂亮地打印它。

怎样才能达到预期的效果?是否有图书馆可以解决这个特定问题?如果可以,怎么做?

谢谢

有!但它不是quickload-able。您必须从 github 下载它。 然而,最方便的设置方法是使用 roswell(它可以安装 github 存储库维护的 Common Lisp 包以及 quickload-able 包)。

我曾经写过一篇关于为 Common Lisp 开发环境设置 Roswell 的文章 - 请参阅:https://towardsdatascience.com/how-to-set-up-common-lisp-ide-in-2021-5be70d88975b .

如果您随后安装了 Roswell,则设置非常简单:

$ ros install muyinliu/jsown-utils # installs from github directly
$ ros install dexador              # installs from quicklisp repo

你准备好了!

(ql:quickload 'jsown-utils)
(ql:quickload 'dexador)

;; https://github.com/muyinliu/jsown-utils

(jsown:pprint-json
 (dex:post "https://html2json.com/api/v1"
           :content (dex:get "https://ambrevar.xyz")))

jsown:pprint-json正是您要搜索的函数!

上面的代码输出:

{
    "error": [],
    "code": [],
    "message": [],
    "data": {
        "head": {
            "title": "‎",
            "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": "mail@ambrevar.xyz",
                "href": "mailto:mail@ambrevar.xyz",
                "attr": {
                    "href": "mailto:mail@ambrevar.xyz"
                }
            },
            {
                "text": "0x9BDCF497A4BBCC7F",
                "href": "ambrevar.asc",
                "attr": {
                    "href": "ambrevar.asc"
                }
            }
        ],
        "forms": [],
        "tables": [],
        "content": "Contact
Email: mail@ambrevar.xyz
PGP: 0x9BDCF497A4BBCC7F"
    }
}

没有 Roswell,您必须遵循 jsown-util 的 github 自述文件说明:

# In shell I would do:

cd ~/quicklisp/local-projects # enter your quicklisp/local-projects
# and then git clone it:
git clone https://github.com/muyinliu/jsown-utils.git


# Then in Common Lisp to install the package using quicklisp:

(ql:quickload 'jsown-utils)

基本上,这意味着进入您的 quicklisplocal-projects 文件夹,然后 git 克隆 jsown-util 的 git 集线器存储库。 因为,然后它对 ql:quickload 可见,它总是先检查其 local-projects 文件夹,然后再在官方 quicklisp 存储库在线搜索包。