Python - 从JSON获取数据

时间:2018-02-23 16:31:19

标签: python json

我正在使用API​​从外部源获取JSON数据,并且不确定如何从JSON获取特定元素。

json_data = json.loads(resp.text) #resp.text is the response from the API call
print(json_data)

打印以下(原始输出为单行/平面,因此我进行了格式化以使其更具可读性)

[
{'version': {
    'rowVersion': 2044}, 
    'name': 'Administrator', 
    'permissions': [
                        {'path': 'Activity/Cancel All', 'name': 'Activity/Cancel All'}, 
                        {'path': 'Activity/View All', 'name': 'Activity/View All'}, 
                        {'path': 'Audit Log/Edit', 'name': 'Audit Log/Edit'}, 
                        {'path': 'Audit Log/View', 'name': 'Audit Log/View'}
                    ], 
    'id': 1, 
    'permissionPaths': ['Activity/Cancel All', 'Activity/View All', 'Audit Log/Edit', 'Audit Log/View'], 
    'displayName': 'Administrator'
}, 

{'version': {
    'rowVersion': 964033}, 
    'name': 'ViewOnly', 
    'permissions': [
                        {'path': 'Activity/View All', 'name': 'Activity/View All'}, 
                        {'path': 'Audit Log/View', 'name': 'Audit Log/View'}, 
                    ], 
    'id': 4, 
    'permissionPaths': ['Activity/View All', 'Audit Log/View'] 
    'displayName': 'ViewOnly'
}
]

以下

for ROLES in json_data:
    print(str(ROLES["name"]))

打印:

Administrator
ViewOnly 

到目前为止,这么好...... 但是,我希望输出是角色和权限名称......

Administrator, Activity/Cancel All
Administrator, Activity/View All
Administrator, Audit Log/Edit
Administrator, Audit Log/View
ViewOnly, Activity/View All
ViewOnly, Audit Log/View

请告诉我如何在Python中实现这一目标。 谢谢!

3 个答案:

答案 0 :(得分:1)

这应该有所帮助。

# -*- coding: utf-8 -*-

json_data = [
{'version': {
    'rowVersion': 2044},
    'name': 'Administrator',
    'permissions': [
                        {'path': 'Activity/Cancel All', 'name': 'Activity/Cancel All'},
                        {'path': 'Activity/View All', 'name': 'Activity/View All'},
                        {'path': 'Audit Log/Edit', 'name': 'Audit Log/Edit'},
                        {'path': 'Audit Log/View', 'name': 'Audit Log/View'}
                    ],
    'id': 1,
    'permissionPaths': ['Activity/Cancel All', 'Activity/View All', 'Audit Log/Edit', 'Audit Log/View'],
    'displayName': 'Administrator'
},

{'version': {
    'rowVersion': 964033},
    'name': 'ViewOnly',
    'permissions': [
                        {'path': 'Activity/View All', 'name': 'Activity/View All'},
                        {'path': 'Audit Log/View', 'name': 'Audit Log/View'},
                    ],
    'id': 4,
    'permissionPaths': ['Activity/View All', 'Audit Log/View'],
    'displayName': 'ViewOnly'
}
]

for ROLES in json_data:
    for n in ROLES["permissions"]:
        print(ROLES["name"], n["name"])

<强>输出:

('Administrator', 'Activity/Cancel All')
('Administrator', 'Activity/View All')
('Administrator', 'Audit Log/Edit')
('Administrator', 'Audit Log/View')
('ViewOnly', 'Activity/View All')
('ViewOnly', 'Audit Log/View')

答案 1 :(得分:0)

这项任务有很多解决方案。接下来显示了列表理解的使用。

for role in json_data:
    print("\n".join(["{role.name}, {permission.name}".format(role=role, permission=permission) for permission in role.permissions]))

您还可以添加额外的for-loop

for role in json_data:
    for permission in role.permissions:
        print("{role.name}, {permission.name}".format(role=role, permission=permission))

答案 2 :(得分:0)

这是我的建议,它允许您通过单行样式代码在列表中获得所需内容。 for循环仅用于打印列表中的每个元素(如果需要):

myList = ["{0}, {1}".format(ROLES["name"], perm["name"]) for ROLES in json_data for perm in ROLES['permissions']]
for i in myList:
    print(i)