通过pyhton从原始的字典中提取并只保留“body“字段内的“data“和“records“部分

原始数据:


{'requestId': '26880.288', 'url': '', 'type': 'XHR', 'status': 200, 'headers': {'Accept-Ranges': 'bytes', 'Access-Control-Allow-Origin': 'https://www.chinamoney.com.cn', 'Ali-Cdn-Real-IP': '221.232.120.59', 'Connection': 'keep-alive', 'Content-Length': '1085', 'Content-Security-Policy': "default-src * 'unsafe-eval' 'unsafe-inline';", 'Content-Type': 'application/json', 'Date': 'Fri, 20 Sep 2024 06:06:19 GMT', 'ETag': '"43d-622845ff0aa86"', 'Last-Modified': 'Fri, 20 Sep 2024 03:10:00 GMT', 'Server': 'nginx', 'Via': '1.1 ID-7203207600254104 uproxy-6', 'X-Cache-Status': 'MISS from KS-CLOUD-HA-MP-11-02, MISS from KS-CLOUD-CD-CT-46-06', 'X-Cdn-Request-ID': '9e1fd701dc48c560a60357c8c625e83f', 'X-Content-Type-Options': ': nosniff', 'X-Frame-Options': 'ALLOW-FROM https://www.chinamoney.com.cn', 'X-XSS-Protection': '1; mode=block', 'x-link-via': 'cdct46:443;hamp11:443;'}, 'body': '{"head":{"version":"2.0","provider":"CWAP","req_code":"","rep_code":"200","rep_message":"","ts":1726801800168,"producer":""},"data":{"showDateEN":"20/09/2024 11:00","showDateCN":"2024-09-20 11:00"},"records":[{"termCode":"O/N","shibor":"1.9320","shibIdUpDown":"5.30","shibIdUpDownNum":5.3000000000,"termCodePath":"O/N"},{"termCode":"1W","shibor":"1.9290","shibIdUpDown":"1.80","shibIdUpDownNum":-1.8000000000,"termCodePath":"1W"},]}', 'base64Encoded': False}
body_json = json.loads(data['body'])
Log.Error(self,body_json)
# 从body_json中提取data和records部分  
extracted_data = {  
    'data': body_json['data'],  
    'records': body_json['records']  
}  
Log.Error(self, body_json['records'])

try:
    json_data= json.dumps(body_json['records'], indent=4)  
    Log.Error(self,f"Valid JSON: {json_data}")
  
except json.JSONDecodeError:
    Log.Error(self,"Invalid JSON")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值