Printing and parsing JSON variable in PHP - php

I've the following PHP code with a JSON variable...
$route_geometry_json = "{
\"route_geometry\": [
[44.911537, 7.671326],
[44.911481, 7.671462],
[44.911455, 7.671531],
[44.911434, 7.671602],
[44.911358, 7.671859],
[44.911273, 7.672175],
[44.911198, 7.672458],
[44.91113, 7.672617],
[44.911069, 7.67275],
[44.911003, 7.672821],
[44.910945, 7.672881],
[44.910869, 7.672954],
[44.910868, 7.673046],
[44.91091, 7.673109],
[44.91095, 7.67319],
[44.910964, 7.673266],
[44.910958, 7.673407],
[44.910955, 7.6735],
[44.910947, 7.673632],
[44.910922, 7.673871],
[44.910828, 7.674786],
[44.910711, 7.675816],
[44.910606, 7.676364],
[44.910467, 7.676322],
[44.910368, 7.676308],
[44.910051, 7.676253],
[44.9097, 7.676162],
[44.90944, 7.676041],
[44.909297, 7.675958],
[44.909174, 7.67583],
[44.909107, 7.675722],
[44.908993, 7.675583],
[44.908758, 7.675448],
[44.90796, 7.675037]
]
}";
print "Route geometry -->" + json_encode($route_geometry_json);
The print return "0": any suggestion / example?
I'd like also to extract / print the coords couples like
44.908993, 7.675583
44.908758, 7.675448
Any suggestion will be appreciated ...
Thanks
Cesare

Use this code:
$route_geometry = json_decode($route_geometry_json);
foreach ($route_geometry->route_geometry as $value) {
echo $value[0].', '.$value[1].'<br />';
}

You need to print_r(json_decode($route_geometry_json)) it and not json_encode
json_encode is for creating a JSON string. But since you already have a JSON string already, you need to Decode it to make it an Array/Object.
UPDATE
Your requirement
echo "Route geometry -->";
print_r(json_decode($route_geometry_json));
You cannot concat a String and an Object, so you were getting that Parse error.

Related

Alternative method to display specific JSON data in PHP

