如何將 dict 轉儲到 JSON 文件? - How to dump a dict to a JSON file?

問題:

I have a dict like this:我有一個這樣的字典:

sample = {'ObjectInterpolator': 1629,  'PointInterpolator': 1675, 'RectangleInterpolator': 2042}

I can't figure out how to dump the dict to a JSON file as showed below:我不知道如何將 dict 轉儲到 JSON 文件,如下所示:

{      
    "name": "interpolator",
    "children": [
      {"name": "ObjectInterpolator", "size": 1629},
      {"name": "PointInterpolator", "size": 1675},
      {"name": "RectangleInterpolator", "size": 2042}
     ]
}

Is there a pythonic way to do this?有沒有一種pythonic的方法來做到這一點?

You may guess that I want to generate a d3 treemap.你可能猜到我想生成一個d3樹狀圖。


解決方案:

參考一: https://en.stackoom.com/question/19Vsy
參考二: https://stackoom.com/question/19Vsy
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章