I have from url this
[{"user_id":"3932131","username":"DanielDimitrov","count300":"1677134","count100":"239025","count50":"41207","playcount":"17730","ranked_score":"1413977663","total_score":"7355146958","pp_rank":"35848","level":"95.5852","pp_raw":"1582.26","accuracy":"97.88556671142578","count_rank_ss":"42","count_rank_s":"337","count_rank_a":"120","country":"BG","events":[]}]
and my php is this
$json = file_get_contents('url');
$obj = json_decode($json);
echo $obj->user_id;
echo 'Username: '.$obj['username'].'<br>';
echo 'PP: '.(int)$obj['pp_raw'].'<br>';
echo 'Level: '.(int)$obj['level'].'<br>';
echo 'Play count: '.$obj['playcount'].'<br>';
I try to remove the brackets from the url and the code run but i get it with brackets... How can i remove them?
<?php
$json = '[{"user_id":"3932131","username":"DanielDimitrov","count300":"1677134","count100":"239025","count50":"41207","playcount":"17730","ranked_score":"1413977663","total_score":"7355146958","pp_rank":"35848","level":"95.5852","pp_raw":"1582.26","accuracy":"97.88556671142578","count_rank_ss":"42","count_rank_s":"337","count_rank_a":"120","country":"BG","events":[]}]';
$in = array("[{", "}]");
$out = array("{", "}");
$obj = json_decode(str_replace($in, $out, $json));
echo $obj->user_id;
echo 'Username: '.$obj->username.'<br>';
echo 'PP: '.(int)$obj->pp_raw.'<br>';
echo 'Level: '.(int)$obj->level.'<br>';
echo 'Play count: '.$obj->playcount.'<br>';
?>
$obj is an array with 1 element.
The short fix is, do $obj = $obj[0] first.
Then you can do $obj["user_id"] etc.
Related
Now I have this JSON URL https://vidsrc.me/movies/latest/page-1.json I want to echo IMDB ID, Title, Quality and Embed URL for each array as this sounds like a multidimensional array. But every time I try to do this. I get one of the following errors:
- Undefined Index
- Undefined offset
So I want to loop through it and echo each of those items and break line between each,
This is the code
<?php
$url = file_get_contents('https://vidsrc.me/movies/latest/page-1.json');
$decode = json_decode($url, TRUE);
$res = $decode->result;
foreach($decode as $value){
$imdb = $res->imdb_id;
$title = $res->title;
$quality = $res->quality;
$url = $res->embed_url;
echo $imdb;
echo "<br>";
echo $tite;
echo "<br>";
echo $quality;
echo "<br>";
echo $url;
}
?>
json_decode() returns you array but you process it like object, you should process it as array:
<?php
$url = file_get_contents('https://vidsrc.me/movies/latest/page-1.json');
$decode = json_decode($url, TRUE);
foreach($decode['result'] as $value){
$imdb = $value['imdb_id'];
$title = $value['title'];
$quality = $value['quality'];
$url = $value['embed_url'];
echo $imdb;
echo "<br>";
echo $title;
echo "<br>";
echo $quality;
echo "<br>";
echo $url;
}
?>
Hi I'm trying get a json from fixer.io and then for each rates echo it but cant get it to work.
the code are
<?php
function usd(){
echo 'HEJ test';
$fixer_access_key = my_access_key;
$url= 'https://data.fixer.io/api/latest?access_key=' . $fixer_access_key;
echo $url;
$json = file_get_contents($url);
$data = json_decode($json);
echo $url . "<br>";
echo 'printing json foreach <br>';
foreach($data as $obj){
echo '...';
$prefix = $obj;
echo $prefix;
echo '<br>';}
echo 'done printing json foreach';
}
usd(); ?>
and the result are:
https://data.fixer.io/api/latest?access_key=my_fixer_key
printing json foreach
done printing json foreach
instead of
$data = json_decode($json);
use
$data = json_decode($json, true);
This should allow foreacha to works - however you will only see first level of json object keys (not nested ones). The second parameter of json_decode change result from object to array.
You will also need to change foreach - to following: foreach($data as $key => $obj) and inside it echo $obj to echo $key;.
Here is simplified working example.
ALTERNATIVE SOLUTION
If working foreach is not your goal but rather pretty printed json, then instead use following code:
$json_string = json_encode($data, JSON_PRETTY_PRINT);
echo $json_string;
I have a JSON structure as below
$json = '{"Number1":"{\"answerPhrase\":\"\\nTEXT,\\nTEXT
TEXT\\n\",\"dateUpdatedInMillisecond\":1234}"}';
When trying to extract the text and numbers I can do the first step and it works but the nested JSON has \\n and it does not give the text as output
The PHP code is as below
$result = json_decode($json);
print_r($result);
echo "<br>";
foreach($result as $key=>$value){
echo $key.$value;
echo "<br>";
$result_nest = json_decode($value);
echo $result_nest->answerPhrase;
echo "<br>";
Why can I not get the text in answerphrase? It works when the text does not have \\n
You may try the below one. You can replace the \n with some other characters. If you want to display enter in browser then you can replace \n with . Please try the below code and let me know if it worked for you.
<?php
$json = '{"Number1":"{\"answerPhrase\":\"\\nTEXT,\\nTEXT TEXT\\n\",\"dateUpdatedInMillisecond\":1234}"}';
$result = json_decode($json);
print_r($result);
echo "\n";
foreach($result as $key=>$value){
echo $key.$value;
echo "<br>";
$value = preg_replace("/\\n/", "___n___", $value);
$result_nest = json_decode($value);
$result_nest->answerPhrase = preg_replace("/___n___/", "\n", $result_nest->answerPhrase);
echo $result_nest->answerPhrase;
echo "<br>";
}
Prefer to fix json before decode and then decode the sub json.
you could do second step in a loop
function test2()
{
$string = '{"Number1":"{\"answerPhrase\":\"\\nTEXT,\\nTEXT
TEXT\\n\",\"dateUpdatedInMillisecond\":1234}"}';
$json = $this->decodeComplexJson($string);
$number = $json->Number1;
//Put this in a loop if you want
$decodedNumber = $this->decodeComplexJson($number);
var_dump($decodedNumber);
echo $decodedNumber->answerPhrase;
}
function decodeComplexJson($string) { # list from www.json.org: (\b backspace, \f formfeed)
$string = preg_replace("/[\r\n]+/", " ", $string);
$json = utf8_encode($string);
$json = json_decode($json);
return $json;
}
I am using this code to get related videos. With this code I get title and link of the video. How can I get the thumb of the video?
<?php
$JSON = file_get_contents("http://gdata.youtube.com/feeds/api/videos/FYpunY-gXxU/related?v=2&alt=json");
$JSON_Data = json_decode($JSON);
$title = $JSON_Data->{'feed'}->{'entry'};
for ($i = 1; $i < 25; $i++)
{
echo ($title[$i]->{'title'}->{'$t'}) . "<br />";
echo $title[$i]->{'link'}[0]->{'href'} . "<br /><br />";
}
?>
When using the json_decode, the best way is to print the entire result for yourself and then see the necessary data you have to get...
Do this:
`<?php
$JSON = file_get_contents("http://gdata.youtube.com/feeds/api/videos/FYpunY-gXxU/related?v=2&alt=json");
$JSON_Data = json_decode($JSON);
echo '<pre>';
print_r($JSON_Data);
?>`
You'll get to know what exactly you have to extract...
I leave that to you... :)
I would :-
<?php
$json = file_get_contents("http://gdata.youtube.com/feeds/api/videos/FYpunY-gXxU/related?v=2&alt=json");
$json_data = json_decode($json, true);
foreach((array)$json_data['feed']['entry'] as $video){
echo $video['title']['$t'].'<br/>';
echo $video['link'][0]['href'].'<br/>';
?>
<img src="<?php echo $video['media$group']['media$thumbnail'][0]['url']?>" />
<img src="<?php echo $video['media$group']['media$thumbnail'][1]['url']?>" />
<?php
}
?>
<?php
$json = file_get_contents('http://tiny.cc/ttrhelp');
$obj = json_decode($json);
$example = $obj->rooms->displayName;
?>
Name: <?php echo $example; ?>
Trying to show the value for 'displayName' but its not showing
Untested code:
<?php
$json = file_get_contents('http://pub.tapulous.com/tapplications/coresocial/v1/chat/api/index.php?method=room_list');
$obj = json_decode($json);
foreach($obj->rooms as $room){
$example = $room->displayName;
echo $example;
}
?>
You probably want $obj->rooms[0]->displayName.
CodePad.
echo $obj->rooms[2]->displayName;
Try
echo $obj->rooms[0]->displayName;