簡體   English   中英

將 python dict 翻譯成 json

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM