0

正如标题所说,我正在尝试将一组 MySQL 查询结果从 django 传递给 javascript。不幸的是,我的页面源显示了一个包含查询结果的字符串数组。这是我的 Django 代码:

resultFEs = []
for result in results:
    fes = FrameElements.objects.filter(frame_name=result.name)
    resultFEs.append(serializers.serialize('json', fes))
results_json = serializers.serialize('json', results)

return render_to_response('newInstance.html',{'scene_id':scene_id,'corpus_id':corpus_id,'sentence_id':sentence_id,'word':word,'word_position':word_position,'results':results_json,'resultFEs':resultFEs})

这是我的html:

<script type="text/javascript">
    var word = '{{word}}';
    var wordPosition = '{{word_position}}';
    var sceneId='{{scene_id}}';
    var corpusId='{{corpus_id}}';
    var sentenceId='{{sentence_id}}';
    var results ={{results|safe}};
    var resultFEs={{resultFEs|safe}};
</script>

这是生成的页面源:

<script type="text/javascript">
        var word = ' the ';
        var wordPosition = '0:0';
        var sceneId='1';
        var corpusId='1';
        var sentenceId='2';
        var results =[{"pk": 1, "model": "annotation_tool.frames", "fields": {"framenet_id": 234234, "name": "Using", "frame_type": "FRAMENET", "timestamp": null, "has_lexicalization": 1, "annotator": 19, "semiotic_status": "LITERAL"}}, {"pk": 2, "model": "annotation_tool.frames", "fields": {"framenet_id": 234234, "name": "Use_computer", "frame_type": "FRAMENET", "timestamp": null, "has_lexicalization": 1, "annotator": 19, "semiotic_status": "LITERAL"}}];
        var resultFEs=['[{"pk": 1, "model": "annotation_tool.frameelements", "fields": {"fe_name": "Agent", "frame": 1, "framenet_id": null, "frame_name": "Using", "core_status": "CORE"}}, {"pk": 2, "model": "annotation_tool.frameelements", "fields": {"fe_name": "Instrument", "frame": 1, "framenet_id": null, "frame_name": "Using", "core_status": "CORE"}}, {"pk": 13, "model": "annotation_tool.frameelements", "fields": {"fe_name": "Self", "frame": 1, "framenet_id": null, "frame_name": "Using", "core_status": "CORE"}}]', '[{"pk": 3, "model": "annotation_tool.frameelements", "fields": {"fe_name": "Agent", "frame": 2, "framenet_id": null, "frame_name": "Use_computer", "core_status": "CORE"}}, {"pk": 4, "model": "annotation_tool.frameelements", "fields": {"fe_name": "Computer", "frame": 2, "framenet_id": null, "frame_name": "Use_computer", "core_status": "CORE"}}, {"pk": 14, "model": "annotation_tool.frameelements", "fields": {"fe_name": "Self", "frame": 2, "framenet_id": null, "frame_name": "Use_computer", "core_status": "CORE"}}, {"pk": 19, "model": "annotation_tool.frameelements", "fields": {"fe_name": "Desk", "frame": 2, "framenet_id": null, "frame_name": "Use_computer", "core_status": "CORE"}}, {"pk": 20, "model": "annotation_tool.frameelements", "fields": {"fe_name": "Chair", "frame": 2, "framenet_id": null, "frame_name": "Use_computer", "core_status": "CORE"}}, {"pk": 21, "model": "annotation_tool.frameelements", "fields": {"fe_name": "Chair", "frame": 2, "framenet_id": null, "frame_name": "Use_computer", "core_status": "CORE"}}]'];
</script>

请注意,在结果页面源代码中,resultFE 的每个元素周围都有引号。基本上,我不想要这些引号,所以我有一个二维的 json 对象数组,而不是一个一维的字符串数组。

提前致谢!

4

1 回答 1

0

如果您使用 jQuery,您可以使用 parseJSON 方法安全地将字符串转换为 JSON 对象:

var resultFEs = jQuery.parseJSON( {{resultFEs|safe}} );

请参阅 parseJSON 文档:http ://api.jquery.com/jQuery.parseJSON/

于 2013-04-10T02:08:49.357 回答