python3处理json数据

时间:2022-06-11 06:45:22

 获取actuator的值

[root@mongo_rs1 tmp]# cat test.py 
import requests
import json

url = 'http://wxtest.mayocase.com:8888/actuator/metrics/tomcat.sessions.active.max'
payload = {'some': 'data'}
headers = {'content-type': 'application/json'}
ret = requests.get(url, data=json.dumps(payload), headers=headers)
print(ret.text)
json_data=json.loads(ret.text)

value=json_data.get('measurements')[0].get('value')
measurements=json_data.get('measurements')
#strs=measurements[0]
#value=strs.get('value')

print(value)
print(value-1.0)

#print(ret.cookies)
[root@mongo_rs1 tmp]# 
[root@mongo_rs1 tmp]# python3 test.py 
{"name":"tomcat.sessions.active.max","measurements":[{"statistic":"VALUE","value":1.0}],"availableTags":[]}
1.0
0.0

 

输出json的值

[root@mongo_rs1 tmp]# cat te.py 
import json

json_data = """{
   "favourite":{
      "bkmrk":{
         "id1490843709594066":{
            "guid":"904eff52277f403b89f6410fe2758646.11",
            "lcate":"1"
         },
         "id1490843712805183":{
            "guid":"58457f60eca64025bc43a978f9c98345.16",
            "lcate":"2"
         },
         "id149084371467327":{
            "guid":"a0f907f9dc8b40f689b083f3eba7228b.16",
            "lcate":"3"
         },
         "id1490843716295393":{
            "guid":"eb75d929455e468bb712e7bc2025d11a.16",
            "lcate":"4"
         }
      }
   }
}"""

data = json.loads(json_data)
for v in data['favourite']['bkmrk'].values():
    print("%s;%s" % (v['lcate'],  v['guid']))
[root@mongo_rs1 tmp]# 
[root@mongo_rs1 tmp]# python3 te.py 
1;904eff52277f403b89f6410fe2758646.11
2;58457f60eca64025bc43a978f9c98345.16
3;a0f907f9dc8b40f689b083f3eba7228b.16
4;eb75d929455e468bb712e7bc2025d11a.16
[root@mongo_rs1 tmp]#