gpt4 book ai didi

python - 使用 Python 解析 JSON 数据

转载 作者:行者123 更新时间:2023-11-28 20:53:19 25 4
gpt4 key购买 nike

我从 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=', '') .我不确定这是执行此操作的正确方法。我怎样才能做得更好?谢谢。

更新我发布了 message在带有问题描述的 GitHub 支持板上。

最佳答案

我会寻找第一个 { 字符:

jsondata = jsondata[jsondata.index("{"):]

关于python - 使用 Python 解析 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5181147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com