您好我正在解析序列化的json -
def wall_copy(request):
posts = user_post.objects.order_by('id')[:20].reverse()
posts_serialized = serializers.serialize('json', posts)
return JsonResponse(posts_serialized, safe=False)
并尝试将数据设为 -
$(document).ready(function()
{
setInterval(function()
{
var xhr = new XMLHttpRequest();
xhr.open("GET","http://10.8.21.17:8000/wall/wall_copy/",false);
xhr.send(null);
var data = JSON.parse(xhr.responseText);
//alert(xhr.status+' '+xhr.statusText);
for(i=0; i<20; i++)
{
alert(data[i].post_content);
}
}, 3000);
});
但问题是每次它都警告为“未定义”。我检查了网址,服务器正在发送json数据,但我无法获取它。
Json数据 -
"[{\"fields\": {\"post_id\": \"rahularyan06:16PM on February 26, 2015\", \"posted_by\": \"rahularyan\", \"post_content\": \"koi nhi re\", \"time_of_post\": \"2015-02-26T18:16:00Z\"}, \"model\": \"wall.user_post\", \"pk\": 77}, {\"fields\": {\"post_id\": \"aquaman06:00PM on February 26, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"kuch nhi\", \"time_of_post\": \"2015-02-26T18:00:44Z\"}, \"model\": \"wall.user_post\", \"pk\": 76}, {\"fields\": {\"post_id\": \"rahularyan06:00PM on February 26, 2015\", \"posted_by\": \"rahularyan\", \"post_content\": \"kya hua??\", \"time_of_post\": \"2015-02-26T18:00:04Z\"}, \"model\": \"wall.user_post\", \"pk\": 75}, {\"fields\": {\"post_id\": \"aquaman12:01AM on February 26, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"lelo\", \"time_of_post\": \"2015-02-26T00:01:14Z\"}, \"model\": \"wall.user_post\", \"pk\": 74}, {\"fields\": {\"post_id\": \"aquaman10:41AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"sahi me lega??\", \"time_of_post\": \"2015-02-25T10:41:43Z\"}, \"model\": \"wall.user_post\", \"pk\": 73}, {\"fields\": {\"post_id\": \"aquaman10:41AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"sahi me lega??\", \"time_of_post\": \"2015-02-25T10:41:34Z\"}, \"model\": \"wall.user_post\", \"pk\": 72}, {\"fields\": {\"post_id\": \"shreyansh10:41AM on February 25, 2015\", \"posted_by\": \"shreyansh\", \"post_content\": \"yo\", \"time_of_post\": \"2015-02-25T10:41:17Z\"}, \"model\": \"wall.user_post\", \"pk\": 71}, {\"fields\": {\"post_id\": \"aquaman10:40AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"muh me lega??\", \"time_of_post\": \"2015-02-25T10:40:53Z\"}, \"model\": \"wall.user_post\", \"pk\": 70}, {\"fields\": {\"post_id\": \"aquaman10:40AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"bc\", \"time_of_post\": \"2015-02-25T10:40:42Z\"}, \"model\": \"wall.user_post\", \"pk\": 69}, {\"fields\": {\"post_id\": \"shreyansh10:40AM on February 25, 2015\", \"posted_by\": \"shreyansh\", \"post_content\": \"lele\", \"time_of_post\": \"2015-02-25T10:40:28Z\"}, \"model\": \"wall.user_post\", \"pk\": 68}, {\"fields\": {\"post_id\": \"aquaman10:39AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"kya bhai??\", \"time_of_post\": \"2015-02-25T10:39:39Z\"}, \"model\": \"wall.user_post\", \"pk\": 67}, {\"fields\": {\"post_id\": \"aquaman10:38AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"sb mast\", \"time_of_post\": \"2015-02-25T10:38:22Z\"}, \"model\": \"wall.user_post\", \"pk\": 66}, {\"fields\": {\"post_id\": \"shreyansh10:38AM on February 25, 2015\", \"posted_by\": \"shreyansh\", \"post_content\": \"or baaki??\", \"time_of_post\": \"2015-02-25T10:38:05Z\"}, \"model\": \"wall.user_post\", \"pk\": 65}, {\"fields\": {\"post_id\": \"aquaman10:03AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"badiya\", \"time_of_post\": \"2015-02-25T10:03:39Z\"}, \"model\": \"wall.user_post\", \"pk\": 64}, {\"fields\": {\"post_id\": \"shreyansh10:03AM on February 25, 2015\", \"posted_by\": \"shreyansh\", \"post_content\": \"thik ba\", \"time_of_post\": \"2015-02-25T10:03:23Z\"}, \"model\": \"wall.user_post\", \"pk\": 63}, {\"fields\": {\"post_id\": \"aquaman10:00AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"kaisan??\", \"time_of_post\": \"2015-02-25T10:00:30Z\"}, \"model\": \"wall.user_post\", \"pk\": 62}, {\"fields\": {\"post_id\": \"aquaman10:00AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"hi\", \"time_of_post\": \"2015-02-25T10:00:04Z\"}, \"model\": \"wall.user_post\", \"pk\": 61}, {\"fields\": {\"post_id\": \"aquaman09:58AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"abe jldi likh\", \"time_of_post\": \"2015-02-25T09:58:43Z\"}, \"model\": \"wall.user_post\", \"pk\": 60}, {\"fields\": {\"post_id\": \"aquaman09:57AM on February 25, 2015\", \"posted_by\": \"aquaman\", \"post_content\": \"hello\", \"time_of_post\": \"2015-02-25T09:57:49Z\"}, \"model\": \"wall.user_post\", \"pk\": 59}, {\"fields\": {\"post_id\": \"shreyansh09:39AM on February 25, 2015\", \"posted_by\": \"shreyansh\", \"post_content\": \"lele\", \"time_of_post\": \"2015-02-25T09:39:39Z\"}, \"model\": \"wall.user_post\", \"pk\": 58}]"
帮助我如何通过javascript获取此数据。
提前致谢。
答案 0 :(得分:4)
您应该以这种方式访问javascript中的json响应:
data[i].fields.post_content
序列化程序为每个对象创建一个带键的字典
fields
,这是每个字段的值model
,这是您的案例中的型号名称"wall.user_post"
。pk
,这是您对象的主键。同样在您的视图中,您将两次转换为json。一次使用序列化程序,一次使用JsonResponse
。您应该从视图中返回以下值:
return HttpResponse(posts_serialized, content_type="application/json")
答案 1 :(得分:0)
以下是我管理简单JSON示例的方法:
def search(request):
rows = MyDBObject.objects.all()
response = {}
for row in rows:
response[row .id] = {'name': row.name, 'field1': row.field1, 'field2': row.field2}
return JsonResponse(response)
在javascript中:
$.getJSON('/ajax/search', function(data) {
var items = [];
$.each(data, function(key, val) {
items.push('<li id="' + key + '"><a href="' + val['field2'] + '" data-ajax="false">' + val['field1'] + ' by ' + val['field2'] + '</a></li>');
});
$('<ul/>', {
'class': 'my-new-list',
html: items.join('')
}).appendTo('#search_results_list');
});