JSONDecodeError: Expecting value: line 1 column 1

后端 未结 2 732
无人共我
无人共我 2021-02-04 00:30

I am receiving this error in Python 3.5.1.

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Here is my code:<

相关标签:
2条回答
  • 2021-02-04 01:06

    If you look at the output you receive from print() and also in your Traceback, you'll see the value you get back is not a string, it's a bytes object (prefixed by b):

    b'{\n  "note":"This file    .....
    

    If you fetch the URL using a tool such as curl -v, you will see that the content type is

    Content-Type: application/json; charset=utf-8
    

    So it's JSON, encoded as UTF-8, and Python is considering it a byte stream, not a simple string. In order to parse this, you need to convert it into a string first.

    Change the last line of code to this:

    info = json.loads(js.decode("utf-8"))
    
    0 讨论(0)
  • 2021-02-04 01:06

    in my case, some characters like " , :"'{}[] " maybe corrupt the JSON format, so use try json.loads(str) except to check your input

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