鉴于下面的JSON结构,我想从项目中获取所有数据,然后随机拉出link
的一个值。
使用random
- 我熟悉从列表中获取随机值。我被困在哪里是如何从列表中列出一个列表。请参阅下面的代码JSON。
Item0:
0
caption: "caption 0"
link: "www.item0.com"
type: "type0"
1
caption: "caption 1"
link: "www.item1.com"
type: "type1"
PYTHON:
chosen_item = "Item0"
firebase = firebase.FirebaseApplication('https://app.firebaseio.com')
result = firebase.get(chosen_item, None)
if result:
for i in result:
result_link = (i['link'])
print result_link
if-statement
循环播放,并将项目中的link
作为unicode
类型返回。我如何从中列出一个列表? (result_link
)
答案 0 :(得分:2)
chosen_item = "Item0"
firebase = firebase.FirebaseApplication('https://app.firebaseio.com')
result = firebase.get(chosen_item, None)
if result:
links = []
for i in result:
links.append(i['link'])
答案 1 :(得分:1)
result_links = [x for x in result['link']]
如果您需要使用索引号,请尝试以下方法:
result_links = []
i = 0
for item in results:
result_links.append(item[i]['link'])
json_index +=1