d={
"state": "ok",
"errmsg": "",
"data": {
"id": 1,
"username": '李元霸'
}
}
}
}
for i in d['data']:
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='123456', db='test', charset='utf8')
cursor = conn.cursor()
tsql = "insert into test1(id,name)values(%s,'%s')" % (i['id'],i['username'])
print(tsql)
cursor.execute(tsql)
conn.commit()
conn.close()
I can handle this kind of deposit, but I don’t know how to add the key.
How about the following 2017-01-02": {
、"16777216": {
These two keys are stored in mysql
{
"state": "ok",
"errmsg": "",
"data": {
"2017-01-02": {
"16777216": {
"id": 1,
"username": '李元霸'
}
},
"2017-01-06": {
"16777456": {
"id": 2,
"username": '陈坤'
}
},
}
}
Dictionary stored in mysql
I have used it
a={xxxx}
Do you want this?
Just use a loop to take out the data you need and insert it into the database
The following code can only handle the simplest case
Besides, the
json
you gave doesn’t seem to be very correct. .run code