我正在使用Django Serialization成功生成JSON对象。我这样认为:
def fetch_content(request, content_id):
content = serializers.serialize('json', [ContentQueue.objects.get(id=content_id)], fields=('title', 'description'))
mimetype = 'application/javascript; charset=utf8'
return HttpResponse(content, mimetype)
这会像这样吐出JSON:
[{"pk": 24, "model": "contentqueue", "fields": {"description": "Aerosmith frontman Steven Tyler was typically gabby...", "title": "Steven Tyler Tells All: On The Cover of Rolling Stone"}}]
我正在尝试使用以下jQuery代码读取此JSON文件:
api_url = /fetch-content/ + content_id;
$.getJSON(api_url, function(json) {
var type = json.title;
var desc = json.description;
<then I display the content onscreen>
我试过通过json.fields.title访问内容,但这不起作用。我做错了什么?
答案 0 :(得分:2)
var type = json[0].fields.title
首先必须定义数组索引。除此之外,你走在正确的轨道上。