简体   繁体   English

如何将python列表转换为json

[英]How to convert python list to json

How to convert list to json :如何将列表转换为 json :

list is :名单是:

cords = [ [20.339751164468076, 85.8083048318772], [20.339866852950124, 85.80909340132757]]

and I want to convert that list into json like this:我想将该列表转换为这样的 json:

json_cords = [
    {
        'lat': 20.339751164468076,
        'lon': 85.8083048318772
    },
    {
        'lat': 20.339866852950124,
        'lon': 85.80909340132757
    }
]

Use the below dictionary comprehension:使用以下词典理解:

import json
cords = [ [20.339751164468076, 85.8083048318772], [20.339866852950124, 85.80909340132757]]
print(json.dumps([{'lat':x,'lon':y} for x,y in cords],indent=4))

Output:输出:

[
    {
        "lat": 20.339751164468076,
        "lon": 85.8083048318772
    },
    {
        "lat": 20.339866852950124,
        "lon": 85.80909340132757
    }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM