lisp中的输入/输出json文件

lisp中的输入/输出json文件,json,lisp,common-lisp,Json,Lisp,Common Lisp,各位早上好, 为了完成这个项目,我再次需要你的帮助 现在我尝试创建两个函数来读取/写入lisp中的文件 这是函数必须如何工作的说明 (json-load filename) -> JSON (json-write JSON filename) -> filename The json-load function opens the file filename returns a JSON object (or generates an error). If file

各位早上好,

为了完成这个项目,我再次需要你的帮助

现在我尝试创建两个函数来读取/写入lisp中的文件

这是函数必须如何工作的说明

    (json-load filename) -> JSON

   (json-write JSON filename) -> filename

The json-load function opens the file filename returns a JSON object (or generates an error). If
filename does not exist the function generates an error. The suggestion is to read the whole file in one
string and then to call json-parse.
The json-write function writes the JSON object to the filename file in JSON syntax. If
filename does not exist, it is created and if it exists it is overwritten. Of course it is expected that
CL-PROMPT> (json-load (json-write '(json-obj # | stuff | #) "foo.json"))
(json-obj # | stuff | #)
这是我的json加载函数

(defun json-load (filename)
(with-open-file (file-stream filename)
    (let ((file-contents (make-string (file-length file-stream))))
        (read-sequence file-contents file-stream)
        file-contents)) (json-parse (file-contents)))
但它不起作用

我也需要一些帮助来编写函数

谢谢各位

编辑1:

(defun json-load (filename)
  (with-open-file (in filename
                      :direction :input
                      :if-does-not-exist :error)
    (file-get-contents filename))
  (json-parse filename))

(defun file-get-contents (filename)
  (with-open-file (stream filename)
    (let ((contents (make-string (file-length stream))))
      (read-sequence contents stream)
      contents)))
所以函数应该是正确的,但问题是,我认为,是文件获取内容函数。 我认为,因为如果我运行这个函数,输出是

“\“{\\\\\\\\\\\\\\\\\\”:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

因此json解析不再识别json对象。 有什么想法吗

编辑2:

我尝试了两个函数,但结果相同。如果我在文件中使用相同的json对象调用json解析,这没关系,但是如果我调用json load lisp,则会用我自己的错误消息“undefined json object(json parse)”响应我。 为什么?

编辑3:

这是一个json写函数,但目前它不起作用

    (defun json-write (json filename)
  (with-open-file (out filename
                       :direction :output
                       :if-exists :overwrite
                       :if-does-not-exist :create)
    (pprint (json out))))
因此,文章开头的描述说,json write函数以json语法将json对象写入文件名文件。 现在有两个问题

1) 我的功能部分正确吗

2) 如何用Json语法编写Json对象


谢谢

我在做同一个项目,希望教授们不介意我们分享信息;) 这就是我采取的方法:

(defun json-load (filename)
  (with-open-file (in filename 
                      :direction :input
                      :if-does-not-exist :error)
   (multiple-value-bind (s) (make-string (file-length in))
      (read-sequence s in)
      (json-parse s))))

请记住,
read sequence
会覆盖给定的序列,在本例中是
s
。我使用
多值绑定
只是为了不必使用变量声明或lambda函数(尽管它只是
(let((v形式))…)
,正如@tfb指出的那样)。

多值绑定是一个可接受的函数吗?我们可以用它吗?@GiuseppeGiubaldo来自教授的评论:sì。Potte usare
MULTIPLE-VALUE-BIND
e
VALUES
e tutte le altre macro e funzioni
MULTIPLE-VALUE-*
,con la ovvia esclusione di
MULTIPLE-VALUE-SETQ
。是的,我刚刚读过这个。所以我尝试运行您的代码,但lisp以错误回应我:functor位置的非法参数:“users/giuseppe/downloads/provaLisp2.txt”。你也有同样的错误吗?@GiuseppeGiubaldo在我的函数末尾缺少了一个“)”(刚刚编辑了答案)。检查您的情况是否也是如此。
(多值绑定(v)形式…
(let((v形式))…)
完全相同,只是不太惯用。