如何访问cl-json从JSON解码的对象? [英] How to access object decoded from JSON by cl-json?

查看:99
本文介绍了如何访问cl-json从JSON解码的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Common Lisp中获取JSON导入.我想出了如何从JSON字符串中解码对象,但是我不知道如何访问返回的对象的属性.要解码一个字符串(并将结果存储在*** tempjson **中),我这样做:

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

如何访问 * tempjson * 数据.例如,如何获取 foo 属性的值?

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

推荐答案

decode-json似乎返回一个关联列表(至少在这种情况下;请参见 assoc :

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

这篇关于如何访问cl-json从JSON解码的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