JSON 파일





데이타형으로 읽기


def json(request):
import json

data = {'team': 'Brazil', 'position': 1, 'host': True, 'lastGame': 'Win'}
dictToJson = json.dumps(data)
return HttpResponse(json.dumps(dictToJson), content_type="application/json")

response = {'error': str(sys.exc_info()[1])}

return HttpResponse(json.dumps(response), content_type="application/json")


def json_response(request):
import datetime
now = datetime.datetime.now()

result = {}
result.update( {'aaa' : 'aaaa'} )
result.update( {'bbb' : 'bbbb'} )
result.update( {'ddd' : 'dddd'} )
result.update( {'eee' : 'eeee'} )
result.update( {'time' : now} )


return JsonResponse(result)


파일형식으로 읽기


def jsonfile(request):
# return HttpResponse(open("data/static/FROALA_CHART/sample_json/CHART_COMP_pie.json", 'rb').read())

from django.conf import settings
from django.http import JsonResponse

p = os.path.join(settings.STATIC_URL, 'FROALA_CHART/sample_json/CHART_COMP_pie.json')

    getFile = open('data/static/FROALA_CHART/sample_json/CHART_COMP_pie.json', 'rb')
json_data = getFile.read()
return HttpResponse(json_data)


def jsonread(request):
import json
filePath = 'data/static/test/json/data.json'
filePath = 'data/arbre/testjson/data.json'
getDATA = json.load(open(filePath, encoding='utf-8'))
html = ''
for i in getDATA:
html+= i + "<br/>"

return HttpResponse(mark_safe(html))