Python从请求打印json数据

2024-10-01 00:33:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在想为什么它不打印我需要的json内容。有人知道我做错了什么吗?在

这是字典

> "listinginfo": {
>     "438309609514180554": {
>       "listingid": "438309609514180554",
>       "price": 35,
>       "fee": 4,
>       "publisher_fee_app": 730,
>       "publisher_fee_percent": "0.10000000149011612",
>       "currencyid": "2003",
>       "steam_fee": 1,
>       "publisher_fee": 3,
>       "converted_price": 50,
>       "converted_fee": 7,
>       "converted_currencyid": "2020",
>       "converted_steam_fee": 2,
>       "converted_publisher_fee": 5,
>       "converted_price_per_unit": 50,
>       "converted_fee_per_unit": 7,
>       "converted_steam_fee_per_unit": 2,
>       "converted_publisher_fee_per_unit": 5,
>       "asset": {
>         "currency": 0,
>         "appid": 730,
>         "contextid": "2",
>         "id": "1579403640",
>         "amount": "1",
>         "market_actions": [
>           {

代码+我需要我要打印的键的值:

^{pr2}$

谢谢你抽出时间。在


Tags: jsonapp内容字典unitpricepublishersteam
2条回答

您可以使用requests.Response.json方法获取解析后的JSON:

r = requests.get(url, headers=headers)
listingInfoJson = r.json()['listinginfo']

我运行了您的代码,看起来listingInfoJson返回时是dict而不是list。所以当你在它上面迭代时,它只是拉动键。在

您正在对unicode对象调用.get方法,这将给您一个AttributeError。您可以用不同的方式运行此代码:

for listingdata in listingInfoJson: 
    print listingInfoJson[listingdata].get('listingid')
    print listingInfoJson[listingdata].get('subTotal') 
    print listingInfoJson[listingdata].get('feeAmount')
    print listingInfoJson[listingdata].get('totalPrice')

或者更好的方式(编辑以供评论):

^{pr2}$

我还要检查你的键值,listingId应该是listingid

相关问题 更多 >