开发者

Converting a Google Directions v3 JSON Array to PHP Array/Variables to Store in MySQL

开发者 https://www.devze.com 2023-02-11 23:19 出处:网络
I\'m trying to convert a JSON object/array from the Google Directions API v3 to PHP so I can store it in a MySQL Database.

I'm trying to convert a JSON object/array from the Google Directions API v3 to PHP so I can store it in a MySQL Database.

The array looks something like this (I truncated it pretty heavily…sorry for making it so long…I guess this is also an informative question for people wanting to know what the array string from Google Directions looks like):

{
"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"}

Using AJAX, I sent this object to PHP to decode it and save it in a database.

The only problem is that I don't know how to parse the JSON into PHP…I kind of feel like a dog that actually caught the car they were chasing…I don't know what to do next.

So my question: How do I take the array above and turn it into something PHP can send to a MySQL database (I already have the database structure set up)? I'm n开发者_如何转开发ot very good with programming languages, so if you could write your response in the most basic way possible, I'd be ever so grateful.


You could just store the JSON string directly in the database in a text field.

Alternately, if you want to read the individual fields in PHP, use the json_decode function to turn the JSON string into a PHP array.

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


Do convert a json object to an object / array that can be used within the PHP Engine you would use json_decode() such as:

$context = json_decode($json_feed);

and them use like an internal array:

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

If you wish to send the array to the database in bulk, I.e, No individual fields within the database then you can use serialize to create a reversible string that can be stored within the database and then unserialize to convert it back into a php enumerable entity

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号