4

Do something for something in the json table?

 2 years ago
source link: https://www.codesd.com/item/do-something-for-something-in-the-json-table.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Do something for something in the json table?

advertisements

So I have this python list

[{
    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': []
}]

And in the end i want to get all the names (testcard1, testcard2, testcard3 and print them)

I tried this code (with x being the list above)

for name in x[0]:
    print name

But if i run this i get

testcard1
testcard2
testcard3

I am quite new to python and I can't really wrap my head around why this wouldn't work? Can anyone help me?


You need to iterate over the list, and extract the 'name' item:

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK