为什么我可以使用CL-JSON库中的通用LISP中将A列表转换为JSON?
我正在使用钢库普通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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该在
输入from-user
中使用单个报价。您只需要在评估文字列表时就引用文字列表,但是当您使用读取
时,不会进行评估。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 useread-from-string
.