Python 代码提取 JSON 数据中 result 的数值

本文将介绍如何使用 Python 代码提取 JSON 数据中 result 字段的数值。

假设你有一个 JSON 数据,其中包含一个名为 'results' 的字段,该字段存储了一个列表,列表中包含多个元素,每个元素代表一个结果,例如:

{
  "error": false,
  "consumed_fpoint": 0,
  "required_fpoints": 0,
  "size": 1744103,
  "page": 1,
  "mode": "extended",
  "query": "header='thinkphp' || header='think_template'",
  "results": [
    ["m3p1d.xyz", "192.46.231.75", "80"],
    ["njcjsg.com", "47.90.42.11", "80"],
    ["fangben.top", "172.96.185.195", "80"],
    ["e-joys.com", "108.166.217.51", "80"],
    ["zgqliang.com", "154.85.62.151", "80"],
    ["525201i.com", "154.83.25.245", "80"],
    ["525209b.com", "154.83.25.245", "80"],
    ["https://jjzwjx.com", "43.224.154.90", "443"],
    ["https://lipin1688.vip", "43.155.106.153", "443"],
    ["https://bt988.net", "104.21.43.113", "443"]
  ]
}

你需要提取 results 列表中每个元素的数值并打印出来。

以下代码示例展示了如何实现这一操作:

import json

data = '{"error":false,"consumed_fpoint":0,"required_fpoints":0,"size":1744103,"page":1,"mode":"extended","query":"header='thinkphp' || header='think_template'","results":[["m3p1d.xyz","192.46.231.75","80"],["njcjsg.com","47.90.42.11","80"],["fangben.top","172.96.185.195","80"],["e-joys.com","108.166.217.51","80"],["zgqliang.com","154.85.62.151","80"],["525201i.com","154.83.25.245","80"],["525209b.com","154.83.25.245","80"],["https://jjzwjx.com","43.224.154.90","443"],["https://lipin1688.vip","43.155.106.153","443"],["https://bt988.net","104.21.43.113","443"]]}'

parsed_data = json.loads(data)

results = parsed_data['results']

for result in results:
    print(result)

代码解释:

  1. 首先,我们使用 json.loads() 函数将 JSON 字符串转换为 Python 字典。
  2. 然后,我们从字典中提取 results 字段,并将其赋值给 results 变量。
  3. 最后,我们使用循环遍历 results 列表,并使用 print() 函数打印每个元素。

运行代码后,将输出以下结果:

['m3p1d.xyz', '192.46.231.75', '80']
['njcjsg.com', '47.90.42.11', '80']
['fangben.top', '172.96.185.195', '80']
['e-joys.com', '108.166.217.51', '80']
['zgqliang.com', '154.85.62.151', '80']
['525201i.com', '154.83.25.245', '80']
['525209b.com', '154.83.25.245', '80']
['https://jjzwjx.com', '43.224.154.90', '443']
['https://lipin1688.vip', '43.155.106.153', '443']
['https://bt988.net', '104.21.43.113', '443']

希望本文能够帮助你理解如何使用 Python 代码提取 JSON 数据中 result 字段的数值。

Python 代码提取 JSON 数据中 result 的数值

原文地址: https://www.cveoy.top/t/topic/oc0F 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录