我无法找到如何转换这样的元素的python列表:
[[["thisid", 24024502], ["points", [[["lat", 37.8732041], ["lon", -122.2562601]], [["lat", 37.8729153], ["lon", -122.2561566]]]], ["name", "Latimer Hall"]]
到像这样的json数组:
{"thisid":24024502, "points": [{"lat": 37.8732041, "lon": -122.2562601}, {"lat":37.8729153, "lon":-122.2561566}], "name":"Latimer Hall"}
基本上,我正在尝试将具有内部结构的列表列表转换为json中的相应列表。
普通的json.dumps(mylist)只返回原始列表(我猜,它是bc它也是一个有效的json对象......)
非常感谢您提出的任何建议!
答案 0 :(得分:4)
原始代码中的parens是不平衡的。如果我在开头删除一个paren:
>>> 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'}
>>>
然后将其序列化为json