解码 json 并迭代 django 模板中的项目

发布于 2024-10-22 08:25:34 字数 11090 浏览 4 评论 0原文

您好,我正在使用 simplejson 导入一些 json,然后进行解码以在 django 模板中使用,

这是解码后的 json:

{u'ServerID': 1, u'Cache': {u'CacheBusted': False, u'FromCache': True}, u'Result': [{u'Url': u'http://listen.grooveshark.com/playlist/Soul_Power/399134', u'Cache': {u'FromCache': True}, u'PlaylistID': u'399134', u'Name': u'Soul Power', u'ModifiedTime': u'1229427645'}, {u'Url': u'http://listen.grooveshark.com/playlist/4RK_(v.3)/491934', u'Cache': {u'FromCache': True}, u'PlaylistID': u'491934', u'Name': u'4RK (v.3)', u'ModifiedTime': u'1231028348'}, {u'Url': u'http://listen.grooveshark.com/playlist/My_Bands/3735842', u'Cache': {u'FromCache': True}, u'PlaylistID': u'3735842', u'Name': u'My Bands', u'ModifiedTime': u'1234384383'}, {u'Url': u'http://listen.grooveshark.com/playlist/Go_Move_Cruise/19255842', u'Cache': {u'FromCache': True}, u'PlaylistID': u'19255842', u'Name': u'Go Move Cruise', u'ModifiedTime': u'1259081222'}, {u'Url': u'http://listen.grooveshark.com/playlist/Goodnight/19854340', u'Cache': {u'FromCache': True}, u'PlaylistID': u'19854340', u'Name': u'Goodnight', u'ModifiedTime': u'1259457504'}, {u'Url': u"http://listen.grooveshark.com/playlist/Bottle_Poppin'_with_Friends_&_Scarfs/22398199", u'Cache': {u'FromCache': True}, u'PlaylistID': u'22398199', u'Name': u"Bottle Poppin' with Friends & Scarfs", u'ModifiedTime': u'1262525701'}, {u'Url': u'http://listen.grooveshark.com/playlist/Primer/23525296', u'Cache': {u'FromCache': True}, u'PlaylistID': u'23525296', u'Name': u'Primer', u'ModifiedTime': u'1264372296'}, {u'Url': u'http://listen.grooveshark.com/playlist/The_Snake_The_Cross_The_Crown/23863213', u'Cache': {u'FromCache': True}, u'PlaylistID': u'23863213', u'Name': u'The Snake The Cross The Crown', u'ModifiedTime': u'1264884692'}, {u'Url': u'http://listen.grooveshark.com/playlist/Ward/24957740', u'Cache': {u'FromCache': True}, u'PlaylistID': u'24957740', u'Name': u'Ward', u'ModifiedTime': u'1265843831'}, {u'Url': u'http://listen.grooveshark.com/playlist/NeuroLinguistic_Programming/26526478', u'Cache': {u'FromCache': True}, u'PlaylistID': u'26526478', u'Name': u'Neuro-Linguistic Programming', u'ModifiedTime': u'1268957617'}, {u'Url': u'http://listen.grooveshark.com/playlist/ISO50_Playlist_#8/27319532', u'Cache': {u'FromCache': True}, u'PlaylistID': u'27319532', u'Name': u'ISO50 Playlist #8', u'ModifiedTime': u'1270560957'}, {u'Url': u'http://listen.grooveshark.com/playlist/Caribou_Swim/27959307', u'Cache': {u'FromCache': True}, u'PlaylistID': u'27959307', u'Name': u'Caribou - Swim', u'ModifiedTime': u'1271811201'}, {u'Url': u'http://listen.grooveshark.com/playlist/Del/28944659', u'Cache': {u'FromCache': True}, u'PlaylistID': u'28944659', u'Name': u'Del', u'ModifiedTime': u'1273576988'}, {u'Url': u'http://listen.grooveshark.com/playlist/ISO50_Playlist_#7/29740328', u'Cache': {u'FromCache': True}, u'PlaylistID': u'29740328', u'Name': u'ISO50 Playlist #7', u'ModifiedTime': u'1274878455'}, {u'Url': u'http://listen.grooveshark.com/playlist/Axe_Atac/29827929', u'Cache': {u'FromCache': True}, u'PlaylistID': u'29827929', u'Name': u'Axe A-tac', u'ModifiedTime': u'1274999610'}, {u'Url': u'http://listen.grooveshark.com/playlist/Finding_Candy/31280614', u'Cache': {u'FromCache': True}, u'PlaylistID': u'31280614', u'Name': u'Finding Candy', u'ModifiedTime': u'1277587721'}, {u'Url': u'http://listen.grooveshark.com/playlist/Tobacco_Fingerlicked_Up_Friends/32410276', u'Cache': {u'FromCache': True}, u'PlaylistID': u'32410276', u'Name': u'Tobacco - Fingerlicked Up Friends', u'ModifiedTime': u'1279650743'}, {u'Url': u'http://listen.grooveshark.com/playlist/Broken_Social_Scene_Forgiveness_Rock_Band/32410310', u'Cache': {u'FromCache': True}, u'PlaylistID': u'32410310', u'Name': u'Broken Social Scene - Forgiveness Rock Band', u'ModifiedTime': u'1279650795'}, {u'Url': u'http://listen.grooveshark.com/playlist/Yeasayer_All_Hour_Cymbals/32410328', u'Cache': {u'FromCache': True}, u'PlaylistID': u'32410328', u'Name': u'Yeasayer - All Hour Cymbals', u'ModifiedTime': u'1279650820'}, {u'Url': u'http://listen.grooveshark.com/playlist/Yeasayer_Odd_Blood/32410341', u'Cache': {u'FromCache': True}, u'PlaylistID': u'32410341', u'Name': u'Yeasayer - Odd Blood', u'ModifiedTime': u'1279650839'}, {u'Url': u'http://listen.grooveshark.com/playlist/The_Chemical_Brothers_Further/32410395', u'Cache': {u'FromCache': True}, u'PlaylistID': u'32410395', u'Name': u'The Chemical Brothers - Further', u'ModifiedTime': u'1279650906'}, {u'Url': u'http://listen.grooveshark.com/playlist/Why?_Alopecia/32694517', u'Cache': {u'FromCache': True}, u'PlaylistID': u'32694517', u'Name': u'Why? - Alopecia', u'ModifiedTime': u'1280139036'}, {u'Url': u'http://listen.grooveshark.com/playlist/Ben_Frost_By_The_Throat/34275177', u'Cache': {u'FromCache': True}, u'PlaylistID': u'34275177', u'Name': u'Ben Frost - By The Throat', u'ModifiedTime': u'1282579712'}, {u'Url': u'http://listen.grooveshark.com/playlist/Digitalism_Idealism/34422107', u'Cache': {u'FromCache': True}, u'PlaylistID': u'34422107', u'Name': u'Digitalism - Idealism', u'ModifiedTime': u'1282771579'}, {u'Url': u'http://listen.grooveshark.com/playlist/Bun_B_Trill_O.G._[Deluxe_Edition]/34914808', u'Cache': {u'FromCache': True}, u'PlaylistID': u'34914808', u'Name': u'Bun B - Trill O.G. [Deluxe Edition]', u'ModifiedTime': u'1283438827'}, {u'Url': u'http://listen.grooveshark.com/playlist/Simian_Mobile_Disco_Attack_Decay_Sustain_Release/35054649', u'Cache': {u'FromCache': False}, u'PlaylistID': u'35054649', u'Name': u'Simian Mobile Disco - Attack Decay Sustain Release', u'ModifiedTime': u'1283640118'}, {u'Url': u'http://listen.grooveshark.com/playlist/Salem_King_Knight/35268471', u'Cache': {u'FromCache': True}, u'PlaylistID': u'35268471', u'Name': u'Salem - King Knight', u'ModifiedTime': u'1283972703'}, {u'Url': u'http://listen.grooveshark.com/playlist/Maxence_Cyrin_Novo_Piano/36527813', u'Cache': {u'FromCache': True}, u'PlaylistID': u'36527813', u'Name': u'Maxence Cyrin - Novo Piano', u'ModifiedTime': u'1285872470'}, {u'Url': u'http://listen.grooveshark.com/playlist/Fingerlick_Buttons_Tarot_Sport/33635471', u'Cache': {u'FromCache': True}, u'PlaylistID': u'33635471', u'Name': u'Fingerlick Buttons - Tarot Sport', u'ModifiedTime': u'1281633330'}, {u'Url': u'http://listen.grooveshark.com/playlist/Scrappy/37988906', u'Cache': {u'FromCache': True}, u'PlaylistID': u'37988906', u'Name': u'Scrappy', u'ModifiedTime': u'1287967005'}, {u'Url': u'http://listen.grooveshark.com/playlist/The_Green_Album_Omer_Saar/40654922', u'Cache': {u'FromCache': True}, u'PlaylistID': u'40654922', u'Name': u'The Green Album - Omer Saar', u'ModifiedTime': u'1291818391'}, {u'Url': u'http://listen.grooveshark.com/playlist/Murdered_Out/7479852', u'Cache': {u'FromCache': True}, u'PlaylistID': u'7479852', u'Name': u'Murdered Out', u'ModifiedTime': u'1236897259'}, {u'Url': u'http://listen.grooveshark.com/playlist/Hot_and_Tired/27931954', u'Cache': {u'FromCache': True}, u'PlaylistID': u'27931954', u'Name': u'Hot and Tired', u'ModifiedTime': u'1271772133'}, {u'Url': u'http://listen.grooveshark.com/playlist/SawTooth_Bass/18284890', u'Cache': {u'FromCache': False}, u'PlaylistID': u'18284890', u'Name': u'Saw-Tooth Bass', u'ModifiedTime': u'1256910446'}, {u'Url': u'http://listen.grooveshark.com/playlist//\\/\\/\\/\\/\\/\\//27216709', u'Cache': {u'FromCache': False}, u'PlaylistID': u'27216709', u'Name': u'/\\/\\/\\/\\/\\/\\/', u'ModifiedTime': u'1270332371'}, {u'Url': u'http://listen.grooveshark.com/playlist/ISO50_Playlist_#10/38892561', u'Cache': {u'FromCache': True}, u'PlaylistID': u'38892561', u'Name': u'ISO50 Playlist #10', u'ModifiedTime': u'1289155227'}, {u'Url': u'http://listen.grooveshark.com/playlist/Chill_Rap/4516', u'Cache': {u'FromCache': True}, u'PlaylistID': u'4516', u'Name': u'Chill Rap', u'ModifiedTime': u'1201712529'}, {u'Url': u'http://listen.grooveshark.com/playlist/The_Basement/29007410', u'Cache': {u'FromCache': True}, u'PlaylistID': u'29007410', u'Name': u'The Basement', u'ModifiedTime': u'1273668603'}, {u'Url': u'http://listen.grooveshark.com/playlist/Fingerlicking_Awesome_Mashups/36954458', u'Cache': {u'FromCache': True}, u'PlaylistID': u'36954458', u'Name': u'Fingerlicking Awesome Mashups', u'ModifiedTime': u'1286489257'}, {u'Url': u'http://listen.grooveshark.com/playlist/The_Augustine/29939116', u'Cache': {u'FromCache': True}, u'PlaylistID': u'29939116', u'Name': u'The Augustine', u'ModifiedTime': u'1275215560'}, {u'Url': u'http://listen.grooveshark.com/playlist/Best_Of_Wu/15335431', u'Cache': {u'FromCache': True}, u'PlaylistID': u'15335431', u'Name': u'Best Of Wu', u'ModifiedTime': u'1250298301'}, {u'Url': u'http://listen.grooveshark.com/playlist/ISO50_Presents:_Dry_Waves/40210604', u'Cache': {u'FromCache': True}, u'PlaylistID': u'40210604', u'Name': u'ISO50 Presents: Dry Waves', u'ModifiedTime': u'1291138412'}, {u'Url': u'http://listen.grooveshark.com/playlist/ReUp_Gang_We_Got_It_4_Cheap_Volume_3/43589616', u'Cache': {u'FromCache': True}, u'PlaylistID': u'43589616', u'Name': u'Re-Up Gang - We Got It 4 Cheap Volume 3', u'ModifiedTime': u'1297612447'}, {u'Url': u'http://listen.grooveshark.com/playlist/ISO50_Playlist_#9/35248549', u'Cache': {u'FromCache': True}, u'PlaylistID': u'35248549', u'Name': u'ISO50 Playlist #9', u'ModifiedTime': u'1283952285'}, {u'Url': u'http://listen.grooveshark.com/playlist/PostRock_Playboy/6183030', u'Cache': {u'FromCache': True}, u'PlaylistID': u'6183030', u'Name': u'Post-Rock Playboy', u'ModifiedTime': u'1236008915'}, {u'Url': u'http://listen.grooveshark.com/playlist/Ambient_Electronic/376180', u'Cache': {u'FromCache': True}, u'PlaylistID': u'376180', u'Name': u'Ambient Electronic', u'ModifiedTime': u'1229023391'}, {u'Url': u'http://listen.grooveshark.com/playlist/Man_On_The_Moon_II:_The_Legend_Of_Mr._Rager/38994029', u'Cache': {u'FromCache': True}, u'PlaylistID': u'38994029', u'Name': u'Man On The Moon II: The Legend Of Mr. Rager', u'ModifiedTime': u'1289301711'}, {u'Url': u'http://listen.grooveshark.com/playlist/Ghostface_Killah_Apollo_Kids/43155947', u'Cache': {u'FromCache': True}, u'PlaylistID': u'43155947', u'Name': u'Ghostface Killah - Apollo Kids', u'ModifiedTime': u'1296741352'}, {u'Url': u'http://listen.grooveshark.com/playlist/All_Funk/2528', u'Cache': {u'FromCache': True}, u'PlaylistID': u'2528', u'Name': u'All Funk', u'ModifiedTime': u'1195084800'}, {u'Url': u'http://listen.grooveshark.com/playlist/Bearbot_Evocation/39236538', u'Cache': {u'FromCache': True}, u'PlaylistID': u'39236538', u'Name': u'Bearbot - Evocation', u'ModifiedTime': u'1289595559'}], u'Success': True, u'RateLimit': {u'CacheBusts': 0, u'ResetTime': 1300238204, u'CallsRemaining': 151, u'CallsMade': 48, u'RemainingSecs': 1256}}

