1. 导入json库
2. 构建字典表,接入数据
3. 使用 json.dumps 转换
代码如下:
>>> import json
>>> data = {}
>>> data["directed"]= True
>>> data["multigraph"] = False
>>> data["graph"] = {}
>>> data["node"]=[{"id":0},{"id":1}]
>>>
>>> data["links"]=[{"source":0,"target":1}]
>>> jdata = json.dumps(data)
>>> print(jdata)
{"directed": true, "graph": {}, "node": [{"id": 0}, {"id": 1}], "links": [{"source": 0, "target": 1}], "multigraph": false}