Can't parse json file: json.decoder.JSONDecodeError: Extra data.

后端 未结 3 741
走了就别回头了
走了就别回头了 2020-12-20 03:56

I have a json file. A simplified version of it looks as following:

{
  \"host\": \"a.com\",
  \"ip\": \"1.2.2.3\",
  \"port\": 8
}
{
  \"host\":         


        
3条回答
  •  感动是毒
    2020-12-20 04:32

    As you already found out: that is not valid JSON.
    You have to modify it to make it valid, specifically, you have to wrap your top-level objects in an array. Try this:

    import json
    from pprint import pprint
    
    with open('myfile.json') as f:
        data = json.loads("[" + 
            f.read().replace("}\n{", "},\n{") + 
        "]")
    
        print(data)
    

提交回复
热议问题