试图将Django的查询结果数组传递给Javascript

时间:2013-04-10 01:52:20

标签: javascript jquery html django

正如标题所说,我正在尝试将一系列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对象数组,而不是一维字符串数组。

提前致谢!

1 个答案:

答案 0 :(得分:0)

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

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

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