Python 将JSON数据从URL解析到MySQL
我让这段代码将JSON数据从URL发送到MySQL数据库,但是我得到错误“字符串索引必须是整数”Python 将JSON数据从URL解析到MySQL,python,json,Python,Json,我让这段代码将JSON数据从URL发送到MySQL数据库,但是我得到错误“字符串索引必须是整数” import urllib.parse import requests import pymysql mydb = pymysql.connect(host='127.0.0.1', user='test', passwd='test', db='test', local_infile=1) r = requests.get('https://example.com/apidata') cur
import urllib.parse
import requests
import pymysql
mydb = pymysql.connect(host='127.0.0.1', user='test', passwd='test', db='test', local_infile=1)
r = requests.get('https://example.com/apidata')
cursor = mydb.cursor()
json_obj = r.json()
for ord in json_obj["0"]:
print("stripe_token", ord["stripe_token"])
cursor.execute("INSERT INTO apidata (stripe_token) VALUES (%s)", (ord["stripe_token"]))
#close the connection to the database.
mydb.commit()
cursor.close()
我的Json数据如下所示
{
"0": {
"need_session_refresh": "1",
"signup_email_sent": "1",
"stripe_cc_expires": "0000",
"stripe_cc_masked": "123456",
"stripe_token": "1x734g834",
我做错了什么
谢谢:)
编辑-我还想递归地解析它,例如
{
"0": {
"stripe_token": "756474745",
"need_session_refresh": "1",
},
"1": {
"nstripe_token": "34563456",
"need_session_refresh": "1",
},
"_total": 43054
}
问题是,当您遍历这样的字典时:
for ord in json_obj["0"]:
json_obj = r.json()
ord = json_obj["0"]
print("stripe_token", ord["stripe_token"])
cursor.execute("INSERT INTO apidata (stripe_token) VALUES (%s)", (ord["stripe_token"]))
for index in json_obj:
if index != "_total":
cursor.execute("INSERT INTO apidata (stripe_token) VALUES (%s)", (json_obj[index]["stripe_token"]))
ord
实际上获取字典中所有键的值,而不是键值对。这意味着当你这样做时:
ord["stripe_token"]
ord
是字典中的键(即字符串),它抱怨您不能使用其他字符串索引到字符串中
我认为问题在于您假设“0”中的结构是一个列表而不是一个字典。如果执行json_obj[“0”][“stripe_token”]
,您应该会发现您得到了所需的值。您还可以删除json_obj[“0”]:中ord的循环,因为json_obj[“0”]
中实际上没有列表
代码将如下所示:
for ord in json_obj["0"]:
json_obj = r.json()
ord = json_obj["0"]
print("stripe_token", ord["stripe_token"])
cursor.execute("INSERT INTO apidata (stripe_token) VALUES (%s)", (ord["stripe_token"]))
for index in json_obj:
if index != "_total":
cursor.execute("INSERT INTO apidata (stripe_token) VALUES (%s)", (json_obj[index]["stripe_token"]))
编辑:您已经添加了需要访问的json\u值
中还有更多的键,因此需要循环访问这些键。代码如下所示:
for ord in json_obj["0"]:
json_obj = r.json()
ord = json_obj["0"]
print("stripe_token", ord["stripe_token"])
cursor.execute("INSERT INTO apidata (stripe_token) VALUES (%s)", (ord["stripe_token"]))
for index in json_obj:
if index != "_total":
cursor.execute("INSERT INTO apidata (stripe_token) VALUES (%s)", (json_obj[index]["stripe_token"]))
这里,ord
将迭代json_obj[“0”]
的键。您根本不需要循环
print(f"stripe_token: {json_obj['0']['stripe_token']}")
只有当json_obj
包含多个元素时,才需要循环
我建议正确处理json对象与您期望的不一样的情况。Hi Rob,您能告诉我代码中的实际情况吗?因为我现在得到的json没有定义。嘿,我已经编辑了我的原始答案,添加了一个代码示例。非常感谢!如果我有更多stripe_token的thank once实例,我将如何浏览它们。目前,它只是将第一个json对象多次复制到一个新行中。如果您有多个stripe_标记,您如何设想json的结构?目前,您提供的JSON只允许一个stripe_标记值。谢谢Rob,我正在尝试并得到TypeError:“int”对象不是subscriptableThank,但我只得到“JSON_obj=JSON.loads(r)NameError:name“JSON”未定义“为什么需要JSON.loads(r)
?您的代码中有json\u obj=r.json()
,这是请求的方法。Response
对象。。。。