【问题标题】:Converting a Google Directions v3 JSON Array to PHP Array/Variables to Store in MySQL将 Google Directions v3 JSON 数组转换为 PHP 数组/变量以存储在 MySQL 中
【发布时间】:2011-06-30 13:38:03
【问题描述】:

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

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

{
"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 数据库的东西(我已经设置了数据库结构)?我对编程语言不是很好,所以如果你能以最基本的方式写你的回复,我会非常感激。

【问题讨论】:

    标签: php arrays json google-maps google-maps-api-3


    【解决方案1】:

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

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

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

    【讨论】:

    • 我需要各个字段……一旦我将其转换为 PHP 数组,如何将其发送到数据库?
    • 我发现了部分问题。通过在 Google Directions 中创建路线生成的 JSON 文件实际上非常复杂,以至于 AJAX 难以将对象发送到 PHP。
    【解决方案2】:

    将 json 对象转换为可在 PHP 引擎中使用的对象/数组,您将使用 json_decode(),例如:

    $context = json_decode($json_feed);
    

    它们像内部数组一样使用:

    if($context["status"] == "OK"){/*...*/}
    foreach($context["routes"] as $Route)
    {
         //have Fun Walking
    }
    

    如果您希望将数组批量发送到数据库,即数据库中没有单独的字段,那么您可以使用serialize 创建一个可以存储在数据库中的可逆字符串,然后使用unserialize 进行转换它回到一个 php 可枚举实体

    【讨论】:

    • 好的,那么在我创建了 foreach 数组之后我该怎么办?
    • 查看我对其他答案的评论
    猜你喜欢
    • 2017-05-03
    • 1970-01-01
    • 2012-02-02
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 2021-09-08
    相关资源
    最近更新 更多