Python-解析JSON数据集
我正在尝试解析看起来像这样的JSON数据集:
{"data":[ {
"Rest":0,
"Status":"The campaign is moved to the archive",
"IsActive":"No",
"StatusArchive":"Yes",
"Login":"some_login",
"ContextStrategyName":"Default",
"CampaignID":1111111,
"StatusShow":"No",
"StartDate":"2013-01-20",
"Sum":0,
"StatusModerate":"Yes",
"Clicks":0,
"Shows":0,
"ManagerName":"XYZ",
"StatusActivating":"Yes",
"StrategyName":"HighestPosition",
"SumAvailableForTransfer":0,
"AgencyName":null,
"Name":"Campaign_01"
},
{
"Rest":82.6200000000008,
"Status":"Impressions will begin tomorrow at 10:00",
"IsActive":"Yes",
"StatusArchive":"No",
"Login":"some_login",
"ContextStrategyName":"Default",
"CampaignID":2222222,
"StatusShow":"Yes",
"StartDate":"2013-01-28",
"Sum":15998,"StatusModerate":"Yes",
"Clicks":7571,
"Shows":5535646,
"ManagerName":"XYZ",
"StatusActivating":"Yes",
"StrategyName":"HighestPosition",
"SumAvailableForTransfer":0,
"AgencyName":null,
"Name":"Campaign_02"
}
]
}
假设可以有许多这样的数据集。
我想遍历它们中的每一个并获取“名称”和“广告系列ID”参数。
到目前为止,我的代码看起来像这样:
decoded_response = response.read().decode("UTF-8")data = json.loads(decoded.response)
for item in data[0]:
for x in data[0][item] ...
-> need a get name procedure
-> need a get campaign_id procedure
可能挺简单的!我对列表/字典不好:(
回答:
使用d[dict_key]
或d.get(dict_key, default)
(提供默认值)访问词典:
jsonResponse=json.loads(decoded_response)jsonData = jsonResponse["data"]
for item in jsonData:
name = item.get("Name")
campaignID = item.get("CampaignID")
我建议你读一些有关字典的东西。
以上是 Python-解析JSON数据集 的全部内容, 来源链接: utcz.com/qa/419541.html