【发布时间】:2011-07-08 01:24:18
【问题描述】:
我从 GitHub Webhook 服务收到一些数据。它包含有关存储库中最近提交的信息。我收到的字符串如下所示:
payload = {
"ref":"refs/heads/master","commits":[
{"added":[],"author":{"username":"myname","email":"my@email.com","name":"John Doe"},"timestamp":"2011-03-03T02:04:32-08:00","removed":[],"url":"https://github.com/repository/commit/3da6de4ae4550aa84ff16b9b52d8c5179e126dc5","message":"Setup some functions","modified":["apps/api/__init__.py","main.py","utils.py"],"id":"3da6de4ae4550aa84ff16b9b52d8c5179e126dc5"}
],
"compare":"https://github.com/repository/compare/a270fe9...eb26a23","after":"eb26a2312e1955ccb2b7cb50d43682be87c30faa","repository":{"pushed_at":"2011/03/03 02:23:14 -0800","has_issues":true,"has_downloads":true,"url":"https://github.com/repository","watchers":1,"fork":false,"has_wiki":true,"private":false,"size":2248,"forks":1,"created_at":"2011/02/23 04:41:51 -0800","owner":{"email":"my@email.com","name":"john"},"name":"john","language":"Python","open_issues":0},"forced":false,"before":"a270fe954359caa323a9880afc3d6301055ea566"
}
删除该变量payload= 并仅获取{} 之间的数据以便我可以使用simplejson 解析的正确JSON 的正确方法是什么?现在我只使用json_data = json_data.replace('payload=', '')。我不确定这是正确的方法。我怎样才能做得更好?谢谢。
更新 我在 GitHub 支持板上发布了 message 并附有问题描述。
【问题讨论】:
-
也许值得让 Github 返回有效的 JSON - 这可能是他们最后的一个错误。
-
您是如何收到的?它是在 HTTP 响应中还是在 HTTP 帖子中?
-
GitHub 向我在设置中提供的 url 发送一个 POST。它在这里描述:help.github.com/post-receive-hooks。所以,它会在我的问题中发送一个带有字符串的 POST。
-
绝对值得像@Thomas 建议的那样在 github 上窃听,因为该页面上的任何样本中都没有变量的迹象。
-
@Nick:嗯,事实上有。 “这是作为带有单个参数的 POST 发送的:‘payload’”。事实上,它是以 HTTP 形式发送的,所以正确的处理方法是这样处理它——我认为
cgi模块中有一些东西。但这在这里可能有点矫枉过正。
标签: python json google-app-engine