添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
飞翔的甜瓜  ·  08 | ...·  6 月前    · 
严肃的苦咖啡  ·  Copy data to and from ...·  1 年前    · 

python获取json中的某个值

在 Python 中读取 JSON 数据的方法有很多,你可以使用内置的 json 库来解析它。

首先,将 JSON 数据加载到 Python 字典中:

import json
# JSON string
json_string = '{"key1": "value1", "key2": "value2"}'
# Parse JSON string into Python dictionary
data = json.loads(json_string)
# Access values in the dictionary
value1 = data["key1"]
value2 = data["key2"]
print(value1) # Output: value1
print(value2) # Output: value2

如果 JSON 数据存储在文件中,你可以使用 json.load 读取它:

import json
# Read JSON data from file
with open("data.json", "r") as file:
    data = json.load(file)
# Access values in the dictionary
value1 = data["key1"]
value2 = data["key2"]
print(value1) # Output: value1
print(value2) # Output: value2