为json表中的每个东西做些什么?

时间:2017-03-14 16:29:10

标签: python json

所以我有这个python列表

[{
    u 'labels': [], u 'pos': 65535,
        u 'manualCoverAttachment':
        False, u 'id': u '58c7019327481c09a2a7d0c3',
        u 'badges': {
            u 'votes': 0, u 'attachments':
                0, u 'subscribed':
                False, u 'due':
                None, u 'comments':
                0, u 'dueComplete':
                False, u 'checkItemsChecked':
                0, u 'fogbugz': u '',
                u 'viewingMemberVoted':
                False, u 'checkItems':
                0, u 'description':
                False
        }, u 'idBoard': u '58c7018716c30b92076cadb1',
        u 'idShort': 1, u 'due':
        None, u 'dueComplete':
        False, u 'shortUrl': u 'https://trello.com/c/qT26hX5v',
        u 'closed': False, u 'subscribed':
        False, u 'dateLastActivity':
        u '2017-03-13T20:31:15.161Z',
        u 'idList': u '58c7018fe93219584112da56',
        u 'idMembersVoted': [], u 'idLabels': [],
        u 'idMembers': [], u 'checkItemStates':
        None, u 'desc': u '', u 'descData':
        None, u 'name': u 'testcard1',
        u 'shortLink': u 'qT26hX5v',
        u 'idAttachmentCover': None,
        u 'url': u 'https://trello.com/c/qT26hX5v/1-testcard1',
        u 'idChecklists': []
}, {
    u 'labels': [], u 'pos': 131071,
        u 'manualCoverAttachment':
        False, u 'id': u '58c815ac4fc3659e11c96e1a',
        u 'badges': {
            u 'votes': 0, u 'attachments':
                0, u 'subscribed':
                False, u 'due':
                None, u 'comments':
                0, u 'dueComplete':
                False, u 'checkItemsChecked':
                0, u 'fogbugz': u '',
                u 'viewingMemberVoted':
                False, u 'checkItems':
                0, u 'description':
                False
        }, u 'idBoard': u '58c7018716c30b92076cadb1',
        u 'idShort': 2, u 'due':
        None, u 'dueComplete':
        False, u 'shortUrl': u 'https://trello.com/c/QCm1is4i',
        u 'closed': False, u 'subscribed':
        False, u 'dateLastActivity':
        u '2017-03-14T16:09:16.656Z',
        u 'idList': u '58c7018fe93219584112da56',
        u 'idMembersVoted': [], u 'idLabels': [],
        u 'idMembers': [], u 'checkItemStates':
        None, u 'desc': u '', u 'descData':
        None, u 'name': u 'testcard2',
        u 'shortLink': u 'QCm1is4i',
        u 'idAttachmentCover': None,
        u 'url': u 'https://trello.com/c/QCm1is4i/2-testcard2',
        u 'idChecklists': []
}, {
    u 'labels': [], u 'pos': 196607,
        u 'manualCoverAttachment':
        False, u 'id': u '58c815aefbd1e359b000ce86',
        u 'badges': {
            u 'votes': 0, u 'attachments':
                0, u 'subscribed':
                False, u 'due':
                None, u 'comments':
                0, u 'dueComplete':
                False, u 'checkItemsChecked':
                0, u 'fogbugz': u '',
                u 'viewingMemberVoted':
                False, u 'checkItems':
                0, u 'description':
                False
        }, u 'idBoard': u '58c7018716c30b92076cadb1',
        u 'idShort': 3, u 'due':
        None, u 'dueComplete':
        False, u 'shortUrl': u 'https://trello.com/c/kKZvxSbL',
        u 'closed': False, u 'subscribed':
        False, u 'dateLastActivity':
        u '2017-03-14T16:09:18.930Z',
        u 'idList': u '58c7018fe93219584112da56',
        u 'idMembersVoted': [], u 'idLabels': [],
        u 'idMembers': [], u 'checkItemStates':
        None, u 'desc': u '', u 'descData':
        None, u 'name': u 'testcard3',
        u 'shortLink': u 'kKZvxSbL',
        u 'idAttachmentCover': None,
        u 'url': u 'https://trello.com/c/kKZvxSbL/3-testcard3',
        u 'idChecklists': []
}]

最后我想得到所有的名字(testcard1,testcard2,testcard3并打印出来)

我尝试了这段代码(x是上面的列表)

for name in x[0]:
    print name

但如果我跑这个我得

testcard1
testcard2
testcard3

我对python很陌生,我无法理解为什么这不起作用?任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:0)

从这个问题来看,可以理解x是一个字典列表。遍历列表x的每个元素,并找到键'name'的值。

for i in x:
     print i['name']

答案 1 :(得分:0)

您需要遍历列表,并提取“名称”项:

for item in list:
    print(item['name'])

答案 2 :(得分:0)

试试这个,然后打印名称,键:其余项目的值。

for name in x:
    print("\n\n\n\n")
    print name['name']
    print "---------------------------------"
    for data in name:
        print data + " : " + str(name[data])
    print "---------------------------------"