无法从JSON响应中获取字符串项目

时间:2019-12-13 20:41:26

标签: python json

我当前正在获取数据以获取API端点,并已成功获取它。但是,我有一个错误,该文件不是字符串。我试图使其像str()XXX.replace("(", "")。它不允许我将商品作为string

res = response.json()
## {'timestamp': 1576269528552,'item': {'id': 'XXXXXXXXXX', 'name': 'XXXXXXXXX' }}

uid = res['item']['id'],
name = res['item']['name']
## uid = ('XXXXXXXXXXXXX',)
## name = ('XXXXXXXXXXXXX',)

我要从中摆脱(),

2 个答案:

答案 0 :(得分:3)

使用res代替request

res = response.json()
## {'timestamp': 1576269528552,'item': {'id': 'XXXXXXXXXX', 'name': 'XXXXXXXXX' }}

uid = res['item']['id'],
name = res['item']['name']
## uid = 'XXXXXXXXXX'
## name = 'XXXXXXXXX'

使用大熊猫,我也可以提取它们:

import pandas as pd
a = pd.DataFrame({'timestamp': 1576269528552,'item': {'id': 'XXXXXXXXXX', 'name': 'XXXXXXXXX' }})
a['item']['id']
# 'XXXXXXXXXX'

a['item']['name']                                                                                                                                                                   
# 'XXXXXXXXX'

答案 1 :(得分:0)

对不起,我弄错了。

来自:

uid = res['item']['id'],
name = res['item']['name']

收件人:

uid = res['item']['id']
name = res['item']['name']

摆脱,