Requests json 字符串轉字典

Requests json 字符串轉字典

1,Requests json 字符串轉字典

#!/usr/local/bin/python3
import requests
import json

url = requests.get('https://filscan.io:8700/v0/filscan/BaseInformation')
data = url.text

'''
json.loads(json_str) json字符串轉換成字典
json.dumps(dict) 字典轉換成json字符串 
'''
data_dict = json.loads(data)
print (data)
print (data_dict)
print (data_dict['data']['tipset_height'])
  • print (data)
{"res":{"code":3,"msg":"success"},"data":{"tipset_height":"42631","block_reward":2.2764078900056703,"avg_message_size":561.1569155215564,"avg_gas_price":0.637861831231997,"avg_messages_tipset":11,"pledge_collateral":"0.759290472367509604"}}
  • print (data_dict)
{'res': {'code': 3, 'msg': 'success'}, 'data': {'tipset_height': '42631', 'block_reward': 2.2764078900056703, 'avg_message_size': 561.1569155215564, 'avg_gas_price': 0.637861831231997, 'avg_messages_tipset': 11, 'pledge_collateral': '0.759290472367509604'}}
  • print (data_dict['data']['tipset_height'])
42631

參考:

  1. python str dic json 字符串和文件轉化爲字典
發佈了633 篇原創文章 · 獲贊 164 · 訪問量 109萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章