Python ValueError: No JSON object could be decoded

前端 未结 6 540
深忆病人
深忆病人 2020-12-30 07:41

I\'m trying to read a json and get its values. I have a folder with the JSON\'s archives, and I need to open all archives and get the values from them.

This is the c

相关标签:
6条回答
  • 2020-12-30 08:01

    I had the same problem today. Trying to understand the cause, I found this issue related to json module:

    http://bugs.python.org/issue18958

    Check if the file is UTF8 encoded and if it is the case, then use codecs module to open and read it or just skip the BOM (byte order mark).

    0 讨论(0)
  • 2020-12-30 08:02

    The reply suggesting that .read() was moving the cursor led to a resolution of my version of the problem. I changed

    print response.read()
    ...
    json_data = json.loads(response.read())
    

    to

    responseStr = response.read()
    print responseStr
    ...
    json_data = json.loads(responseStr)
    
    0 讨论(0)
  • 2020-12-30 08:07

    Try using this in your ajax/$http with JSON data

    contentType: "application/json; charset=utf-8"

    0 讨论(0)
  • 2020-12-30 08:11

    It's possible the .read() method is moving the cursor to the end of the file. Try:

    for filename in filenames:
        with open(os.path.join(dirname,filename)) as fd:
            json_data = json.load(fd)
    

    and see where that gets you.

    This, of course, assumes you have valid JSON, as your example demonstrates. (Look out for trailing commas)

    0 讨论(0)
  • 2020-12-30 08:12

    I resolved this error by Converting the json file to UTF-8 with no BOM. Below is a python snippet and url for conversion

    myFile=open(cases2.json, 'r')
    myObject=myFile.read()
    u = myObject.decode('utf-8-sig')
    myObject = u.encode('utf-8')
    myFile.encoding
    myFile.close()
    myData=json.loads(myObject,'utf-8')
    
    0 讨论(0)
  • 2020-12-30 08:24

    For me it was an encoding problem, you can try using Notepad++ to edit your .json file and change the Encoding to UTF-8 without BOM. Another thing you could check is if your json script is valid

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