我有點新的(使用Python 2.7)和Python解析JSON數據。有一個服務,我必須發送API調用,並且JSON響應如下所示。 '行'中的項目數量可能會有所不同。我需要做的是隻取第二行的「內容」,如果有第二行,並將其放入列表中。本質上,它只是一個「活動確認號碼」的列表,沒有別的。如果有幫助的話,這個數字總是隻有9個數字。任何建議將非常感激。蟒蛇+ JSON:解析列出
{"response":
{"result":
{"Potentials":
{"row":
[
{"no":"1","FL":
{"content":"523836000004148171","val":"POTENTIALID"}
},
{"no":"2","FL":
{"content":"523836000004924051","val":"POTENTIALID"}
},
{"no":"3","FL":
[
{"content":"523836000005318448","val":"POTENTIALID"},
{"content":"694275295","val":"Campaign Confirmation Number"}
]
},
{"no":"4","FL":
[
{"content":"523836000005318662","val":"POTENTIALID"},
{"content":"729545274","val":"Campaign Confirmation Number"}
]
},
{"no":"5","FL":
[
{"content":"523836000005318663","val":"POTENTIALID"},
{"content":"903187021","val":"Campaign Confirmation Number"}
]
},
{"no":"6","FL":
{"content":"523836000005322387","val":"POTENTIALID"}
},
{"no":"7","FL":
[
{"content":"523836000005332558","val":"POTENTIALID"},
{"content":"729416761","val":"Campaign Confirmation Number"}
]
}
]
}
},
"uri":"/crm/private/json/Potentials/getSearchRecords"}
}
編輯:輸出用於該實施例的一個例子將是:
confs = [694275295,729545274,903187021,729416761]
或
confs = [ '694275295' ,「729545274」,「903187021」,「729416761」]
真的,如果他們存儲爲字符串或整數
沒關係編輯2:這裏是我的代碼剪斷:
import urllib
import urllib2
import datetime
import json
key = '[removed]'
params = {
'[removed]'
}
final_URL = 'https://[removed]'
data = urllib.urlencode(params)
request = urllib2.Request(final_URL,data)
response = urllib2.urlopen(request)
content = response.read()
j = json.load(content)
confs = []
for no in j["response"]["result"]["Potentials"]["row"]:
data = no["FL"]
if isinstance(data, list) and len(data) > 1:
confs.append(int(data[1]["content"]))
print confs
您可以根據此示例添加所需輸出示例嗎? – martincho
剛剛添加的例子 – crookedleaf