How to access object decoded from JSON by cl-json?

2019-05-26 11:46发布

I am trying to get JSON import in Common Lisp. I figured out how to decode an object from a JSON string, but I don't know how to access the properties of the object that's returned. To decode a string (and store the result in ***tempjson**), I do this:

(defun test-json ()
 (with-input-from-string
   (s "{\"foo\": [1, 2, 3], \"bar\": true, \"baz\": \"!\"}")
    (defparameter *tempjson* (json:decode-json s))))

How can I access *tempjson* data. For example, how can I get the value of the foo property?

1条回答
Lonely孤独者°
2楼-- · 2019-05-26 12:13

decode-json appears to return an association list (at least in this case; see documentation). You can access the values with the function assoc:

(defun test-json ()
  (with-input-from-string (s "{\"foo\": [1, 2, 3], \"bar\": true, \"baz\": \"!\"}")
    (let ((data (json:decode-json s)))
      (format t "~a~%" (rest (assoc :foo data))))))
查看更多
登录 后发表回答