如何从django响应中提取数据

JSON - How to extract data recieved from django response?

本文关键字:提取 数据 响应 django      更新时间:2023-09-26

我的回复是这样的:

[{"0":"k@k.com"},{"1":"p@p.com"},{"2":"n@n.com"},{"3":"b@b.com},{"4":"k1@k1.com"},{"5":"z@z.com"},{"6":"k2@k2.com"}]

我正试图通过以下html阅读此内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function(){

        document.write('Getting JSON response...');
        $.getJSON(<my django url>,
            function(data) {
            alert("receiveng"+data["0"]);
            });
    });
</script>
</head>

<body>
</body>
</html>

但我没有得到任何警告框的数据,只是得到JSON响应.....

这是我的返回语句:

return HttpResponse( simplejson.dumps(result) , content_type='application/json')

问题是在你的JSON格式,不能被解析/评估。例如,任何JSON对象都是这样的:

[
    {"attributeName1":"value1", "attributeName2":"value2"},
    {"attributeName1":"value3", "attributeName2":"value4"}
]

但在JSON示例中,您将attributeName1作为0。如果我们给变量命名为0也是一样的。比如define 0 = k@k.com。这是不可能的。试着把它改成id0

谢谢大家的帮助。

data[0][0]表记有效。

运行循环,直到data[i][j]的值为undefined,我用的是:

数据[我][i.toString ());