我一直在研究這段代碼幾個小時,試圖通過提供的json數據迭代一堆東西。可以弄清楚如何正確迭代這些嵌套列表和對象。通過json數據與Python進行交互
import json
data = """
{
"tracks": "1",
"timeline": {
"0.733251541": [
{
"id": 1,
"bounds": {
"Width": 0.5099463905313426,
"Height": 0.2867199993133546,
"Y": 0.4436400003433228,
"X": 0.4876505160745349
}
}
],
"0.965": [
{
"id": 1,
"bounds": {
"Width": 0.4205311330135182,
"Height": 0.2363199994340539,
"Y": 0.2393400002829731,
"X": 0.1593787633901481
}
}
],
"1.098224": [
{
"id": 1,
"bounds": {
"Width": 0.4568560813801344,
"Height": 0.2564799993857742,
"Y": 0.1992600003071129,
"X": 0.1000513407532317
}
}
]
},
"taggedTracks": {
"1": "dirk"
}
}
"""
json = json.loads(data)
for a in json["timeline"]:
for b in a:
for c in b["bounds"]:
print a, c["Width"], c["Height"], c["Y"], c["X"]
有人可以引導我在正確的方向如何處理提供的JSON數據?
我收到以下錯誤消息。
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
TypeError: string indices must be integers