【发布时间】:2017-08-23 21:22:46
【问题描述】:
我在理解将元组转换为 json 的操作顺序时遇到了一些麻烦。这是我的清单:
coordinates = [(45.62, -122.23),(45.63, -122.22),(45.64, -122.21)]
通过我的代码运行它时:
coordinates = [(45.62, -122.23),(45.63, -122.22),(45.64, -122.21)]
coordinatesList = json.dumps(dict(coordinates))
我设法得到一个 json 数组。这是我当前的输出:
print(coordinatesList)
{"45.62": -122.23, "45.63": -122.22, "45.64": -122.21}
如您所见,我在这里遇到了多个问题。一些坐标用引号封装,而第二个值则没有。我还需要使用“lat”和“lng”附加值以适应 Google Maps api,但我对流控制的位置感到困惑。
这是我想要的输出:
[
{lat: 45.62, lng: -122.23},
{lat: 45.63, lng: -122.22},
{lat: 45.64, lng: -122.21},
]
【问题讨论】:
标签: python json google-maps tuples coordinates