Francica Zhao Francica Zhao - 4 months ago 13
JSON Question

json.loads not working in django

I have a object(or string ? I am not sure..) in my mysql database, and I want to turn it back to python dictionary in views and then process it and then send back to html.

so here is my code in views.py:

def measure_detail(request, measurement_id):

_ClickedPts = ClickedPts.objects.get(measurement=measurement_id)

_ClickedPts = json.loads(_ClickedPts)

return render(request, 'measurement_detail.html', {'measurement': _measurement, 'clicked_pts': _ClickedPts})


_clickedPts looks like this:


{0: {u'loc': [1, 2], u'pts': [[456.45631334215614, 1155.23758883418], [1087.2590986710893, 1175.6156203824098]], u'imsize': (3264, 2448)}, 9: {u'loc': [1, 2], u'pts': [[838.4383679397915, 1183.738210969312], [1751.0879501479042, 1179.0747927841176]], u'imsize': (3264, 2448)}, 28: {u'loc': [1, 2], u'pts': [[1544.854017880959, 1041.081669022464], [2262.6099907645976, 1044.953878976804]], u'imsize': (3264, 2448)}, 14: {u'loc': [1, 2], u'pts': [[1122.5721676687076, 1211.5634319190933], [2053.740361759838, 1221.0344281491446]], u'imsize': (3264, 2448)}, 15: {u'loc': [1, 2], u'pts': [[1037.1904375418785, 1166.702956796377], [1959.451782586886, 1190.2974020603272]], u'imsize': (3264, 2448)}}

Answer

The following code works:

test = ClickedPts.objects.values('pts')

test = test[0]['pts']

test = ast.literal_eval(test)

#print type(test)
#print test