在 Emacs 中发出 JSON 请求 [英] Making JSON requests within Emacs

查看:30
本文介绍了在 Emacs 中发出 JSON 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正处于编写 Emacs 主要模式 的早期阶段,用于浏览和贡献网站在 Stack Exchange 网络上,与 diredlist-packages 的工作方式大致相同,其中一些灵感来自 magit组织模式.

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 list-packages works, with a few inspirations from magit and org-mode.

当然,问题是,我不知道我首先如何将 Emacs 与 SE API (v2.1) 连接起来.我从未在 Elisp 中做过任何涉及网络连接的事情,尽管我对这种语言本身很满意(并且已经多次查看 package.el).

The problem is, of course, I have no idea how I would interface Emacs with the SE API (v2.1) in the first place. I've never done anything that involves a network connection within Elisp, although I'm comfortable with the language itself (and have taken more than a few looks at package.el).

我从未使用过 JSON,尽管我正在学习 W3C 的教程 就可以了.

I've never worked with JSON, although I'm in the middle of W3C's tutorial on it.

一个简单的hello world"就足够了,可能是

A simple 'hello world' would suffice, possibly along the lines of

(execute-json-query "/info")

W3C 教程似乎也没有讨论请求.我必须对此进行自己的研究.
我真的不知道我在做什么;我昨天下午才刚刚开始狂热地工作.

The W3C tutorial doesn't seem to go over requests, either. I'll have to do my own research on that.
I really don't have any idea what I'm doing; I've only just started feverishly working on this yesterday afternoon.

推荐答案

其他答案的问题在于 Stack Exchange API 是GZIP'd 和 Emacs 附带的 url.el 不会自动解压缩它.

The problem with other answers is that Stack Exchange API is GZIP'd and url.el shipped with Emacs does not automatically decompress it.

看看我的 request.el 库,它支持自动解压(说实话,我只是添加了支持).以下是获取 stackoverflow 中最活跃问题的示例:

Take a look at my request.el library which supports automatic decompression (to be honest, I just added the support). Here is an example to fetch the most active question in stackoverflow:

(request
 "https://api.stackexchange.com/2.1/questions"
 :params '((order . "desc")
           (sort . "activity")
           (site . "stackoverflow"))
 :parser 'json-read
 :success (function*
           (lambda (&key data &allow-other-keys)
             (let* ((item (elt (assoc-default 'items data) 0))
                    (title (assoc-default 'title item))
                    (tags (assoc-default 'tags item)))
               (message "%s %S" title tags)))))

request.el 有据可查,带有 可执行示例 并且经过良好测试.

request.el is well documented, comes with executable examples and is well tested.

这篇关于在 Emacs 中发出 JSON 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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