我有一個smaple.json如下:如何解析包含列表中的列表在python中的部分內的json文件?
{"Detail":[
{
"DocType":"txt",
"Name":"hello.txt",
}
]}
我需要有aginst「名稱」字段的值。我想在我的腳本如下:
file="c:/sample.json"
for list in file:
if (str(list['Detail'])=='None'):
print("Do nothing")
else:
ana = list['Detail']
val = (str(ana)[1:-1])
print val
print val['Name']
,我也得到輸出:
{"DocType":"txt","Name":"hello.txt"}
error:print (ana['Name'])
TypeError: string indices must be integers
那我做錯了應如何我得到的「名稱」字段的細節。
你正在處理一個JSON對象爲一個字符串,它是不是一個好的做法訪問它。嘗試將其作爲字典處理,然後訪問您想要的元素。這看起來也很容易。 –