Trying to find a simple way to import data from a JSON file into Python. My initial thoughts would be to read the file line by line, but this might imply some additional processing which should already be done in a library.
The ideal solution would look something like:
import json_library
the_data = json_library.load_from_file('my_file.json')
where 'my_file.json' contains a JSON-formatted variable.
解决方案
json will do that for you.
import json
data = json.load(open('my_file.json', 'r'))
Content of demo file:
{"hello":"stack overflow"}
Demo:
>>> import json
>>> print(json.load(open('my_file.json','r')))
{u'hello': u'stack overflow'}