我无法找到如何转换 python 元素列表,如下所示:
[[["thisid", 24024502], ["points", [[["lat", 37.8732041], ["lon", -122.2562601]], [["lat", 37.8729153], ["lon", -122.2561566]]]], ["name", "Latimer Hall"]]
Run Code Online (Sandbox Code Playgroud)
到 json 元素数组,如下所示:
{"thisid": 24024502, "points": [{"lat": 37.8732041, "lon": -122.2562601}, {"lat": 37.8729153, "lon": -122.2561566}], "name": "Latimer Hall"}
Run Code Online (Sandbox Code Playgroud)
基本上,我试图将具有内部结构的列表列表转换为 json 中的相应列表。
Plainjson.dumps(mylist)只返回原始列表(我猜,这是因为它也是一个有效的 json 对象......)
非常感谢您的任何建议!
原始代码中的括号不平衡。如果我在开始时删除一个括号:
>>> a = [["thisid", 24024502], ["points", [[["lat", 37.8732041], ["lon", -122.2562601]], [["lat", 37.8729153], ["lon", -122.2561566]]]], ["name", "Latimer Hall"]]
>>> b = dict(a)
>>> for i, l in enumerate(b['points']):
... b['points'][i] = dict(l)
...
>>> b
{'points': [{'lat': 37.8732041, 'lon': -122.2562601}, {'lat': 37.8729153, 'lon': -122.2561566}], 'thisid': 24024502, 'name': 'Latimer Hall'}
>>>
Run Code Online (Sandbox Code Playgroud)
然后序列化成json