我想在Python中构造一个dict,json.dumps(arg)将转换为以下JSON结构:
"{\"type\":\"id\",
\"entries:\":
[[\"a\",91],
[\"b\",65],
[\"c\",26],
[\"d\",25]]}"
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止:
json_dict = {'type': str("id"),
'entries': [['a': "91"], #Error line
['b': "65"],
['c': "26"],
['d': "25"]]}
Run Code Online (Sandbox Code Playgroud)
我在用#Error行标记的行上出现"无效语法"错误.如何在dict中表示此层次结构并仍然能够将其转换为所需的JSON结构?
Python列表使用逗号,而不是冒号:
json_dict = {'type': str("id"),
'entries': [['a', "91"], # note the comma after 'a', not a colon
['b', "65"],
['c', "26"],
['d', "25"]]}
Run Code Online (Sandbox Code Playgroud)
使用逗号,这是现在有效的Python语法,生成可以序列化为JSON的数据结构:
>>> json_dict = {'type': str("id"),
... 'entries': [['a', "91"],
... ['b', "65"],
... ['c', "26"],
... ['d', "25"]]}
>>> import json
>>> json.dumps(json_dict)
'{"type": "id", "entries": [["a", "91"], ["b", "65"], ["c", "26"], ["d", "25"]]}'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
132 次 |
| 最近记录: |