Making JSON requests within Emacs

后端 未结 3 1453
走了就别回头了
走了就别回头了 2020-12-28 17:40

I am in the early stages of writing an Emacs major mode for browsing and contributing to sites on the Stack Exchange network, in much the same way as dired and

3条回答
  •  甜味超标
    2020-12-28 18:29

    This may not be the best way of doing things but it seems to work for me.

    (defun fetch-json (url)
       (with-current-buffer (url-retrieve-synchronously url)
         ; there's probably a better way of stripping the headers
         (search-forward "\n\n")
         (delete-region (point-min) (point))
         (buffer-string)))
    

    Then calling this function with a url will return the content of the response, in this case, json. I've used the reddit api as an example because I'm not sure how the Stack Exchange api works.

     (fetch-json "http://reddit.com/r/emacs.json")
    

    There is pretty much no error checking included here, if the url returns no data then this will blow up.

提交回复
热议问题