Python/Json:Expecting property name enclosed in double quotes

前端 未结 16 2385
南方客
南方客 2020-11-27 03:23

I\'ve been trying to figure out a good way to load JSON objects in Python. I send this json data:

{\'http://example.org/about\': {\'http://purl.org/dc/terms/         


        
16条回答
  •  孤街浪徒
    2020-11-27 03:37

    As the other answers explain well the error occurs because of invalid quote characters passed to the json module.

    In my case I continued to get the ValueError even after replacing ' with " in my string. What I finally realized was that some quote-like unicode symbols had found their way into my string:

     “  ”  ‛  ’  ‘  `  ´  ″  ′ 
    

    To clean all of these you can just pass your string through a regular expression:

    import re
    
    raw_string = '{“key”:“value”}'
    
    parsed_string = re.sub(r"[“|”|‛|’|‘|`|´|″|′|']", '"', my_string)
    
    json_object = json.loads(parsed_string)
    
    

提交回复
热议问题