为什么我可以使用CL-JSON库中的通用LISP中将A列表转换为JSON?

发布于 2025-02-04 04:06:05 字数 1342 浏览 2 评论 0原文

我正在使用钢库普通LISP(SBCL),emacs,slime和一个称为 cl-json

我可以做:

CL-USER> (ql:quickload :cl-json)
To load "cl-json":
  Load 1 ASDF system:
    cl-json
; Loading "cl-json"

CL-USER> (json:encode-json-alist-to-string '(("name" .  "Pedro")))
"{\"name\":\"Pedro\"}"

好。现在,假设用户通过接口输入此ALIST。用户输入alist后,系统将其读取为字符串,因此:

CL-USER> (defvar input-from-user "'((\"name\" . \"John\"))")
INPUT-FROM-USER

CL-USER> input-from-user
"'((\"name\" . \"John\"))"

现在,我尝试:

CL-USER> (read-from-string input-from-user)
'(("name" . "John"))
20

CL-USER> (nth-value 0 (read-from-string input-from-user))
'(("name" . "John"))

CL-USER> (json:encode-json-alist-to-string
             (nth-value 0 (read-from-string input-from-user )))

; Evaluation aborted on #<JSON:UNENCODABLE-VALUE-ERROR expected-type: T datum: '(("name" . "John"))>.

出于某种原因,粘液引发错误:

Value '(("name" . "John")) is not of a type which can be encoded by ENCODE-JSON-ALIST.
   [Condition of type JSON:UNENCODABLE-VALUE-ERROR]

为什么会发生这种情况?我该如何解决?

I am using Steel Bank Common Lisp (SBCL), Emacs, Slime, and a library called CL-JSON.

I can do:

CL-USER> (ql:quickload :cl-json)
To load "cl-json":
  Load 1 ASDF system:
    cl-json
; Loading "cl-json"

CL-USER> (json:encode-json-alist-to-string '(("name" .  "Pedro")))
"{\"name\":\"Pedro\"}"

Ok. Now, suppose this alist is being inputed by the user via an interface. After the user inputs the alist, it is read by the system as a string, thus:

CL-USER> (defvar input-from-user "'((\"name\" . \"John\"))")
INPUT-FROM-USER

CL-USER> input-from-user
"'((\"name\" . \"John\"))"

Now, I try:

CL-USER> (read-from-string input-from-user)
'(("name" . "John"))
20

CL-USER> (nth-value 0 (read-from-string input-from-user))
'(("name" . "John"))

CL-USER> (json:encode-json-alist-to-string
             (nth-value 0 (read-from-string input-from-user )))

; Evaluation aborted on #<JSON:UNENCODABLE-VALUE-ERROR expected-type: T datum: '(("name" . "John"))>.

For some reason, Slime throws the error:

Value '(("name" . "John")) is not of a type which can be encoded by ENCODE-JSON-ALIST.
   [Condition of type JSON:UNENCODABLE-VALUE-ERROR]

Why is this happening? How can I solve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

残龙傲雪 2025-02-11 04:06:05

您不应该在输入from-user中使用单个报价。您只需要在评估文字列表时就引用文字列表,但是当您使用读取时,不会进行评估。

* (defvar input-from-user "((\"name\" . \"John\"))")
INPUT-FROM-USER
* (read-from-string input-from-user)
(("name" . "John"))
19
* (nth-value 0 (read-from-string input-from-user))
(("name" . "John"))

You shouldn't have the single quote in input-from-user. You only need to quote a literal list when it's going to be evaluated, but no evaluation goes on when you use read-from-string.

* (defvar input-from-user "((\"name\" . \"John\"))")
INPUT-FROM-USER
* (read-from-string input-from-user)
(("name" . "John"))
19
* (nth-value 0 (read-from-string input-from-user))
(("name" . "John"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文