Give the supplied JSON I want to extract the latitude and longitude coordinates only.
"vectorQuery": {
"layers": {
"50772": {
"crs": {
"type": "name",
"properties": {
"name": "EPSG:4326"
}
},
"field_names": ["id", "appellation", "affected_surveys", "parcel_intent", "topology_type", "statutory_actions", "land_district", "titles", "survey_area", "calc_area"],
"type": "FeatureCollection",
"features": [{
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[174.8592712667, -36.9675741167],
[174.8593271167, -36.9673525333],
[174.8593513167, -36.9672686333],
[174.8592025167, -36.9671776333],
[174.8591615833, -36.9672144833],
[174.8588608833, -36.9674851833],
[174.8592451667, -36.9678935833],
[174.8592699833, -36.9679199],
[174.8594188, -36.9680780333],
[174.8597915667, -36.9678483167],
[174.85991985, -36.9677692333],
[174.8599389, -36.96770445],
[174.8596869333, -36.9676552667],
[174.8596362667, -36.9676453833],
[174.8592712667, -36.9675741167]
]
]
]
},
"distance": 0,
"type": "Feature",
"properties": {
"id": 5073537,
"appellation": "Lot 2 DP 64503",
"affected_surveys": "DP 64503",
"parcel_intent": "DCDB",
"topology_type": "Primary",
"statutory_actions": null,
"land_district": "North Auckland",
"titles": "NA20A/1419",
"survey_area": 3688.0,
"calc_area": 3686.0
},
"id": 995125
}]
}
}
}
}
I actually just want to get the coordinates. I can pick these of these by ...
print_r($arr['vectorQuery']['layers']['50772']['features'][0]['geometry']['coordinates'][0][0]);
The issue being that the key 50772 can change each request. Is there a method of traversing directly to the coordinates?
Related
I have never used JSON before so apologies if this is a simple request.
I have a webhook setup that sends me a JSON Post (Example Below) - I want to extract the two answers from this "text":"250252" & {"label":"CE"}
{
"event_id": "1",
"event_type": "form_response",
"form_response": {
"form_id": "VpWTMQ",
"token": "1",
"submitted_at": "2018-05-22T14:11:56Z",
"definition": {
"id": "VpWTMQ",
"title": "SS - Skill Change",
"fields": [
{
"id": "kUbaN0JdLDz8",
"title": "Please enter your ID",
"type": "short_text",
"ref": "9ac66945-899b-448d-859f-70562310ee5d",
"allow_multiple_selections": false,
"allow_other_choice": false
},
{
"id": "JQD4ksDpjlln",
"title": "Please select the skill required",
"type": "multiple_choice",
"ref": "a24e6b58-f388-4ea9-9853-75f69e5ca337",
"allow_multiple_selections": false,
"allow_other_choice": false
}
]
},
"answers": [
{
"type": "text",
"text": "250252",
"field": {
"id": "kUbaN0JdLDz8",
"type": "short_text"
}
},
{
"type": "choice",
"choice": {
"label": "CE"
},
"field": {
"id": "JQD4ksDpjlln",
"type": "multiple_choice"
}
}
]
}
}
I have this currently in my PHP file:
$data = json_decode(file_get_contents('php://input'));
$ID = $data->{"text"};
$Skill = $data->{"label"};
This does not work and all I get is null - Any help would really be appreciated, Thank You.
You need to look at the JSON object you're receiving to know the structure of the object you're receiving after using json_decode, what you're trying to get is in $data->form_response->answers, So you can have a variable for easier access:
$answers = $data->form_response->answers;
remember $answers is an array
So to achieve what you're trying to get, you can do:
$data = json_decode(file_get_contents('php://input'));
$answers = $data->form_response->answers;
$ID = $answers[0]->text;
$Skill = $answers[1]->choice->label;
I have a Google Map using the Google Map API drawing polylines from a .json file. A portion of the file structure can be seen below, it just repeats for each polyine feature. It was created in GIS and converted to a .geojson and then json.
I am looking to allow a user to modify specific properties of a feature through a mapping interface. I have the iterface and user interactions all built out but am having trouble modifying the .json file.
I have tried the following:
PHP (Note: I am hard-coding some values to test/get it to work)
$jsonString = file_get_contents('../json/hartford.json');
$data = json_decode($jsonString, true);
foreach ($data as $key => $entry) {
if ($entry['UID'] == 25301) {
$data[$key]['RENDER_CL'] = "99";
}
}
$newJsonString = json_encode($data);
file_put_contents('../json/hartford.json', $newJsonString);
Errors:
Warning: Illegal string offset 'UID' on line...
Notice: Undefined index: UID on line...
Thoughts:
I feel I am not going deep enough in to the json "tree" to access the properties. I have not modified json data before so I am a bit lost. Should I somehow be going deeper into the structure? UID is unique for each item. Thanks.
JSON file
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [{
"type": "Feature",
"properties": {
"FULLNAME": "I-84 E",
"RENDER_CL": 0,
"FCC": "Highway",
"clicked": "no",
"UID": 25301
},
"geometry": {
"type": "MultiLineString",
"coordinates": [[[-72.70621818192563523552962578833103179931640625, 41.7494854544135023388662375509738922119140625], [-72.7059645455619971698979497887194156646728515625, 41.74953636350443275659927166998386383056640625], [-72.7057163637438179648597724735736846923828125, 41.749591818049879066165885888040065765380859375], [-72.705553636471080380943021737039089202880859375, 41.749633636231692435103468596935272216796875], [-72.7053663637438063460649573244154453277587890625, 41.749688181686252619329025037586688995361328125], [-72.705190909198364579424378462135791778564453125, 41.749742727140784381845151074230670928955078125], [-72.70496727283472182534751482307910919189453125, 41.749821818049866806177305988967418670654296875], [-72.704716363743813190012588165700435638427734375, 41.749924545322613766984432004392147064208984375], [-72.70461181828926555681391619145870208740234375, 41.74996818168625623002299107611179351806640625]]]
}
}, {
"type": "Feature",
"properties": {
"FULLNAME": "I-84 E",
"RENDER_CL": 0,
"FCC": "Highway",
"clicked": "no",
"UID": 25302
},
"geometry": {
"type": "MultiLineString",
"coordinates": [[[-72.7150890910165372815754381008446216583251953125, 41.749747272595328695388161577284336090087890625], [-72.7141218182892572485798154957592487335205078125, 41.7496918180498823858215473592281341552734375]]]
}
},
You are accessing your object's property incorrectly. the correct way would be $entry['properties']['UID'].
Example
<?php
$json =
'{
"type": "Feature",
"properties": {
"FULLNAME": "I-84 E",
"RENDER_CL": 0,
"FCC": "Highway",
"clicked": "no",
"UID": 25302
},
"geometry": {
"type": "MultiLineString",
"coordinates": [[[-72.7150890910165372815754381008446216583251953125, 41.749747272595328695388161577284336090087890625], [-72.7141218182892572485798154957592487335205078125, 41.7496918180498823858215473592281341552734375]]]
}
}';
$obj = json_decode($json, true);
print $obj['properties']['UID'];
I'm trying to create a game API thing that requires this decoded, but I'm not sure how (this is just for a certain user, so the values wont be the same)
[
{
"Id": 382779,
"Name": "DarkAge Ninjas"
},
{
"Id": 377291,
"Name": "Emerald Knights of the Seventh Sanctum"
},
{
"Id": 271454,
"Name": "Knights of RedCliff"
},
{
"Id": 288278,
"Name": "Knights of the Splintered Skies "
},
{
"Id": 375307,
"Name": "Korblox's Empire"
},
{
"Id": 387867,
"Name": "Ne'Kotikoz"
},
{
"Id": 696519,
"Name": "Orinthians"
},
{
"Id": 27770,
"Name": "Retexture Artists Official Channel"
},
{
"Id": 585932,
"Name": "Retexturing Apprentices "
},
{
"Id": 7,
"Name": "Roblox"
},
{
"Id": 679727,
"Name": "ROBLOX Community Staff and Forum Users"
},
{
"Id": 127081,
"Name": "Roblox Wiki"
}
]
How can I decode this in PHP so It has a list like
DarkAge Ninjas
Emerald Knights of the Seventh Sanctum
Knights of RedCliff
etc, and have the Id decoded separately so I can make a clickable link out of it :/
You will need json_decode to turn json into php array
$api_json = '[
{ "Id": 382779, "Name": "DarkAge Ninjas" },
{ "Id": 377291, "Name": "Emerald Knights of the Seventh anctum" }
...
]';
$api_data = json_decode($api_json, true);
//Now you can loop over the array and print the `Name`
foreach($api_data as $d) {
echo $d['Name'];
}
above code will output
DarkAge Ninjas
Emerald Knights of the Seventh Sanctum
Knights of RedCliff
...
To make link with ids just add this in above loop
echo ''. $d['Name'].'';
as Ed Cottrell suggested, Read the manual: json_decode to know more
I have been getting my facebook friends work history. But the result is an array whose content is as follows :
{
"id": "xxx",
"friends": {
"data": [
{
"name": "Indiarto Priadi",
"work": [
{
"employer": {
"id": "111178415566505",
"name": "Liputan 6 SCTV"
}
},
{
"employer": {
"id": "107900732566334",
"name": "SCTV"
}
}
],
"id": "502163984"
},
{
"name": "Agustin Kertawijaya",
"work": [
{
"employer": {
"id": "138215336225242",
"name": "Trader Corporation (Canada)"
},
"location": {
"id": "110941395597405",
"name": "Toronto, Ontario"
},
"position": {
"id": "168673399850791",
"name": "Desktop operator <Pre press>"
},
"start_date": "2006-06-01",
"end_date": "2008-06-01"
},
{
"employer": {
"id": "114464911939560",
"name": "Merrill Corporation Canada"
},
"location": {
"id": "110941395597405",
"name": "Toronto, Ontario"
},
"position": {
"id": "190075304347365",
"name": "Financial Print Project Manager"
},
"start_date": "2006-06-01",
"end_date": "2011-10-01"
}
],
"id": "511990261"
}
],
"paging": {
"next": "https://graph.facebook.com/1065251285/friends?limit=2&fields=name,work&offset=2&__after_id=511990261"
}
}
}
Now i want to display "only" the employer name on my page, but i can't find the way to do that.
Here is my code :
$userFriends = $facebook->api('/'.$userId.'/friends');
for($i=0;$i<=3;$i++){ //retrieved friends (max 4 persons)
$value=$userFriends["data"][$i];
echo "<div id='$value[id]'>";
$profile = $facebook->api("/".$value["id"]);
$friendsWork = $facebook->api("/".$value["id"]."?fields=work");
echo "<strong>".$profile["name"]."<br></strong>";
echo " <img src='https://graph.facebook.com/".$value["id"]."/picture'><br>";
echo "Username : ".$profile["username"]."<br>";
echo "Local : ".$profile["locale"]."<br>";
echo "Birthdate : ".$profile["birthday"]."<br>";
print_r($friendsWork); // <--- the result is an array
echo "<hr>";
echo "</div>";
}
Does anyone know how to display the company(employer) name only?
any answers would be greatly appreciated. Thank You
Here you go, nothing fancy, I just did what I said in my comment
$json = '{
"id": "xxx",
"friends": {
"data": [
{
"name": "Indiarto Priadi",
"work": [
{
"employer": {
"id": "111178415566505",
"name": "Liputan 6 SCTV"
}
},
{
"employer": {
"id": "107900732566334",
"name": "SCTV"
}
}
],
"id": "502163984"
},
{
"name": "Agustin Kertawijaya",
"work": [
{
"employer": {
"id": "138215336225242",
"name": "Trader Corporation (Canada)"
},
"location": {
"id": "110941395597405",
"name": "Toronto, Ontario"
},
"position": {
"id": "168673399850791",
"name": "Desktop operator <Pre press>"
},
"start_date": "2006-06-01",
"end_date": "2008-06-01"
},
{
"employer": {
"id": "114464911939560",
"name": "Merrill Corporation Canada"
},
"location": {
"id": "110941395597405",
"name": "Toronto, Ontario"
},
"position": {
"id": "190075304347365",
"name": "Financial Print Project Manager"
},
"start_date": "2006-06-01",
"end_date": "2011-10-01"
}
],
"id": "511990261"
}
],
"paging": {
"next": "https://graph.facebook.com/1065251285/friends?limit=2&fields=name,work&offset=2&__after_id=511990261"
}}}'; // remember the last two closing curlys, you left them out of the code block in the OP.
$obj = json_decode($json);
foreach ($obj->friends->data as $friend) {
echo '<h1>' . $friend->name . '</h1>';
foreach ($friend->work as $job) {
echo 'Employee: ' . $job->employer->name . '<br/>';
}
echo '<hr>';
}
Output:
Indiarto Priadi
Employee: Liputan 6 SCTV
Employee: SCTV
--------------------------------------
Agustin Kertawijaya
Employee: Trader Corporation (Canada)
Employee: Merrill Corporation Canada
--------------------------------------
You just have to parse the array you obtained, like this-
$userFriends = $facebook->api('/'.$userId.'/friends?fields=work,name');
foreach($userFriends['data'] as $friend)
{
$friend_name = $friend['name'];
$emp_name=array();
// list of all employers of this friend
if(isset($friend['work']))
{
foreach($friend['work'] as $work)
{
array_push($emp_name, $work['employer']['name']);
}
}
else
{
$emp_name = "N.A.";
}
}
I want to make a PHP JSON data foreach, but I met some problem. First: I can not get the properties part. Second it alawys show wrong in line: echo '<div class="image">...
Fatal error: Cannot use object of type stdClass as array in ...
This is the json data:
[
{
"post_id": "504464716_189878284371815",
"message": "\"Happy Birthday to You\" \"Happy Birthday to Mama\"",
"attachment": {
"media": [
{
"href": "http:\/\/www.facebook.com\/photo.php?fbid=493710409716&set=a.453260184716.254996.504464716",
"alt": "",
"type": "photo",
"src": "http:\/\/photos-f.ak.fbcdn.net\/hphotos-ak-snc4\/hs049.snc4\/34793_493710409716_504464716_5821684_2056840_s.jpg",
"photo": {
"aid": "2166659457206182932",
"pid": "2166659457211749620",
"owner": 504464716,
"index": 24,
"width": 225,
"height": 225
}
}
],
"name": "Wall Photos",
"href": "http:\/\/www.facebook.com\/album.php?aid=254996&id=504464716",
"caption": "\"Happy Birthday to You\" \"Happy Birthday to Mama\"",
"description": "",
"properties": [
{
"name": "By",
"text": "Suman Acharya",
"href": "http:\/\/www.facebook.com\/profile.php?id=504464716"
}
],
"icon": "http:\/\/static.ak.fbcdn.net\/rsrc.php\/yD\/r\/aS8ecmYRys0.gif",
"fb_object_type": "album",
"fb_object_id": "2166659457206182932"
},
"action_links": null,
"privacy": {
"value": ""
}
},
...
]
Here is my php code:
foreach ($data as $result) {
echo '<div class="title">'.htmlspecialchars($result->message).'<br />'.htmlspecialchars($result->description).'<br />'.htmlspecialchars($result->caption).'<br />';
if(!empty($result->attachment->properties[0]->text)){
foreach ($result->attachment->properties[0] as $properties) {
echo htmlspecialchars($properties->name).'<br />'.htmlspecialchars($properties->text).'</div>';
}
}
if(!empty($result->attachment->media)){
echo '<div class="image"><img src="'.htmlspecialchars($result->attachment->media[0]->src).'" /><br>'.htmlspecialchars($result->attachment->media[0]->type).'</div>';
}
}
If i were you i would just force the decoding to an assoc array by true as the second arg to json_decode. If you cant or dont want to do that then try accessing it like this:
$result->attachment->media->{0}->href
Use json_decode($the_data, true); instead of json_decode($the_data); that way it will return you an associative array instead of a StdClass.