2016-01-13 84 views
0

我正在基於我知道的最終輸出應該在json中重寫視圖,但它將字典作爲字符串返回。python的Json格式

新的輸出

{ 
    "results": 
    [" 
    { 
     'plot': u'', 
     'runtime': u'N/A', 
     'description': u'x', 
     'videos': [ 
     { 
      'id': 823, 
      'name': u'x', 
      'youtube_id': u'FtcubOnXgZk' 
     } 
     ], 
     'country': u'India', 
     'writer': u'Neetu Varma, Ranjeev Verma', 
     'name': u'Chalk N Duster', 
     'id': 940, 
     'director': u'Jayant Gilatar', 
     'hot': True, 
     'content': u'x', 
     'actors': u'Shabana Azmi, Arya Babbar, Gavie Chahal, Juhi Chawla', 
     'year': 2015, 
     'images': [ 
     {'small': '/media/cache/62/fd/62fd5158d281c042e3cf1f919183e94e.jpg', 'medium': '/media/cache/5e/32/5e32ebb1a4d25bba0d0c70b4b448e948.jpg'}], 
     'trailer_youtube_id': u'FtcubOnXgZk', 
     'type': 'movie', 
     'slug': u'chalk-n-duster', 
     'categories': [{'parent_id': 2, 'id': 226, 'name': u'Drama'}], 
     'shows': { 
      'starts': '2016-01-16', 
      'booking_url': u'', 
      'venue': { 
       'address': u'', 
       'id': 854, 
       'name': u'Nyali Cinemax', 
       'area': { 
        'id': 52, 
        'parent': { 
         'id': 48, 
         'name': u'Mombasa' 
         }, 
        'name': u'Nyali' 
        } 
       }, 
       'starts_time': '18:30:00' 
       } 
      }", "{'plot': u'' .... 

舊輸出

"results": [ 
     { 
      "actors": "x", 
      "categories": [ 
       { 
        "id": 299, 
        "name": "Biography", 
        "parent_id": 2 
       }, 
      ], 
      "content": "x", 
      "country": "x", 
      "description": "x", 
      "director": "x", 
      "hot": true, 
      "id": 912, 
      "images": [ 
       { 
        "medium": "/media/cache/d2/b3/d2b3a7885e7c39bfc5c2b297b66619c5.jpg", 
        "small": "/media/cache/e2/d0/e2d01b2c7c77d3590536666de4a7fd7d.jpg" 
       } 
      ], 
      "name": "Bridge of Spies", 
      "plot": "x", 
      "runtime": "141 min", 
      "shows": [ 
       { 
        "booking_url": "", 
        "starts": "2015-11-27", 
        "starts_time": "16:30:00", 
        "venue": { 
         "address": "The Junction Shopping Mall", 
         "area": { 
          "id": 68, 
          "name": "Ngong Road", 
          "parent": { 
           "id": 2, 
           "name": "Nairobi" 
          } 
         }, 
         "id": 1631, 
         "name": "Century Cinemax Junction" 
        } 
       }, 
      ], 
      "slug": "bridge-of-spies", 
      "trailer_youtube_id": "", 
      "type": "movie", 
      "videos": [ 
       { 
        "id": "795", 
        "name": "Bridge of Spies", 
        "youtube_id": "2-2x3r1m2I4" 
       } 
      ], 
      "writer": "Matt Charman, Ethan Coen, Joel Coen", 
      "year": 2015 
     }, ... 

     ] 

這裏的看法,我知道節目也應該是一個名單,但爲了開始測試,我需要的數據來以正確的格式。如果涉及太多的重寫,我可以使用鏈接和解釋。

@memoize(timeout=60*60) 
def movies_json(): 
    today = datetime.date.today() 
    movies = Movie.objects.filter(shows__starts__gte=today) 
    results = [] 
    number = len(movies) 
    for movie in movies: 
     print "Now Remaining: {0}".format(number) 
     number -= 1 
     medium = get_thumbnail(movie.picture(), '185x274', crop='center', quality=99).url 
     small = get_thumbnail(movie.picture(), '50x74', crop='center', quality=99).url 
     movie_details = { 
      'director':movie.director, 
      'plot':movie.plot, 
      'actors':movie.actors, 
      'content':movie.content, 
      'country':movie.country, 
      'description':movie.description, 
      'hot':movie.hot, 
      'id':movie.id, 
      'images':[{'medium':medium, 'small':small}], 
      'name':movie.name, 
      'plot':movie.plot, 
      'runtime':movie.runtime, 
      'slug':movie.slug, 
      'type':'movie', 
      'writer':movie.writer, 
      'year':movie.year, 

     } 
     youtube_details = movie.videos.filter(youtube_id__isnull=False)[0] 
     movie_details['trailer_youtube_id'] = youtube_details.youtube_id if youtube_details.youtube_id else "" 
     movie_details['videos'] = [ 
      { 
       'id':youtube_details.id, 
       'name':movie.name, 
       'youtube_id':youtube_details.youtube_id, 
      } 
     ] 
     shows = [] 
     for show in movie.shows.all(): 
      show_details = { 
       'booking_url':show.booking_url, 
       'starts':show.starts.isoformat(), 
       'starts_time':show.starts_time.isoformat(), 
       'venue': { 
        'address':show.venue.address, 
        'area': { 
         'id': show.venue.area.id, 
         'name': show.venue.area.name, 
         'parent': { 
          'id': show.venue.area.parent.id, 
          'name': show.venue.area.parent.name, 
         } 
        }, 
        'id': show.venue.id, 
        'name': show.venue.name, 
       } 
      } 
      shows.append(show_details) 
     movie_details['shows'] = show_details 

     category_list = [] 
     for category in movie.categories.all(): 
      category_details = { 
       'id':category.id, 
       'name':category.name, 
       'parent_id':category.parent.id, 
      } 
      category_list.append(category_details) 
     movie_details['categories'] = category_list 

     results.append(movie_details) 
    return results 

的數據是由Django的REST框架0.4.0

+0

'返回json.dumps(結果)' –

+0

@KlausD返回。我應該添加它鏈接到django休息框架,我改變它這裏的結果 –

+0

>>> this = movies_json() >>> this '[{「trailer_youtube_id」:「vRnhEjP3R-c」,「plot」:「兩位姐妹決定在他們的父母出售家庭之前舉行最後一次家庭聚會。「,」運行時間「:」118分鐘「,」描述「:」兩個姐妹決定在父母出售家庭之前舉行最後一次家庭聚會。 「,」videos「:[{」youtube_id「:」vRnhEjP3R-c「,」id「:811,」name「:」Sisters「}],」country「:」USA「,」writer「:」Paula Pell劇本)「,」slu「」:「姐妹」,「導演」:「傑森摩爾」,「熱」:真實,「演員」:「艾米 –

回答

1
import json 
json_obj = json.load(json_string)