我有一个 ajax View :
def ajax_prices(request):
data = {'data':'data'}
return HttpResponse(json.dumps(data), mimetype='application/json')
我想使用本地 json 文件 (prices.json) 对此进行测试。如何导入本地json文件?
本地 json 文件 'prices.json'
{"aaData": [
[1, "70.1700", "2008-12-29 11:23:00"],
[2, "70.2600", "2008-12-29 16:22:00"],
[3, "70.6500", "2008-12-30 11:30:00"],
[4, "70.8700", "2008-12-30 16:10:00"],
[5, "70.5500", "2009-01-02 11:09:00"],
[6, "70.6400", "2009-01-02 16:15:00"],
[7, "70.6500", "2009-01-05 11:17:00"]
]}
我不能这样做:
data = '/static/prices.json'
最佳答案
使用json模块:
import json
json_data = open('/static/prices.json')
data1 = json.load(json_data) # deserialises it
data2 = json.dumps(data1) # json formatted string
json_data.close()
参见 here了解更多信息。
关于python - Django加载本地json文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10498234/