结果中的每个项目我需要以下内容:

• Url • 播放列表ID • 字典中的名称

,但我想知道在将数据传递到 django 模板之前是否需要在 view.py 中整理/排序这些数据?然后我如何循环遍历这个以便我得到

  • Result1: URL, PlaylistID, Name
  • Result2: URL, PlaylistID, Name
  • Result3: URL, PlaylistID, Name
  • Result4: URL, PlaylistID, Name ...etc

我正在学习 python 所以任何感谢帮助或建议。

谢谢 乔恩

Hi I am using simplejson to import some json and then decode for use within a django template,

this is the decoded json:

{u'ServerID': 1, u'Cache': {u'CacheBusted': False, u'FromCache': True}, u'Result': [{u'Url': u'http://listen.grooveshark.com/playlist/Soul_Power/399134', u'Cache': {u'FromCache': True}, u'PlaylistID': u'399134', u'Name': u'Soul Power', u'ModifiedTime': u'1229427645'}, {u'Url': u'http://listen.grooveshark.com/playlist/4RK_(v.3)/491934', u'Cache': {u'FromCache': True}, u'PlaylistID': u'491934', u'Name': u'4RK (v.3)', u'ModifiedTime': u'1231028348'}, {u'Url': u'http://listen.grooveshark.com/playlist/My_Bands/3735842', u'Cache': {u'FromCache': True}, u'PlaylistID': u'3735842', u'Name': u'My Bands', u'ModifiedTime': u'1234384383'}, {u'Url': u'http://listen.grooveshark.com/playlist/Go_Move_Cruise/19255842', u'Cache': {u'FromCache': True}, u'PlaylistID': u'19255842', u'Name': u'Go Move Cruise', u'ModifiedTime': u'1259081222'}, {u'Url': u'http://listen.grooveshark.com/playlist/Goodnight/19854340', u'Cache': {u'FromCache': True}, u'PlaylistID': u'19854340', u'Name': u'Goodnight', u'ModifiedTime': u'1259457504'}, {u'Url': u"http://listen.grooveshark.com/playlist/Bottle_Poppin'_with_Friends_&_Scarfs/22398199", u'Cache': {u'FromCache': True}, u'PlaylistID': u'22398199', u'Name': u"Bottle Poppin' with Friends & Scarfs", u'ModifiedTime': u'1262525701'}, {u'Url': u'http://listen.grooveshark.com/playlist/Primer/23525296', u'Cache': {u'FromCache': True}, u'PlaylistID': u'23525296', u'Name': u'Primer', u'ModifiedTime': u'1264372296'}, {u'Url': u'http://listen.grooveshark.com/playlist/The_Snake_The_Cross_The_Crown/23863213', u'Cache': {u'FromCache': True}, u'PlaylistID': u'23863213', u'Name': u'The Snake The Cross The Crown', u'ModifiedTime': u'1264884692'}, {u'Url': u'http://listen.grooveshark.com/playlist/Ward/24957740', u'Cache': {u'FromCache': True}, u'PlaylistID': u'24957740', u'Name': u'Ward', u'ModifiedTime': u'1265843831'}, {u'Url': u'http://listen.grooveshark.com/playlist/NeuroLinguistic_Programming/26526478', u'Cache': {u'FromCache': True}, u'PlaylistID': u'26526478', u'Name': u'Neuro-Linguistic Programming', u'ModifiedTime': u'1268957617'}, {u'Url': u'http://listen.grooveshark.com/playlist/ISO50_Playlist_#8/27319532', u'Cache': {u'FromCache': True}, u'PlaylistID': u'27319532', u'Name': u'ISO50 Playlist #8', u'ModifiedTime': u'1270560957'}, {u'Url': u'http://listen.grooveshark.com/playlist/Caribou_Swim/27959307', u'Cache': {u'FromCache': True}, u'PlaylistID': u'27959307', u'Name': u'Caribou - Swim', u'ModifiedTime': u'1271811201'}, {u'Url': u'http://listen.grooveshark.com/playlist/Del/28944659', u'Cache': {u'FromCache': True}, u'PlaylistID': u'28944659', u'Name': u'Del', u'ModifiedTime': u'1273576988'}, {u'Url': u'http://listen.grooveshark.com/playlist/ISO50_Playlist_#7/29740328', u'Cache': {u'FromCache': True}, u'PlaylistID': u'29740328', u'Name': u'ISO50 Playlist #7', u'ModifiedTime': u'1274878455'}, {u'Url': u'http://listen.grooveshark.com/playlist/Axe_Atac/29827929', u'Cache': {u'FromCache': True}, u'PlaylistID': u'29827929', u'Name': u'Axe A-tac', u'ModifiedTime': u'1274999610'}, {u'Url': u'http://listen.grooveshark.com/playlist/Finding_Candy/31280614', u'Cache': {u'FromCache': True}, u'PlaylistID': u'31280614', u'Name': u'Finding Candy', u'ModifiedTime': u'1277587721'}, {u'Url': u'http://listen.grooveshark.com/playlist/Tobacco_Fingerlicked_Up_Friends/32410276', u'Cache': {u'FromCache': True}, u'PlaylistID': u'32410276', u'Name': u'Tobacco - Fingerlicked Up Friends', u'ModifiedTime': u'1279650743'}, {u'Url': u'http://listen.grooveshark.com/playlist/Broken_Social_Scene_Forgiveness_Rock_Band/32410310', u'Cache': {u'FromCache': True}, u'PlaylistID': u'32410310', u'Name': u'Broken Social Scene - Forgiveness Rock Band', u'ModifiedTime': u'1279650795'}, {u'Url': u'http://listen.grooveshark.com/playlist/Yeasayer_All_Hour_Cymbals/32410328', u'Cache': {u'FromCache': True}, u'PlaylistID': u'32410328', u'Name': u'Yeasayer - All Hour Cymbals', u'ModifiedTime': u'1279650820'}, {u'Url': u'http://listen.grooveshark.com/playlist/Yeasayer_Odd_Blood/32410341', u'Cache': {u'FromCache': True}, u'PlaylistID': u'32410341', u'Name': u'Yeasayer - Odd Blood', u'ModifiedTime': u'1279650839'}, {u'Url': u'http://listen.grooveshark.com/playlist/The_Chemical_Brothers_Further/32410395', u'Cache': {u'FromCache': True}, u'PlaylistID': u'32410395', u'Name': u'The Chemical Brothers - Further', u'ModifiedTime': u'1279650906'}, {u'Url': u'http://listen.grooveshark.com/playlist/Why?_Alopecia/32694517', u'Cache': {u'FromCache': True}, u'PlaylistID': u'32694517', u'Name': u'Why? - Alopecia', u'ModifiedTime': u'1280139036'}, {u'Url': u'http://listen.grooveshark.com/playlist/Ben_Frost_By_The_Throat/34275177', u'Cache': {u'FromCache': True}, u'PlaylistID': u'34275177', u'Name': u'Ben Frost - By The Throat', u'ModifiedTime': u'1282579712'}, {u'Url': u'http://listen.grooveshark.com/playlist/Digitalism_Idealism/34422107', u'Cache': {u'FromCache': True}, u'PlaylistID': u'34422107', u'Name': u'Digitalism - Idealism', u'ModifiedTime': u'1282771579'}, {u'Url': u'http://listen.grooveshark.com/playlist/Bun_B_Trill_O.G._[Deluxe_Edition]/34914808', u'Cache': {u'FromCache': True}, u'PlaylistID': u'34914808', u'Name': u'Bun B - Trill O.G. [Deluxe Edition]', u'ModifiedTime': u'1283438827'}, {u'Url': u'http://listen.grooveshark.com/playlist/Simian_Mobile_Disco_Attack_Decay_Sustain_Release/35054649', u'Cache': {u'FromCache': False}, u'PlaylistID': u'35054649', u'Name': u'Simian Mobile Disco - Attack Decay Sustain Release', u'ModifiedTime': u'1283640118'}, {u'Url': u'http://listen.grooveshark.com/playlist/Salem_King_Knight/35268471', u'Cache': {u'FromCache': True}, u'PlaylistID': u'35268471', u'Name': u'Salem - King Knight', u'ModifiedTime': u'1283972703'}, {u'Url': u'http://listen.grooveshark.com/playlist/Maxence_Cyrin_Novo_Piano/36527813', u'Cache': {u'FromCache': True}, u'PlaylistID': u'36527813', u'Name': u'Maxence Cyrin - Novo Piano', u'ModifiedTime': u'1285872470'}, {u'Url': u'http://listen.grooveshark.com/playlist/Fingerlick_Buttons_Tarot_Sport/33635471', u'Cache': {u'FromCache': True}, u'PlaylistID': u'33635471', u'Name': u'Fingerlick Buttons - Tarot Sport', u'ModifiedTime': u'1281633330'}, {u'Url': u'http://listen.grooveshark.com/playlist/Scrappy/37988906', u'Cache': {u'FromCache': True}, u'PlaylistID': u'37988906', u'Name': u'Scrappy', u'ModifiedTime': u'1287967005'}, {u'Url': u'http://listen.grooveshark.com/playlist/The_Green_Album_Omer_Saar/40654922', u'Cache': {u'FromCache': True}, u'PlaylistID': u'40654922', u'Name': u'The Green Album - Omer Saar', u'ModifiedTime': u'1291818391'}, {u'Url': u'http://listen.grooveshark.com/playlist/Murdered_Out/7479852', u'Cache': {u'FromCache': True}, u'PlaylistID': u'7479852', u'Name': u'Murdered Out', u'ModifiedTime': u'1236897259'}, {u'Url': u'http://listen.grooveshark.com/playlist/Hot_and_Tired/27931954', u'Cache': {u'FromCache': True}, u'PlaylistID': u'27931954', u'Name': u'Hot and Tired', u'ModifiedTime': u'1271772133'}, {u'Url': u'http://listen.grooveshark.com/playlist/SawTooth_Bass/18284890', u'Cache': {u'FromCache': False}, u'PlaylistID': u'18284890', u'Name': u'Saw-Tooth Bass', u'ModifiedTime': u'1256910446'}, {u'Url': u'http://listen.grooveshark.com/playlist//\\/\\/\\/\\/\\/\\//27216709', u'Cache': {u'FromCache': False}, u'PlaylistID': u'27216709', u'Name': u'/\\/\\/\\/\\/\\/\\/', u'ModifiedTime': u'1270332371'}, {u'Url': u'http://listen.grooveshark.com/playlist/ISO50_Playlist_#10/38892561', u'Cache': {u'FromCache': True}, u'PlaylistID': u'38892561', u'Name': u'ISO50 Playlist #10', u'ModifiedTime': u'1289155227'}, {u'Url': u'http://listen.grooveshark.com/playlist/Chill_Rap/4516', u'Cache': {u'FromCache': True}, u'PlaylistID': u'4516', u'Name': u'Chill Rap', u'ModifiedTime': u'1201712529'}, {u'Url': u'http://listen.grooveshark.com/playlist/The_Basement/29007410', u'Cache': {u'FromCache': True}, u'PlaylistID': u'29007410', u'Name': u'The Basement', u'ModifiedTime': u'1273668603'}, {u'Url': u'http://listen.grooveshark.com/playlist/Fingerlicking_Awesome_Mashups/36954458', u'Cache': {u'FromCache': True}, u'PlaylistID': u'36954458', u'Name': u'Fingerlicking Awesome Mashups', u'ModifiedTime': u'1286489257'}, {u'Url': u'http://listen.grooveshark.com/playlist/The_Augustine/29939116', u'Cache': {u'FromCache': True}, u'PlaylistID': u'29939116', u'Name': u'The Augustine', u'ModifiedTime': u'1275215560'}, {u'Url': u'http://listen.grooveshark.com/playlist/Best_Of_Wu/15335431', u'Cache': {u'FromCache': True}, u'PlaylistID': u'15335431', u'Name': u'Best Of Wu', u'ModifiedTime': u'1250298301'}, {u'Url': u'http://listen.grooveshark.com/playlist/ISO50_Presents:_Dry_Waves/40210604', u'Cache': {u'FromCache': True}, u'PlaylistID': u'40210604', u'Name': u'ISO50 Presents: Dry Waves', u'ModifiedTime': u'1291138412'}, {u'Url': u'http://listen.grooveshark.com/playlist/ReUp_Gang_We_Got_It_4_Cheap_Volume_3/43589616', u'Cache': {u'FromCache': True}, u'PlaylistID': u'43589616', u'Name': u'Re-Up Gang - We Got It 4 Cheap Volume 3', u'ModifiedTime': u'1297612447'}, {u'Url': u'http://listen.grooveshark.com/playlist/ISO50_Playlist_#9/35248549', u'Cache': {u'FromCache': True}, u'PlaylistID': u'35248549', u'Name': u'ISO50 Playlist #9', u'ModifiedTime': u'1283952285'}, {u'Url': u'http://listen.grooveshark.com/playlist/PostRock_Playboy/6183030', u'Cache': {u'FromCache': True}, u'PlaylistID': u'6183030', u'Name': u'Post-Rock Playboy', u'ModifiedTime': u'1236008915'}, {u'Url': u'http://listen.grooveshark.com/playlist/Ambient_Electronic/376180', u'Cache': {u'FromCache': True}, u'PlaylistID': u'376180', u'Name': u'Ambient Electronic', u'ModifiedTime': u'1229023391'}, {u'Url': u'http://listen.grooveshark.com/playlist/Man_On_The_Moon_II:_The_Legend_Of_Mr._Rager/38994029', u'Cache': {u'FromCache': True}, u'PlaylistID': u'38994029', u'Name': u'Man On The Moon II: The Legend Of Mr. Rager', u'ModifiedTime': u'1289301711'}, {u'Url': u'http://listen.grooveshark.com/playlist/Ghostface_Killah_Apollo_Kids/43155947', u'Cache': {u'FromCache': True}, u'PlaylistID': u'43155947', u'Name': u'Ghostface Killah - Apollo Kids', u'ModifiedTime': u'1296741352'}, {u'Url': u'http://listen.grooveshark.com/playlist/All_Funk/2528', u'Cache': {u'FromCache': True}, u'PlaylistID': u'2528', u'Name': u'All Funk', u'ModifiedTime': u'1195084800'}, {u'Url': u'http://listen.grooveshark.com/playlist/Bearbot_Evocation/39236538', u'Cache': {u'FromCache': True}, u'PlaylistID': u'39236538', u'Name': u'Bearbot - Evocation', u'ModifiedTime': u'1289595559'}], u'Success': True, u'RateLimit': {u'CacheBusts': 0, u'ResetTime': 1300238204, u'CallsRemaining': 151, u'CallsMade': 48, u'RemainingSecs': 1256}}

