Check if key exists and iterate the JSON array using Python

前端 未结 7 585
南旧
南旧 2020-12-02 06:28

I have a bunch of JSON data from Facebook posts like the one below:

{\"from\": {\"id\": \"8\", \"name\": \"Mary Pinter\"}, \"message\": \"How ARE you?\", \"c         


        
7条回答
  •  一整个雨季
    2020-12-02 07:19

    If all you want is to check if key exists or not

    h = {'a': 1}
    'b' in h # returns False
    

    If you want to check if there is a value for key

    h.get('b') # returns None
    

    Return a default value if actual value is missing

    h.get('b', 'Default value')
    

提交回复
热议问题