[英]python parsing json response
我是JSON的新手,正在嘗試了解如何解析JSON響應。 在下面的示例中,我想知道如何檢索'issueId':'executions':'id'的值? 在下面的示例中,它是“ 8195” .....
r = requests.get(baseURL + getExecutionsForIssueId + id, auth=('user','pass'))
data = r.json()
JSON Response:
{
"status": {
"1": {
"id": 1,
"color": "#75B000",
"description": "Test was executed and passed successfully.",
"name": "PASS"
},
"2": {
"id": 2,
"color": "#CC3300",
"description": "Test was executed and failed.",
"name": "FAIL"
},
"3": {
.
.
.
}
},
"issueId": 15825,
"executions": [
{
"id": 8195,
"orderId": 7635,
"executionStatus": "-1",
"comment": "",
"htmlComment": "",
.
.
.
您的JSON
對象只是Python中的字典。 像這樣訪問所需的值:
data['executions']
產生一個類似的字典對象數組,假設您按預期輸入了JSON
響應。
executions = data['executions']
order_id = executions[0]['orderId']
如果希望遍歷它們以找到id
為8195的正確對象:
executions = data['executions'] # [{'id':8195,'orderId':7635,...}, {...}, ...]
for e in executions:
if e['id'] == 8195: # e is the dict you want
order_id = e['orderId']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.