你不可不知的 JSON 基本介紹
error_reporting(E_ALL^E_NOTICE^E_WARNING); //關閉PHP的所有錯誤提示 header('Content-Type: application/json; charset=utf-8'); //記得加utf-8 解決亂碼 require_once("db.config.php"); $jsonString = ' { 'action':"ok", "contents":[ { "Name": "涼宮ハルヒン", "Attrib": "SOS団長" }, { "Name": "キョン", "Attrib": "普通人" } ], "data":{ "Name": "涼宮ハルヒン","Attrib": "SOS団長"} } ';
$arr_json = json_decode( $jsonString ); echo $arr_json->data->Name . " ";涼宮ハルヒン
$arr_json = json_decode( $jsonString ); $json_output .= "debug/>"; $json_output .= '{'; $json_output .= '"action" :"success" ,'; //傳送參數回傳 $json_output .= '"params" : '; $json_output .= $jsonString ; $json_output .= ', '; //傳送參數回傳 $json_output .= ' "data" : "'; // echo $arr_json->contents[1]->user_share_loc_id ; foreach ($arr_json->contents as $key => $value) { echo $arr_json->contents[$key]->Name."\n"; echo $arr_json->contents[$key]->Attrib."\n"; $json_output_data .= $arr_json->contents[$key]->Name.", "; $json_output .= $json_output_data; $json_output .= '"}'; echo $json_output;
涼宮ハルヒン SOS団長 キョン 普通人 debug/>{"action" :"success" ,"params" : { "contents":[ { "Name": "涼宮ハルヒン", "Attrib": "SOS団長" }, { "Name": "キョン", "Attrib": "普通人" } ] } , "data" : "涼宮ハルヒン, キョン, "}若為抓取頁面 切割過濾JSON外的方法
$output = explode("debug/>", $pageContents); $arr_json =json_decode($output[1]); $action = $arr_json->{'action'}; echo $action; echo $arr_json->contents[0]->Name;涼宮ハルヒン
Google Places API
{
"debug_info": [],
"html_attributions": [],
"results": [
{
"geometry": {
"location": {
"lat": 25.085571,
"lng": 121.564002
}
},
"icon": "http://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png",
"id": "3d7fbd4541ad66c611646f4f592fa12101a24170",
"name": "肯達咖啡",
"opening_hours": {
"open_now": true
},
...
]
$url = "https://maps.googleapis.com/maps/api/place/search/json?location=25.091075,121.559834&sensor=false&radius=700&types=cafe&key=yourkey"; $place_search_json_result = file_get_contents($url, false); $jsonContent =json_decode($place_search_json_result); echo $jsonContent->results[0]->name ; $geo_result = $jsonContent->results[0]; echo '(' ; echo $geo_result->geometry->location->lat; echo ',' ; echo $geo_result->geometry->location->lng; echo ')' ;肯達咖啡(25.085571,121.564002) php取得json
$pageContents = file_get_contents($url, false); $place_search_json_result = explode("debug/>", $pageContents); $locations_db_json =$place_search_json_result[1];php傳值json給js
// var locations = [ // { // latlng : new google.maps.LatLng(25.0336148, 120.56480220000003), // info : "hello" // }, // { // latlng : new google.maps.LatLng(25.0336148, 121.56480220000003), // info : "world" // } // ]; var locations = [ ]; for (var i = 0; i < locations.length; i++) { var marker = new google.maps.Marker({ position: locations[i].latlng, //icon: pinkmarker, //shadow: shadow, map: map }); google.maps.event.addListener(marker, 'click', (function(marker, i) { return function() { infowindow.setContent(locations[i].info); infowindow.open(map, marker); } })(marker, i)); }google spreadsheets return json
header('Content-type: application/json; charset="utf-8"'); $google_sheet_api = 'https://spreadsheets.google.com/feeds/list/1IKeEX6Vbn-T8pCiwLqURgYf_GGojE198iXXIS93_bD4/od6/public/values?alt=json'; $api_json = file_get_contents($google_sheet_api); $api_json_str_replace = str_replace("$","",$api_json); $arr_json = json_decode(stripslashes($api_json_str_replace)); //echo $arr_json->version; //echo $arr_json->feed->xmlns; //echo $arr_json->feed->title->type; //echo $arr_json->feed->title->t; //工作表1 //echo $arr_json->feed->entry[0]->gsxname->t; //echo $arr_json->feed->entry[0]->gsxlatitude->t.","; //echo $arr_json->feed->entry[0]->gsxlongitude->t; $data =[]; foreach ($arr_json->feed->entry as $key => $value) { $name = $arr_json->feed->entry[$key]->gsxname->t; $latitude = $arr_json->feed->entry[$key]->gsxlatitude->t; $longitude = $arr_json->feed->entry[$key]->gsxlongitude->t; $address = $arr_json->feed->entry[$key]->gsxaddress->t; $record_array_preson = array("id"=>$key,"name"=>$name,"latitude"=>$latitude,"longitude"=>$longitude,"address"=>$address); array_push($data, $record_array_preson); } $json_output .= json_encode($data) ; echo $json_output;[
{
"id": 0,
"name": "家樂福重慶店",
"latitude": "25.0591477",
"longitude": "121.5138074",
"address": "台北市大同區重慶北路二段171號"
},
{
"id": 1,
"name": "家樂福大直店",
"latitude": "25.0824879",
"longitude": "121.5580935",
"address": "台北市中山區樂群三路218號"
},
..
]
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。