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

时光毁灭记忆、已成空白 提交于 2019-12-08 02:52:08

问题


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:


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


来源:https://stackoverflow.com/questions/34924568/how-to-access-object-decoded-from-json-by-cl-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!