I need the following for each item in result:

• Url
• PlaylistID
• Name

from the dictionary, but I was wondering if I need to tidy/sort this data in my views.py before I pass the data to the django template? and then how do I loop through this so I get

  • Result1: URL, PlaylistID, Name
  • Result2: URL, PlaylistID, Name
  • Result3: URL, PlaylistID, Name
  • Result4: URL, PlaylistID, Name ...etc

I am just learning python so any help or suggestions are appreciated.

thanks
Jon

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一页 2024-10-29 08:25:34

一旦您使用 Python 的 jsonsimplejson 模块将 JSON 数据加载到 Python 对象中,一切都应该在您的模板中正常工作。

在将内容发送到您的模板之前,我会像这样提取结果...

def foo_view(request):
    ....
    decoded_json = json.loads(json_string)
    return render_to_response('foo.html',{'results':decoded_json['Result']})

这样,在您的模板中,您将能够像这样处理每个结果...

<ul id="results">
     {% for result in results %}
     <li>Result{{ forloop.counter }}: {{ result.URL }}, {{ result.PlaylistID }}, {{ result.Name }} ...</li>
     {% endfor %}
</ul>

输出中的数据将以与它相同的顺序出现在 Results 处的 JSON 数组中执行操作。如果您需要对数据进行排序,那么您将需要在您的视图中执行此操作,而不是在模板中。

