2013-03-28 3 views
1

일부 JSON을보기에 게시합니다. 이제 데이터를 구문 분석하여 데이터베이스에 추가하려고합니다.장고보기에서 JSON을 구문 분석하는 방법

속성 nametheme을 가져와 pages 배열을 반복해야합니다. 다음과 같이 내 JSON은 다음과 같습니다

여기
{ 
    "name": "xaAX", 
    "logo": "", 
    "theme": "b", 
    "fullSiteLink": "http://www.hello.com", 
    "pages": [ 
     { 
      "id": "1364484811734", 
      "name": "Page Name", 
      "type": "basic", 
      "components": { 
       "img": "", 
       "text": "" 
      } 
     }, 

     { 
      "name": "Twitter", 
      "type": "twitter", 
      "components": { 
       "twitter": { 
        "twitter-username": "zzzz" 
       } 
      } 
     } 
    ] 
} 

내가 지금까지 무엇을 가지고 :

def smartpage_create_ajax(request): 

    if request.POST: 

     # get stuff and loop over each page? 

     return HttpResponse('done') 

답변

3

파이썬 인코딩 json를 제공/디코딩 JSON

import json 
json_dict = json.loads(request.POST['your_json_data']) 
json_dict['pages'] 

[ 
    { 
     "id": "1364484811734", 
     "name": "Page Name", 
     "type": "basic", 
     "components": { 
      "img": "", 
      "text": "" 
     } 
    }, 

    { 
     "name": "Twitter", 
     "type": "twitter", 
     "components": { 
      "twitter": { 
       "twitter-username": "zzzz" 
      } 
     } 
    }, 

    } 
] 
관련 문제