如何将列表列表(列表)转换为python中的json数组? [英] How to convert list of list (of lists) to json array in python?

查看:1408
本文介绍了如何将列表列表(列表)转换为python中的json数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在查找如何转换像这样的元素的python列表时遇到麻烦:

I'm having trouble finding how to convert python list of of elements like this:

[[["thisid", 24024502], ["points", [[["lat", 37.8732041], ["lon", -122.2562601]], [["lat", 37.8729153], ["lon", -122.2561566]]]], ["name", "Latimer Hall"]]

到这样的元素的json数组:

to json array of elements like that:

{"thisid":24024502, "points": [{"lat": 37.8732041, "lon": -122.2562601}, {"lat":37.8729153, "lon":-122.2561566}], "name":"Latimer Hall"}

基本上,我正在尝试将具有内部结构的列表转换为json中的相应列表.

Basically, I'm trying to convert list of lists with inner structure to a corresponding list in json.

简单的json.dumps(mylist)只会返回原始列表(我想是因为它也是一个有效的json对象...)

Plain json.dumps(mylist) just returns the original list (I guess, it's bc it's a valid json object as well...)

非常感谢您提出的任何建议!

Many thanks for any suggestions you may have!

推荐答案

原始代码中的parens不平衡.如果我在一开始就删除了一个括号:

The parens in your original code are unbalanced. If i remove one paren in the beginning:

>>> 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

Then serialize it into json

这篇关于如何将列表列表(列表)转换为python中的json数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