在一行中显示两个不同的 JSON 结果
Display two different JSON results in one line
我是 JSON、Google Maps APIs DistanceMatrix 和 Places
的新手
我正在尝试显示来自两个不同 JSON 结果
的结果
基本上都是两个不同的"lists"
编辑
对于 object_json
我从我的坐标中获取所有附近的地方,然后我将获取每个结果的 places_id 并使用它来查询距离矩阵 api 并获取每个距离 object_json2
代码
#name and types from Places API
lines = (f"{s['name']}: {', '.join(r.replace('_', ' ') for r in s['types'])} -" for s in object_json['results'][0:5])
#distance from DistanceMatrix API
lines2 = (f"{t['distance']['text']}" for t in item['elements'] for item in object_json2['rows'])
样本JSON
object_json:获取名称、类型和places_id
"results" : [
{
"name" : "Joe’s Thai Kitchen",
"place_id" : "ChIJIadbw8sb2jER3i-OvZSCBGk",
"types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
},
object_json2 根据附近的搜索结果,得到places_id,得到每个元素distance的距离矩阵结果
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "0.3 km",
"value" : 303
},
"duration" : {
"text" : "2 mins",
"value" : 123
},
"status" : "OK"
},
{
"distance" : {
"text" : "90 m",
"value" : 90
},
"duration" : {
"text" : "1 min",
"value" : 36
},
"status" : "OK"
}
]
}
],
前两个结果
Joe’s Thai Kitchen: restaurant, food, point of interest, establishment - ['0.4 km', '77 m', '0.3 km', '0.8 km', '0.3 km']
Hong Kong Street Chun Kee: restaurant, food, point of interest, establishment - ['0.4 km', '77 m', '0.3 km', '0.8 km', '0.3 km']
想要的前两个结果
Joe’s Thai Kitchen: restaurant, food, point of interest, establishment - 0.4 km
Hong Kong Street Chun Kee: restaurant, food, point of interest, establishment - 77 m
希望有人能帮忙谢谢。
首先,我认为您没有正确解释 Distance Maxtrix API 的结果。这也导致 the XY Problem.
但无论如何,您实际要做的是加入 object1
和 object2
中两个列表的结果。您可以使用 zip()
执行此操作。 zip()
接受给定的每个可迭代对象和 returns 每个列表的第 nth 元素的元组。并停在最短的一个:
>>> list(zip('ABCD', 'xyz'))
[('A', 'x'), ('B', 'y'), ('C', 'z')]
假设 object_json
看起来像这样:
object_json = {
"results" : [
{
"name" : "Joe’s Thai Kitchen",
"place_id" : "ChIJIadbw8sb2jER3i-OvZSCBGk",
"types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
},
{
"name" : "Hong Kong Street Chun Kee",
"place_id" : "KgbcszVo-I3reJ2BS8WBDAijiHc",
"types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
}
]
}
根据您的示例使用 object_json2
:
>>> lines = (f"{s['name']}: {', '.join(r.replace('_', ' ') for r in s['types'])} -" for s in object_json['results'][0:5])
>>> lines2 = (f"{t['distance']['text']}" for item in object_json2['rows'] for t in item['elements'])
>>>
>>> [' '.join((x, y)) for x, y in zip(lines, lines2)]
['Joe’s Thai Kitchen: restaurant, food, point of interest, establishment - 0.3 km',
'Hong Kong Street Chun Kee: restaurant, food, point of interest, establishment - 90m'
]
(顺便说一句,在您的代码中,这一行是语法错误:lines2 = (f"{t['distance']['text']}" for t in item['elements'] for item in object_json2['rows'])
。
第二个 for 循环应该先出现。)
我是 JSON、Google Maps APIs DistanceMatrix 和 Places
的新手
我正在尝试显示来自两个不同 JSON 结果
基本上都是两个不同的"lists"
编辑
对于 object_json
我从我的坐标中获取所有附近的地方,然后我将获取每个结果的 places_id 并使用它来查询距离矩阵 api 并获取每个距离 object_json2
代码
#name and types from Places API
lines = (f"{s['name']}: {', '.join(r.replace('_', ' ') for r in s['types'])} -" for s in object_json['results'][0:5])
#distance from DistanceMatrix API
lines2 = (f"{t['distance']['text']}" for t in item['elements'] for item in object_json2['rows'])
样本JSON object_json:获取名称、类型和places_id
"results" : [
{
"name" : "Joe’s Thai Kitchen",
"place_id" : "ChIJIadbw8sb2jER3i-OvZSCBGk",
"types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
},
object_json2 根据附近的搜索结果,得到places_id,得到每个元素distance的距离矩阵结果
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "0.3 km",
"value" : 303
},
"duration" : {
"text" : "2 mins",
"value" : 123
},
"status" : "OK"
},
{
"distance" : {
"text" : "90 m",
"value" : 90
},
"duration" : {
"text" : "1 min",
"value" : 36
},
"status" : "OK"
}
]
}
],
前两个结果
Joe’s Thai Kitchen: restaurant, food, point of interest, establishment - ['0.4 km', '77 m', '0.3 km', '0.8 km', '0.3 km']
Hong Kong Street Chun Kee: restaurant, food, point of interest, establishment - ['0.4 km', '77 m', '0.3 km', '0.8 km', '0.3 km']
想要的前两个结果
Joe’s Thai Kitchen: restaurant, food, point of interest, establishment - 0.4 km
Hong Kong Street Chun Kee: restaurant, food, point of interest, establishment - 77 m
希望有人能帮忙谢谢。
首先,我认为您没有正确解释 Distance Maxtrix API 的结果。这也导致 the XY Problem.
但无论如何,您实际要做的是加入 object1
和 object2
中两个列表的结果。您可以使用 zip()
执行此操作。 zip()
接受给定的每个可迭代对象和 returns 每个列表的第 nth 元素的元组。并停在最短的一个:
>>> list(zip('ABCD', 'xyz'))
[('A', 'x'), ('B', 'y'), ('C', 'z')]
假设 object_json
看起来像这样:
object_json = {
"results" : [
{
"name" : "Joe’s Thai Kitchen",
"place_id" : "ChIJIadbw8sb2jER3i-OvZSCBGk",
"types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
},
{
"name" : "Hong Kong Street Chun Kee",
"place_id" : "KgbcszVo-I3reJ2BS8WBDAijiHc",
"types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
}
]
}
根据您的示例使用 object_json2
:
>>> lines = (f"{s['name']}: {', '.join(r.replace('_', ' ') for r in s['types'])} -" for s in object_json['results'][0:5])
>>> lines2 = (f"{t['distance']['text']}" for item in object_json2['rows'] for t in item['elements'])
>>>
>>> [' '.join((x, y)) for x, y in zip(lines, lines2)]
['Joe’s Thai Kitchen: restaurant, food, point of interest, establishment - 0.3 km',
'Hong Kong Street Chun Kee: restaurant, food, point of interest, establishment - 90m'
]
(顺便说一句,在您的代码中,这一行是语法错误:lines2 = (f"{t['distance']['text']}" for t in item['elements'] for item in object_json2['rows'])
。
第二个 for 循环应该先出现。)