亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Convert python json to dictionary and store it in mysql
代言
代言 2017-06-22 11:51:32
0
3
1191
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

along with the fields id and username.
{
    "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

代言
代言

reply all(3)
習慣沉默

a={xxxx}

Do you want this?

小葫蘆
d = {
    "state": "ok",
    "errmsg": "",
    "data": {
        "2017-01-02": {
            "16777216": {
               "id": 1,
                "username": '李元霸'
            }
        },
        "2017-01-06": {
            "16777456": {
                "id": 2,
                "username": '陳坤'
            }
        },
    }
}

for k, v in d['data'].iteritems():
    number = v.keys()[0]
    print k, number, v[number]['id'], v[number]['username']
學習ing

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

#*--encoding: utf8--*

import json
    
string = '''{
    "state": "ok",
    "errmsg": "",
    "data": {
        "2017-01-02": {
            "16777216": {
               "id": 1,
                "username": "李元霸"
            }
        },
        "2017-01-06": {
            "16777456": {
                "id": 2,
                "username": "陳坤"
            }
        }
    }
}'''

ret  = json.loads(string)
data = ret['data']

for date in data.keys():
    arr = []
    arr.append(date)
    info = data[date]
    arr.append(info.keys()[0])
    arr.append(info.values()[0]['id'])
    arr.append(info.values()[0]['username'])
    print(arr)

Besides, the json you gave doesn’t seem to be very correct. .
run code

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template