Once you've used Python's json or simplejson module to load the JSON data into Python objects, everything should just work in your template.

Before sending things to your template I would pull out the results like so...

def foo_view(request):
    ....
    decoded_json = json.loads(json_string)
    return render_to_response('foo.html',{'results':decoded_json['Result']})

That way in your template you'll be able to work on each result like so...

<ul id="results">
     {% for result in results %}
     <li>Result{{ forloop.counter }}: {{ result.URL }}, {{ result.PlaylistID }}, {{ result.Name }} ...</li>
     {% endfor %}
</ul>

The data in the output will appear in the same order as it did in the JSON array at Results. If you need to sort the data then you will need to do that in your view, NOT in your template.

嘿咻 2024-10-29 08:25:34

在将字典发送到 Django 之前,您不需要在 Python 中进行任何进一步的预处理(超出您已经完成的操作;使用 simplejson 将 JSON 字符串解析为字典)。 Django 的模板系统非常擅长处理字典和列表。

我假设您已将字典传递给名为 obj 的变量中的 Django。

您应该能够使用 for 循环访问字典的结果部分中的每个项目:

<ul>
{% for result in obj.Result %}
    <li>{{ result.Url }}, {{ result.PlaylistID }}, {{ result.Name }}</li>
{% endfor %}
</ul>

例如,将结果放入项目符号列表中,并将 Url、播放列表和名称放在逗号分隔的列表中。

You don't need to do any further pre-processing in Python before sending the dictionary to Django (beyond what you have already done; using simplejson to parse the JSON string into a dictionary). Django's templating system is very good at dealing with dictionaries and lists.

I will assume you have passed the dictionary to Django in a variable called obj.

You should be able to access each item in the Result part of the dictionary using a for loop:

<ul>
{% for result in obj.Result %}
    <li>{{ result.Url }}, {{ result.PlaylistID }}, {{ result.Name }}</li>
{% endfor %}
</ul>

For example, will place the results in a bulleted list, with the Url, Playlist and Name in a comma-separated list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文