Reading JSON from a file?

前端 未结 7 1842
無奈伤痛
無奈伤痛 2020-11-22 03:33

I am getting a bit of headache just because a simple looking, easy statement is throwing some errors in my face.

I have a json file called strings.json like this:

相关标签:
7条回答
  • 2020-11-22 04:01

    Here is a copy of code which works fine for me

    import json
    
    with open("test.json") as json_file:
        json_data = json.load(json_file)
        print(json_data)
    

    with the data

    {
        "a": [1,3,"asdf",true],
        "b": {
            "Hello": "world"
        }
    }
    

    you may want to wrap your json.load line with a try catch because invalid JSON will cause a stacktrace error message.

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