我有json数据,我想只选择一些项目我怎么能在python中做到这一点?

时间:2015-01-26 11:37:02

标签: python json parsing

   {
       "data": [
            {
                "category": "Organization", 
                "name": "GITM IEEE", 
                "category_list": [
                    {
                        "id": "198503866828628", 
                        "name": "Organization"
                    }
                ], 
                "access_token": "x", 
                "perms": [
                    "EDIT_PROFILE", 
                    "CREATE_CONTENT", 
                    "MODERATE_CONTENT", 
                    "CREATE_ADS", 
                    "BASIC_ADMIN"
                ], 
                "id": "474676675993584"
            }, 
            {
                "category": "Event planning/event services", 
                "name": "Ignitron 2014", 
                "category_list": [
                    {
                        "id": "192119584190796", 
                        "name": "Event"
                    }
                ], 
                "access_token": "x", 
                "perms": [
                    "ADMINISTER", 
                    "EDIT_PROFILE", 
                    "CREATE_CONTENT", 
                    "MODERATE_CONTENT", 
                    "CREATE_ADS", 
                    "BASIC_ADMIN"
                ], 
                "id": "731808386867764"
            }, 
            {
                "access_token": "x", 
                "category": "Community organization", 
                "perms": [
                    "ADMINISTER", 
                    "EDIT_PROFILE", 
                    "CREATE_CONTENT", 
                    "MODERATE_CONTENT", 
                    "CREATE_ADS", 
                    "BASIC_ADMIN"
                ], 
                "name": "GITM Model United Nations (GITM MUN)", 
                "id": "232082683584902"
            }, 
            {
                "access_token": "x", 
                "category": "Computers/technology", 
                "perms": [
                    "ADMINISTER", 
                    "EDIT_PROFILE", 
                    "CREATE_CONTENT", 
                    "MODERATE_CONTENT", 
                    "CREATE_ADS", 
                    "BASIC_ADMIN"
                ], 
                "name": "Bros!s", 
                "id": "332832946796051"
            }, 
            {
                "access_token": "x", 
                "category": "Musician/band", 
                "perms": [
                    "ADMINISTER", 
                    "EDIT_PROFILE", 
                    "CREATE_CONTENT", 
                    "MODERATE_CONTENT", 
                    "CREATE_ADS", 
                    "BASIC_ADMIN"
                ], 
                "name": "6 Strings", 
                "id": "267855836697950"
            }, 
            {
                "access_token": "x", 
                "category": "Sports team", 
                "perms": [
                    "ADMINISTER", 
                    "EDIT_PROFILE", 
                    "CREATE_CONTENT", 
                    "MODERATE_CONTENT", 
                    "CREATE_ADS", 
                    "BASIC_ADMIN"
                ], 
                "name": "Counter Strike::74th Cavalry", 
                "id": "486465148068957"
            }, 
            {
                "access_token": "x", 
                "category": "Entertainer", 
                "perms": [
                    "ADMINISTER", 
                    "EDIT_PROFILE", 
                    "CREATE_CONTENT", 
                    "MODERATE_CONTENT", 
                    "CREATE_ADS", 
                    "BASIC_ADMIN"
                ], 
                "name": "Bakchod's of Hostel.", 
                "id": "356188731080152"
            }
        ]
    }

这是数据,我想选择页面的名称我怎么能在python中做到这一点?

2 个答案:

答案 0 :(得分:0)

遍历json_data["data"]中的词典列表,并使用密钥访问您需要的任何内容。

print([dic["name"] for dic in json_data["data"]])
['GITM IEEE', 'Ignitron 2014', 'GITM Model United Nations (GITM MUN)', 'Bros!s', '6 Strings', 'Counter Strike::74th Cavalry', "Bakchod's of Hostel."]

如果有可能name不在每个字典中,请使用in进行检查:

print([dic["name"] for dic in d["data"] if "name" in dic])

答案 1 :(得分:0)

此代码应该完成工作:

import json

json_file = open('your_json_file_path')
json_dict = json.load(json_file)

site_names = []

for page in json_dict.get('data', []):
    if page.get('name'):
        site_names.append(page['name'])

print site_names

<强>输出:

[u'GITM IEEE', u'Ignitron 2014', u'GITM Model United Nations (GITM MUN)', u'Bros
!s', u'6 Strings', u'Counter Strike::74th Cavalry', u"Bakchod's of Hostel."]