如何使用python在JSON中提取特定的多个值? [英] How to extract specific multiple values in JSON using python?

查看:42
本文介绍了如何使用python在JSON中提取特定的多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可能,我必须提取特定的多个值并在文件中打印这些特定值.

I Have to extract specific multiple values and print those specific values in a file if possible.

我尝试了下面的代码来做到这一点

I tried the below code to do this

JSON value from URL is: 
{'data': [{'value': '0.0.0.0'}, {'value': '0.0.0.1'}, {'value': '0.0.0.2'}]}

import requests
import json
url = 'https://www.example.com'
response = requests.get('url', headers=headers , verify=False)  
json_data = json.loads(response.text)
value = json_data['data'][0]['value']
print (value)

output of this : 0.0.0.0

但我想在文件(.txt)中打印所有这些值,如下所示:

But i want to print in a file(.txt) all these values like below:

0.0.0.0
0.0.0.1
0.0.0.3

请帮我解决这个问题.

推荐答案

你想要的是一个循环

json_data = {'data': [{'value': '0.0.0.0'}, {'value': '0.0.0.1'}, {'value': '0.0.0.2'}]}

for x in json_data['data']:
  print (x['value'])

这篇关于如何使用python在JSON中提取特定的多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