将JSON数据导入Python

Jua*_*oto 1 python json file

试图找到一种将数据从JSON文件导入Python的简单方法.我最初的想法是逐行读取文件,但这可能意味着应该在库中进行一些额外的处理.

理想的解决方案看起来像:

import json_library

the_data = json_library.load_from_file('my_file.json')
Run Code Online (Sandbox Code Playgroud)

其中'my_file.json'包含一个JSON格式的变量.

tim*_*geb 6

json会为你做这件事.

import json
data = json.load(open('my_file.json', 'r'))
Run Code Online (Sandbox Code Playgroud)

演示文件的内容:

{"hello":"stack overflow"}
Run Code Online (Sandbox Code Playgroud)

演示:

>>> import json
>>> print(json.load(open('my_file.json','r')))
{u'hello': u'stack overflow'}
Run Code Online (Sandbox Code Playgroud)