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

前端 未结 3 1978
执念已碎
执念已碎 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']
    

提交回复
热议问题