以下是一个使用 Python 来处理这种 JSON 数据结构的示例代码:
python
import json
# 假设的 JSON 数据
json_data = '''
{
"物理码 1": [
{"时间": "2024-06-20 10:00:00", "状态": "正常", "rtsp 地址": "rtsp://example.com/stream1"},
{"时间": "2024-06-20 11:00:00", "状态": "异常", "rtsp 地址": "rtsp://example.com/stream2"}
],
"物理码 2": [
{"时间": "2024-06-20 12:00:00", "状态": "正常", "rtsp 地址": "rtsp://example.com/stream3"},
{"时间": "2024-06-20 13:00:00", "状态": "正常", "rtsp 地址": "rtsp://example.com/stream4"}
]
}
'''
# 解析 JSON 数据
data = json.loads(json_data)
# 打印解析后的数据
for physical_code, items in data.items():
print(f"物理码: {physical_code}")
for item in items:
print(f" 时间: {item['时间']}, 状态: {item['状态']}, RTSP 地址: {item['rtsp 地址']}")
在上述代码中,首先定义了一个包含您所描述结构的 JSON 数据字符串。然后使用 json.loads()
函数将其解析为 Python 数据结构。最后通过遍历打印出每个物理码及其对应的数组项中的详细信息。