如何将字符串化的 JSON 从无缩进和转义反斜杠转换为精美缩进且无转义字符的 JSON?

发布于 2025-01-16 15:42:57 字数 2305 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌辣 2025-01-23 15:42:57

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

我曾经写过一篇关于为 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": "[email protected]",
                "href": "mailto:[email protected]",
                "attr": {
                    "href": "mailto:[email protected]"
                }
            },
            {
                "text": "0x9BDCF497A4BBCC7F",
                "href": "ambrevar.asc",
                "attr": {
                    "href": "ambrevar.asc"
                }
            }
        ],
        "forms": [],
        "tables": [],
        "content": "Contact
Email: [email protected]
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 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:

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

And you are ready to go!

(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 is exactly the function you are searching for!

The code above outputs:

{
    "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": "[email protected]",
                "href": "mailto:[email protected]",
                "attr": {
                    "href": "mailto:[email protected]"
                }
            },
            {
                "text": "0x9BDCF497A4BBCC7F",
                "href": "ambrevar.asc",
                "attr": {
                    "href": "ambrevar.asc"
                }
            }
        ],
        "forms": [],
        "tables": [],
        "content": "Contact
Email: [email protected]
PGP: 0x9BDCF497A4BBCC7F"
    }
}

without Roswell, you have to follow jsown-util's github Readme instructions:

# 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)

Basically it means to go into your quicklisp's local-projects folder and git clone the github repository of jsown-utils there.
Because, then it is visible for ql:quickload which always checks in its local-projects folder first, before it searches for the package in the official quicklisp repo online.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文