gpt4 book ai didi

php - 将 Google Directions v3 JSON 数组转换为 PHP 数组/变量以存储在 MySQL 中

转载 作者:行者123 更新时间:2023-12-04 06:33:59 27 4
gpt4 key购买 nike

我正在尝试将 JSON 对象/数组从 Google Directions API v3 转换为 PHP,以便我可以将其存储在 MySQL 数据库中。

数组看起来像这样(我把它截断了很多......抱歉让它这么长......我想这对于想要知道来自 Google Directions 的数组字符串是什么样子的人来说也是一个信息性问题):

{
"status":"OK",
"routes":[{
"summary":"Lakelands Trail State Park",
"legs":[{
"steps":[{
"travel_mode":"BICYCLING",
"start_location":{"za":42.73698,"Ba":-84.4838},
"end_location":{"za":42.74073,"Ba":-84.48378},
"polyline":{"points":"cazcGvvsbOmVC","levels":"BB"},
"duration":{"value":68,"text":"1 min"},
"distance":{"value":417,"text":"0.3 mi"},
"encoded_lat_lngs":"cazcGvvsbOmVC",
"path":[{"za":42.73698,"Ba":-84.4838},{"za":42.740730000000006,"Ba":-84.48378000000001}],
"lat_lngs":[{"za":42.73698,"Ba":-84.4838},{"za":42.740730000000006,"Ba":-84.48378000000001}],
"instructions":"Head north on Abbot Rd toward Elizabeth St",
"start_point":{"za":42.73698,"Ba":-84.4838},
"end_point":{"za":42.74073,"Ba":-84.48378}
},{
//more steps go here
//end steps array
}}],

"duration":{"value":12309,"text":"3 hours 25 mins"},
"distance":{"value":66198,"text":"41.1 mi"},
"start_location":{"za":42.66069,"Ba":-84.07321},
"end_location":{"za":42.27668,"Ba":-83.74076},
"start_address":"E Grand River Ave, Fowlerville, MI 48836, USA",
"end_address":"angell hall, 435 S State St, Ann Arbor, MI 48109, USA",
"via_waypoint":[]
//end leg array
}],

"copyrights":"Map data ©2011 Google",
"warnings":["Bicycling directions are in beta. Use caution – This route may contain streets that aren't suited for bicycling."],
"waypoint_order":[0],
"bounds":{"U":{"b":42.276680000000006,"d":42.740840000000006},"O":{"d":-84.4838,"b":-83.73996000000001}},
"optimized_waypoint_order":[0]}],
"Ef":{"origin":"east lansing, mi","destination":"1139 Angell Hall 435 S. State Street Ann Arbor, MI 48109",
"waypoints":[{"location":"Bloated Goat Saloon, East Grand River Avenue, Fowlerville, MI","stopover":true}],"optimizeWaypoints":false,"travelMode":"BICYCLING"}

使用 AJAX,我将此对象发送到 PHP 以对其进行解码并将其保存在数据库中。

唯一的问题是我不知道如何将 JSON 解析成 PHP……我有点像一条狗,实际上捕获了他们正在追的车……我不知道下一步该怎么做。

所以我的问题:
如何获取上面的数组并将其转换为 PHP 可以发送到 MySQL 数据库的内容(我已经设置了数据库结构)?我不太擅长编程语言,所以如果你能用最基本的方式来写你的回复,我会非常感激。

最佳答案

您可以将 JSON 字符串直接存储在数据库中的文本字段中。

或者,如果您想读取 PHP 中的各个字段,请使用 json_decode函数将 JSON 字符串转换为 PHP 数组。

http://php.net/manual/en/function.json-decode.php

关于php - 将 Google Directions v3 JSON 数组转换为 PHP 数组/变量以存储在 MySQL 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5054250/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com