Python/Json:Expecting property name enclosed in double quotes

前端 未结 16 2369
南方客
南方客 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:44

    as JSON only allows enclosing strings with double quotes you can manipulate the string like this:

    str = str.replace("\'", "\"")
    

    if your JSON holds escaped single-quotes (\') then you should use the more precise following code:

    import re
    p = re.compile('(?

    This will replace all occurrences of single quote with double quote in the JSON string str and in the latter case will not replace escaped single-quotes.

    You can also use js-beautify which is less strict:

    $ pip install jsbeautifier
    $ js-beautify file.js
    

提交回复
热议问题