5

我正在使用 json.el 和 furl.el 从 toggl.com 的 API 获取时间条目 - 不幸的是,我认为它不会将响应分成多个列表:

JSON 响应(数据中的两个条目):

{"data":[{"id":50439783,"description":"bar","billable":false,"start":"2012-10-20T18:07:00Z","stop":"2012-10-20T22:39:00Z","duration":16320,"workspace":{"id":172049,"name":"Dan's workspace"},"tag_names":[],"ignore_start_and_stop":false,"updated_at":"2012-10-20T22:40:26Z","user_id":231062},{"id":50440682,"description":"Test Time Entry","billable":false,"start":"2012-10-20T23:40:16Z","stop":"2012-10-20T23:50:16Z","duration":600,"workspace":{"id":172049,"name":"Dan's workspace"},"tag_names":[],"ignore_start_and_stop":false,"updated_at":"2012-10-20T23:51:32Z","user_id":231062}],"related_data_updated_at":"2012-10-19T00:00:00Z"}

json-read-from-string 响应:

(related_data_updated_at . 2012-10-19T00:00:00Z)                                                                                                                          
(data . [((user_id . 231062) (updated_at . 2012-10-20T22:40:26Z) (ignore_start_and_stop . :json-false) (tag_names . []) (workspace ... ...) (duration . 16320) (stop . 20\
12-10-20T22:39:00Z) (start . 2012-10-20T18:07:00Z) (billable . :json-false) (description . bar) (id . 50439783)) ((user_id . 231062) (updated_at . 2012-10-20T23:51:32Z) \
(ignore_start_and_stop . :json-false) (tag_names . []) (workspace ... ...) (duration . 600) (stop . 2012-10-20T23:50:16Z) (start . 2012-10-20T23:40:16Z) (billable . :jso\
n-false) (description . Test Time Entry) (id . 50440682))])

因此,如果该列表被认为是多个条目,我该如何访问第一个?car 显示related_data_updated_at 和关键数据。cdr 显示related_data_updated_at 的值,以及其他所有内容。

我认为 elisp 不会有太大帮助,但这里只是以防万一:

(defun toggl-display-time-entries(json-string)                                                                                                                            
  (with-current-buffer (get-buffer-create "Toggl")                                                                                                                        
    (erase-buffer)                                                                                                                                                        
    (let ((inhibit-read-only t)                                                                                                                                           
          (time-entries (json-read-from-string json-string)))                                                                                                             
      (dolist (time-entry time-entries)                                                                                                                                   
        (newline)                                                                                                                                                         
        (insert (format "%s" (cdr time-entry)))))))
4

1 回答 1

5

您已经很接近了,但您需要提取 ' 数据,然后遍历数组,而不是列表。例如,这会打印数组中每个项目的用户 ID:

(let ((array-of-times
       (cdr 
    (assoc 'data
           (json-read-from-string json-string)))))
  (let ((c (length array-of-times)))
    (dotimes (n c)
      (insert (format "user_id %s\n" (cdr (assoc 'user_id (elt array-of-times n))))))))
于 2012-10-21T03:47:02.803 回答