[日志] {"图像":" /SAS/default/download/uploads.image.85f2588e34848596.30362d32353033392e746966.tif"," filename":& #34; /SAS/default/download/06-25039.tif","开始":1437444049436,"宽度":1080,"高度&#34 ;: 734,"事件":[{"颜色":"#0000FF""宽度":3,"擦除&#34 ;:假,"点":[{" X" 795" Y":256,"时间":1437444050332},{ " X":754," Y":260,"时间":1437444050338},{" X":642," ý":271,"时间":1437444050355},{" X":466," Y" 291"时间" :1437444050372},{" X" 268" Y":318,"时间":1437444050389},{" X":148 " Y" 344"时间":1437444050406},{" X" 101" Y":359,&#34 ;时间":1437444050423},{" X" 92" Y":369,"时间":1437444050441},{" X& #34;:104," Y":377,"时间":1437444050458},{" X" 161" Y&#34 ;: 381,"时间":1437444050475},{&#3 4; X" 268" Y" 381"时间":1437444050492},{" X" 405" Y&# 34;:366,"时间":1437444050509},{" X" 513" Y":346,"时间":1437444050527 },{" X":557," Y":328,"时间":1437444050544},{" X":554,& #34; Y" 315"时间":1437444050562},{" X":529," Y" 305"时间& #34;:1437444050579},{" X":484," Y":301,"时间":1437444050596},{"#34 X& ;:435," Y":304,"时间":1437444050613},{" X":401," Y":316, "时间":1437444050630},{" X":388," Y" 329"时间":1437444050648},{&# 34; X":389," Y":342,"时间":1437444050665},{" X":406," Y&# 34;:356,"时间":1437444050682},{" X":430," Y":367,"时间":1437444050699 },{" X":449," Y":370,"时间":1437444050716},{" X":457,& #34; Y":370,"时间":1437444050733},{" X":458," Y":370,"时间& #34;:14374440507 51},{" X":457," Y":368,"时间":1437444050769}]}],"端&#34 ;: 1437444051345,"已过去":1909}(实验,第164行)
这是我需要保存的字符串。我必须执行两项任务:
将字符串保存在系统上并将其显示为视图(原样)
仅显示特定参数。如"已过去"在此字符串中作为视图。
我该怎么做? 谢谢您的帮助! :)
答案 0 :(得分:0)
根据您希望显示JSON字符串的内容/方式,您有一些不同的选项。如果要显示存储在变量strJSON中的原始JSON字符串,可以使用{{= strJSON}}将其注入适当位置的视图中。 web2py有一个内置的方法BEAUTIFY,它使注入的变量输出看起来更好,即你可以调用{{= strJSON}}来利用它。
如果您只想打印某些元素和/或手动格式化它,就像在第二个问题中那样,我建议将JSON字符串转换回Python对象。 web2py有一个名为SimpleJSON的模块,可以为您完成所有JSON编码/解码魔术。它位于gluon.contrib.simplejson。要将JSON字符串转换为对象,您可以使用函数' loads'。要将Python对象转换为JSON字符串,请使用'转储'。所以,例如:
{{import gluon.contrib.simplejson as sjson}}
{{myObj = sjson.loads(strJSON)}}
{{strJSON = sjson.dumps(myObj)}}
既然您的JSON字符串存储为对象,您可以根据您的对象是列表,字典等来访问Python中通常会使用的元素。例如,如果我们从屏幕截图中转换JSON在一个对象中,我们可以在Python中访问图像名称:
imgname = myObj [" image"]
同样地,您可以通过以下方式访问第一个事件:
my_events = myObj [" events"] [0]
这是' color',' width'等的字典