[英]Translate python dict to json
我正在嘗試使用 jinja2 將 python dict 遍歷到 json。 我有以下python結構
examples:
ex1: example1
ex2: example2
使用以下 jinja2:
examples= [{{(examples|default({}))|tojson}}]
我正在實現
examples=[{"ex1":"example1","ex2":"example2"}]
我怎樣才能達到
examples=[{"ex1":"example1"},{"ex2":"example2"}]
就像是
examples = {'ex1': 'example1', 'ex2': 'example2'}
out = []
for k,e in examples.items():
out.append({k:e})
print(out)
如果真的需要,可以調用 json 嗎?
我必須在我的鍵之前放入 - 才能將其創建為數組。
examples:
- ex1: example1
- ex2: example2
所以現在我的代碼給出了兩個對象的結果,如下所示
examples=·[{"ex1":"example1"},{"ex2":"example2"}]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.