Parsing JSON with Python: TypeError: list indices must be integers, not str

前端 未结 3 1975
执念已碎
执念已碎 2020-12-14 13:04

I\'m using Python to parse through some JSON data for specific values. Specifically I want to pull the following:

  • author_id
  • created_at
  • public
相关标签:
3条回答
  • 2020-12-14 13:25

    It's exactly as the error says. fields['events'] is a list, so you can't index it with ['public']. You need to iterate through the values, each of which is a dictionary.

    for event in fields['events']:
        print event['public']
    
    0 讨论(0)
  • 2020-12-14 13:29
    print(fields['events'][0]['public'])
    

    fields['events'] is a list so you need to use ['events'][0] to access the dict inside the list.

    0 讨论(0)
  • 2020-12-14 13:43

    Insted of fields['events']['public'] it should be fields['events'][0]['public']

    0 讨论(0)
提交回复
热议问题