I am extracting data from a JSON file using a URL like this:
$html5=file_get_contents("url");
$data = json_decode($html5);
echo $data->a->k->f;
This displays $27.58 which is correct.
Here is my JSON data:
{
"a":{
"k":{
"c":"tr",
"f":"$27.58",
"fb":"$30.35",
"ci":"12873809",
"cname":"Cdkeysgame_com",
"ep":"26.05",
"epb":"28.66",
"a":"5473091",
"it":"steam",
"sl":"0",
"l":null,
"lt":null,
"x":0,
"v":"retail",
"ne":0,
"so":0,
"tr":304,
"r":97,
"cf":1,
"p":"27.58317275",
"pb":"30.3467843",
"eci":"865723abbf1cbb952ad4d2da8a8c925e"
},
"k_1":{
"c":"gb",
"f":"$27.64",
"fb":"$30.40",
"ci":"1065801",
"cname":"World_of_games",
"ep":"26.10",
"epb":"28.71",
"a":"781851",
"it":"steam",
"sl":"0",
"l":null,
"lt":null,
"x":0,
"v":"all",
"ne":0,
"so":0,
"tr":1041328,
"r":99,
"cf":1,
"p":"27.6361155",
"pb":"30.39972705",
"eci":"d01a7cacb0e424123985bfe2e53a0523"
},
"k_2":{
"c":"ch",
"f":"$27.68",
"fb":"$30.44",
"ci":"696012",
"cname":"G_hard",
"ep":"26.14",
"epb":"28.75",
"a":"1287052",
"it":"steam",
"sl":"0",
"l":null,
"lt":null,
"x":0,
"v":"retail",
"ne":0,
"so":0,
"tr":10818,
"r":99,
"cf":1,
"p":"27.6784697",
"pb":"30.44208125",
"eci":"a6666c0a47acb70d14b757cd52f1b9cc"
}}
I need to display the same data without using k. For example I want to:
echo $data->a->'Something that specifies first element of 'a' i.e k and not'k_1' and 'k_2'->f;
Since I am scraping this content I cannot change anything like structure or data in this JSON file.
If you just want the first entry under a, then you can decode as an array:
$data = json_decode($json, true);
Then get the first element of $data['a'] and use that. current() will also work:
echo reset($data['a'])['f'];
Or re-index $data['a'] numerically and access the first one 0:
echo array_values($data['a'])[0]['f'];
To catch them all:
foreach($data['a'] as $values) {
echo $values['f'];
//break; if you only want the first one
}
You can do this so:
$object = json_decode($html5);
foreach ($object->a as $element) {
echo $element->f;
}

Accessing json with php

I want to read the json data on the page http://mattrb.com/txt.txt
For example, let's say I want to get the name "Bulbasaur." I have this code:
<?php
$file = file_get_contents("http://mattrb.com/txt.txt");
$json = json_decode($file);
echo $json->1->name;
?>
This code causes the php to simply not load. Is this because you can't use a number? Next I tried this:
<?php
$file = file_get_contents("http://mattrb.com/txt.txt");
$json = json_decode($file);
$num = 1;
echo $json->$num->name;
?>
This allows the php to load, but still returns nothing. What am I doing wrong?
Your json is invalid. Please check at http://jsonlint.com/.
Also you can access "1" in php like this: echo $json->{1}->name;
Your json file isn't valide You have a comma problem item number 135 try to delete it so you can parse the file .
"135": {, //the problem of your json data is here
"levels": [5, 15],
"probability": 4 "name": "Jolteon",
"attack": 65,
"defense": 60,
"type": "electric",
"moves": [
"tackle",
"thundershock",
"thunder"
],
"curve": 1.6
},
Your json should not contain newline and other invalid characters.
In other words - your json is invalid. json_decode does not work on the file.
$file = file_get_contents("http://mattrb.com/txt.txt");
var_dump(json_decode($file));
// NULL

php decode JSON get values

I'm trying to decode JSON format
What I am sending is:
{
"id": 123,
"name": "John",
“surname”: “Smith”,
“department”: 3
}
I am sending POST with data via Postman, in the picture.
So, this is the data I want to decode:
"data_serever_received": "{ \"id\": 123, \"name\": \"john\", “surname”: “Smith”, “department”: 3 }"
I tried
$this->input->data["id"]
but it is not working.
How can I get the id, name, surname and etc. values?
Your JSON is invalid “ and ” are not ".
(Zoom in with your browser if you can't see the difference).
You need to start with valid JSON before you can parse it.
You can use json_decode to make it an array.
$json_array = json_decode($json_object);//$json_object will need to contain your json string.
then you can access like this:
echo $json_array["data"][“surname”];
PHP Doc on json_decode: http://php.net/manual/en/function.json-decode.php

Retrieve Json Decode Elements

I've tried a variety of things, but still have not been able to return username from the output below. How would I do this? I ran this line below and got the string below:
$work = exec($cmd);
var_dump($work);
$json_object = json_decode($work, true);
string(1118) "{"user"=>{"id"=>4151878, "username"=>"jerry_smithjerry", "firstname"=>"Jerry ", "lastname"=>"Smith", "birthday"=>nil, "sex"=>0, "city"=>nil, "state"=>nil, "country"=>nil, "registration_date"=>"2013-07-24T16:12:20-04:00", "about"=>"", "domain"=>"jerry_smithjerry.500px.com", "fotomoto_on"=>false, "locale"=>"en", "show_nude"=>false, "fullname"=>"Jerry Smith", "userpic_url"=>"/graphics/userpic.png", "upgrade_status"=>0, "store_on"=>false, "email"=>"jerry_smithjerry#aol.com", "upload_limit"=>20, "upload_limit_expiry"=>"2013-07-24T21:50:58-04:00", "upgrade_type"=>0, "upgrade_status_expiry"=>nil, "auth"=>{"facebook"=>0, "twitter"=>0}, "contacts"=>{}, "equipment"=>{}, "photos_count"=>0, "affection"=>0, "in_favorites_count"=>0, "friends_count"=>0, "followers_count"=>0, "not_active"=>true, "avatars"=>{"default"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "large"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "small"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "tiny"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}}}}" "
echo:
{"user"=>{"id"=>4151878, "username"=>"jerry_smithjerry", "firstname"=>"Jerry ", "lastname"=>"Smith", "birthday"=>nil, "sex"=>0, "city"=>nil, "state"=>nil, "country"=>nil, "registration_date"=>"2013-07-24T16:12:20-04:00", "about"=>"", "domain"=>"jerry_smithjerry.500px.com", "fotomoto_on"=>false, "locale"=>"en", "show_nude"=>false, "fullname"=>"Jerry Smith", "userpic_url"=>"/graphics/userpic.png", "upgrade_status"=>0, "store_on"=>false, "email"=>"jerry_smithjerry#aol.com", "upload_limit"=>20, "upload_limit_expiry"=>"2013-07-24T22:11:09-04:00", "upgrade_type"=>0, "upgrade_status_expiry"=>nil, "auth"=>{"facebook"=>0, "twitter"=>0}, "contacts"=>{}, "equipment"=>{}, "photos_count"=>0, "affection"=>0, "in_favorites_count"=>0, "friends_count"=>0, "followers_count"=>0, "not_active"=>true, "avatars"=>{"default"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "large"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "small"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "tiny"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}}}}
var_dump:
string(1118) "{"user"=>{"id"=>4151878, "username"=>"jerry_smithjerry", "firstname"=>"Jerry ", "lastname"=>"Smith", "birthday"=>nil, "sex"=>0, "city"=>nil, "state"=>nil, "country"=>nil, "registration_date"=>"2013-07-24T16:12:20-04:00", "about"=>"", "domain"=>"jerry_smithjerry.500px.com", "fotomoto_on"=>false, "locale"=>"en", "show_nude"=>false, "fullname"=>"Jerry Smith", "userpic_url"=>"/graphics/userpic.png", "upgrade_status"=>0, "store_on"=>false, "email"=>"jerry_smithjerry#aol.com", "upload_limit"=>20, "upload_limit_expiry"=>"2013-07-24T22:11:09-04:00", "upgrade_type"=>0, "upgrade_status_expiry"=>nil, "auth"=>{"facebook"=>0, "twitter"=>0}, "contacts"=>{}, "equipment"=>{}, "photos_count"=>0, "affection"=>0, "in_favorites_count"=>0, "friends_count"=>0, "followers_count"=>0, "not_active"=>true, "avatars"=>{"default"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "large"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "small"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "tiny"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}}}}"
Your json string doesn't have valid json, the json strings uses : as a key value separator, in your case you have used a php associative array like => notation. This json is not valid json,
according to this: json_decode returns string type instead of object You may be running json_encode() on a json string. json_encode() is used if you want to turn an object or array into json.
If you are doing something like $work = json_encode($json_string) somewhere before that code, that would be the cause. remove that line, run json_decode() on your original json string and it should be fine.
Once you flesh that out, you can do
$json_object = json_decode($your_json_string);
echo $json_object->user->username; //echo's the username

Extract Data from a Website using PHP

I am trying to get PHP to extract the TOKEN (the uppercase one), USERID (uppercase), and the USER NAME (uppercase) from a web page with the following text.
{
"rsp":{
"stat":"ok",
"auth":{
"token":"**TOKEN**",
"perms":"read",
"user":{
"id":"**USERID**",
"username":"**USER NAME**",
"fullname":"**NAME OF USER**"
}
}
}
}
(This is from the RTM api, getting the authentication token of the user).
How would I go about doing this? Thanks!
EDIT:
how would i get task name "Buy Milk!" & the due date of the task"2011-02-28T.." using json_decode and php here? Thanks!
{
"rsp":{
"stat":"ok",
"tasks":{
"rev":"REV_NUMBER",
"list":{
"id":"ID_NUMBER",
"taskse­­ries":{
"id":"ID_NUMBER",
"created":"2010-11-16T00:01:50Z",
"modified":"2011-02-28T05:09:36Z",
"name":"Buy Milk!",
"source":"js",
"url":"",
"location_id":"",
"rrule":{
"every":"1",
"$t":"FREQ=W­­EEKLY;INTERVAL=1"
},
"tags":[
],
"participants":[
],
"notes":[
],
"task":{
"id":"ID_NUMBER" ­­,
"due":"2011-02-28T05:00:00Z",
"has_due_time":"0",
"added":"2011-02-21T05:04:49Z",
"completed":"",
"deleted":"",
"priority":"2",
"postponed":"0",
"estima­­te":""
}
}
}
}
}
}
As Delan suggested, use json_decode. Here is an example of how you would use json_decode to extract the information you require.
// your json string
$string = '{"rsp":{"stat":"ok","auth":{"token":"**TOKEN**","perms":"read","user":{"id":"**USERID**","username":"**USER NAME**","fullname":"**NAME OF USER**"}}}}';
// parse json string to an array
$array = json_decode($string, true);
// auth token
echo $array['rsp']['auth']['token'];
// user details
echo $array['rsp']['auth']['user']['id'];
echo $array['rsp']['auth']['user']['username'];
echo $array['rsp']['auth']['user']['fullname'];
UPDATE I've updated the code to use json_decode's $assoc parameter to convert from an object to an assoc array.
ANOTHER UPDATE To answer your updated question..
how would i get task name "Buy Milk!" & the due date of the task"2011-02-28T.." using json_decode and php here? Thanks!
This code would work to get the values that you want.
//string(9) "Buy Milk!"
echo $array['rsp']['tasks']['list']['taskseries']['name'];
// string(20) "2011-02-28T05:00:00Z"
echo $array['rsp']['tasks']['list']['taskseries']['task']['due'];
This code isn't ideal, but it gets the job done for you.
If the string is JSON, json_decode in PHP will do the trick. It's was implemented in PHP 5.2.0.
http://www.php.net/manual/en/function.json-decode.php

Categories