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.
我的mysql数据库中有一个对象(或字符串?我不确定..),我想在视图中将其转回python字典,然后处理它然后发送回html。
so here is my code in views.py:
所以这是我在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:
_clickedPts看起来像这样:
{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)}}
{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)}}
2 个解决方案
#1
1
Your string isn't a proper JSON string, and actually looks like a Python JSON object. You would be better off using ast.literal_eval to convert this string to a Python JSON Object. Just call ast.literal_val(_ClickedPts) instead of json.loads(_ClickedPts) to get what you want.
您的字符串不是正确的JSON字符串,实际上看起来像一个Python JSON对象。最好使用ast.literal_eval将此字符串转换为Python JSON对象。只需调用ast.literal_val(_ClickedPts)而不是json.loads(_ClickedPts)即可获得所需内容。
#2
0
The following code works:
以下代码有效:
test = ClickedPts.objects.values('pts')
test = test[0]['pts']
test = ast.literal_eval(test)
#print type(test)
#print test
#1
1
Your string isn't a proper JSON string, and actually looks like a Python JSON object. You would be better off using ast.literal_eval to convert this string to a Python JSON Object. Just call ast.literal_val(_ClickedPts) instead of json.loads(_ClickedPts) to get what you want.
您的字符串不是正确的JSON字符串,实际上看起来像一个Python JSON对象。最好使用ast.literal_eval将此字符串转换为Python JSON对象。只需调用ast.literal_val(_ClickedPts)而不是json.loads(_ClickedPts)即可获得所需内容。
#2
0
The following code works:
以下代码有效:
test = ClickedPts.objects.values('pts')
test = test[0]['pts']
test = ast.literal_eval(test)
#print type(test)
#print test