Python - 解析 JSON 数据集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16675849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:20:32  来源:igfitidea点击:

Python - Parsing JSON Data Set

pythonjsondictionary

提问by Aivoric

I am trying to parse a JSON data set that looks something like this:

我正在尝试解析如下所示的 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"
    }
    ]
}

Lets assume that there can be many of these data sets.

让我们假设可以有很多这样的数据集。

I would like to iterate through each one of them and grab the "Name" and the "Campaign ID" parameter.

我想遍历它们中的每一个并获取“名称”和“广告系列 ID”参数。

So far my code looks something like this:

到目前为止,我的代码看起来像这样:

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

Probably quite straight forward! I am not good with lists/dictionaries :(

可能很直接!我不擅长列表/字典:(

采纳答案by Zac

Access dictionaries with d[dict_key]or d.get(dict_key, default)(to provide default value):

使用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")

I suggest you read something about dictionaries.

我建议你读一些关于字典的东西。