在 Python 中,您可以使用内置的 json
模块来解码 JSON 数据。以下是一个简单的示例:
import json
# JSON 字符串
json_string = '{"name": "John", "age": 30, "city": "New York"}'
# 解码 JSON 数据
decoded_data = json.loads(json_string)
# 访问解码后的数据
print(decoded_data["name"]) # 输出: John
print(decoded_data["age"]) # 输出: 30
print(decodedData["city"]) # 输出: New York
如果您需要解码 JSON 数据流,可以使用 json.JSONDecoder
类。以下是一个示例:
import json
# JSON 数据流
json_stream = '{"name": "John", "age": 30, "city": "New York"}'
# 创建 JSON 解码器
decoder = json.JSONDecoder()
# 解码 JSON 数据流
decoded_data = decoder.decode(json_stream)
# 访问解 decode后的数据
print(decoded_data["name"]) # 输出: John
print(decoded_data["age"]) # 输出: 30
print(decoded_data["city"]) # 输出: New York
领取专属 10元无门槛券
手把手带您无忧上云