我想将django模型作为JSON文件转储,以便通过传单使用它在地图上显示点。但是,当我转储JSON文件时,它会为JSON文件中的双引号创建其他转义序列。我的问题是如何创建和保存不带转义序列的JSON文件。
下面是我在views.py中的代码。
非常感谢您。
def JSON(request):
queryset = Poi.objects.all()
queryset = serializers.serialize('json', queryset)
queryset = queryset[:0] +'markers =' + queryset[0:]
with open (r'C:\Users\Savas\Desktop\MY_WEB_PROJECTS\WEB_TOOLS\web_tools\static\js\queryset.json', 'w') as outfile:
json.dump(queryset, outfile, ensure_ascii=False)
return HttpResponse(queryset, content_type="application/json")
这是JSON文件:
"markers =[{\"model\": \"project.poi\", \"pk\": 28, \"fields\": {\"name_site\": \"gtrgr\", \"number_order\": 15156, \"latitude\": 58.5, \"longitude\": 15.4, \"a_measured_height_lt\": 2.5, \"k_measured_height_lt\": 2.6}}, {\"model\": \"project.poi\", \"pk\": 29, \"fields\": {\"name_site\": \"Sik sok\", \"number_order\": 568956, \"latitude\": 56.9, \"longitude\": 1.5, \"a_measured_height_lt\": 2.5, \"k_measured_height_lt\": 3.6}}, {\"model\": \"project.poi\", \"pk\": 30, \"fields\": {\"name_site\": \"freg\", \"number_order\": 189465, \"latitude\": 34.6, \"longitude\": 16.5, \"a_measured_height_lt\": 2.5, \"k_measured_height_lt\": 2.5}}, {\"model\": \"project.poi\", \"pk\": 31, \"fields\": {\"name_site\": \"dewf\", \"number_order\": 1856, \"latitude\": 26.6, \"longitude\": 10.0, \"a_measured_height_lt\": 5.6, \"k_measured_height_lt\": 2.6}}, {\"model\": \"project.poi\", \"pk\": 32, \"fields\": {\"name_site\": \"nhfmh\", \"number_order\": 156, \"latitude\": 54.5, \"longitude\": 26.5, \"a_measured_height_lt\": 2.6, \"k_measured_height_lt\": 1.7}}, {\"model\": \"project.poi\", \"pk\": 35, \"fields\": {\"name_site\": \"vfv\", \"number_order\": 651561, \"latitude\": 56.8, \"longitude\": 89.5, \"a_measured_height_lt\": 5.2, \"k_measured_height_lt\": 2.6}}]"
这是我删除转义序列时的JSON字段:
markers =[{"model": "project.poi", "pk": 28, "fields": {"name_site": "gtrgr", "number_order": 15156, "latitude": 58.5, "longitude": 15.4, "a_measured_height_lt": 2.5, "k_measured_height_lt": 2.6}}, {"model": "project.poi", "pk": 29, "fields": {"name_site": "Sik sok", "number_order": 568956, "latitude": 56.9, "longitude": 1.5, "a_measured_height_lt": 2.5, "k_measured_height_lt": 3.6}}, {"model": "project.poi", "pk": 30, "fields": {"name_site": "freg", "number_order": 189465, "latitude": 34.6, "longitude": 16.5, "a_measured_height_lt": 2.5, "k_measured_height_lt": 2.5}}, {"model": "project.poi", "pk": 31, "fields": {"name_site": "dewf", "number_order": 1856, "latitude": 26.6, "longitude": 10.0, "a_measured_height_lt": 5.6, "k_measured_height_lt": 2.6}}, {"model": "project.poi", "pk": 32, "fields": {"name_site": "nhfmh", "number_order": 156, "latitude": 54.5, "longitude": 26.5, "a_measured_height_lt": 2.6, "k_measured_height_lt": 1.7}}, {"model": "project.poi", "pk": 35, "fields": {"name_site": "vfv", "number_order": 651561, "latitude": 56.8, "longitude": 89.5, "a_measured_height_lt": 5.2, "k_measured_height_lt": 2.6}}]