2011-05-12 4 views
1

장고 직렬화를 사용하여 JSON 객체를 성공적으로 생성하고 있습니다. 나는이보기에 그렇게 :jQuery를 사용하여 장고의 직렬화로 생성 된 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을 뱉어 :

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> 

: 나는 아래 jQuery 코드이 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"}}] 

json.fields.title로 내용에 액세스하려고 시도했지만 작동하지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

답변

2
var type = json[0].fields.title 

먼저 배열 색인을 정의해야합니다. 그 외에는 옳은 길을 가고 있습니다.

+0

아아 스냅. 감사! – tabdon