Trying to parse a json - php

After getting a json from Youtube API. I used this code to parse it:
$en = json_encode($json, true);
echo "<pre>";
print_r($en);
echo "</pre>";
here is the result:
{"items":[{"id":"UC-D8SQfw-J-lSWFSbyX6wRg"}]}
I made it clear with an online json parser here's how it looks:
{
"items":[
{
"id":"UC-D8SQfw-J-lSWFSbyX6wRg"
}
]
}
I want to get id value. I have tried $en->items->id but it says Notice: Trying to get property of non-object
how do I get the value of id ?

$en = json_encode($json, true);
$en_b = json_decode($en, true);
echo $en_b['items'][0]['id'];

Related

Get value from Json object (url) in php

I have a json i am fetching remotely in php,
the Json returned is as follow:
{
"base_currency_code":"USD",
"base_currency_name":"United States dollar",
"amount":"1.0000",
"updated_date":"2022-01-30",
"rates":{
"KWD":{
"currency_name":"Kuwaiti dinar",
"rate":"0.3030",
"rate_for_amount":"0.3030"
}
},
"status":"success"
}
the i try to get the specific value from key : rate :
$url='https://...';
$json = file_get_contents($url);
$obj = json_decode($json,true);
echo $obj['rates']['KWD']['rate'];
but I have no output, when I would like to have 0.3030. Why is this?
test.json
{
"base_currency_code":"USD",
"base_currency_name":"United States dollar",
"amount":"1.0000",
"updated_date":"2022-01-30",
"rates":{
"KWD":{
"currency_name":"Kuwaiti dinar",
"rate":"0.3030",
"rate_for_amount":"0.3030"
}
},
"status":"success"
}
test.php
<?php
$url='test.json';
$json = file_get_contents($url);
$obj = json_decode($json,true);
echo $obj['rates']['KWD']['rate'];
Result 0.3030
If it is not working, you cannot access the site remotely.
try it and make sure it works
<?php
$url='https......';
$json = file_get_contents($url);
echo $json;

Error in getting data from Json in PHP

I am trying to access data from json in PHP but it seems not working.
code:
$raw =file_get_contents("http://api.mydomain.com/data.json");
$data = json_decode($raw->list);
echo $data;
I'm getting error that list is not an object.
Here is my json
{ "list" : [ { "data1":" my data"}, {"data2": "my data 2"}]};
What did u do wrong? Also how can i access data1 and others?
You don't need the $raw->list bit and you are getting an object back from json_decode so use print_r and not echo
$raw = file_get_contents("http://api.mydomain.com/data.json");
$data = json_decode($raw);
print_r($data);

How to access specific value in php and json

I'm using a token:
$url = "https://api.pipedrive.com/v1/deals?api_token=3bd884bb0078f836a56f1464097fb71eac9d50ce";
and here's the json text(not everything);
{
"success":true,
"data":[{
"id":1,
"creator_user_id":{
"id":1682756,
"name":"Kamilah",
"email":"kamilah#fractal.ae",
"has_pic":false,
"pic_hash":null,
"active_flag":true,
"value":1682756},
"title":"FFS Organization deal"
}]
}
I wanted to display "title" and I always get an error
Notice: Undefined index: creator_user_id in
C:\xampp\htdocs\pipedrive\getjson.php on line 8
Here's my code so far:
$url = "https://api.pipedrive.com/v1/deals?api_token=3bd884bb0078f836a56f1464097fb71eac9d50ce";
$response = file_get_contents($url);
$object = json_decode($response, true);
echo $object['data']['creator_user_id']['title'];
I'm new to json so I'm just practicing and trying to figure out how to echo a specific value in php. Would be appreciated if you can explain exactly how it works when you echo from json to php.
Thank you! :)
First of all you need to debug the array after using json_decode().
<?php
$url = "https://api.pipedrive.com/v1/deals?api_token=3bd884bb0078f836a56f1464097fb71eac9d50ce";
$response = file_get_contents($url);
$object = json_decode($response, true);
echo "<pre>";
print_r($object); // this will print all data into array format.
?>
As per this array, you do not have title index inside the creator_user_id array. title is a separate index.
Also note that, $object['data'] containing two indexes not one. you can get title as:
<?php
foreach ($object['data'] as $key => $value) {
//print_r($value); // this will print the values inside the data array.
echo $value['title']."<br/>"; // this will print all title inside your array.
}
?>
Result:
FFS Organization deal
Google deal

Foreach array loop for JSON

Here is my PHP/JSON code:
$json_url = "http://dailydota2.com/match-api";
$json = file_get_contents($json_url);
$json=str_replace('},
]',"}
]",$json);
$decoded= json_decode($json);
$data=$decoded->matches[0];
foreach ($data as $value) {
print_r($value->team1->logo_url);
}
Now I have the following problem
Notice: Trying to get property of non-object
and
Notice: Undefined property: stdClass::$team1
I just want use foreach loop and then show my results in HTML.
Why I am getting the 2 mentioned problems and how can I show the correct results?
Ok so the url your are using return VALID JSON, no need to change any of it!
I suggest using arrays, it has always appeared simpler to me
Do you want the logo_url from team or image_url from league? I will show both in my implementation.
So here is some corrected code
$json_url = "http://dailydota2.com/match-api";
$json = file_get_contents($json_url);
$decoded= json_decode($json,true); // True turns it into an array
$data = $decoded['matches'];
foreach ($data as $value) {
//I am not sure which one you want!!!
echo $value['league']['image_url'] . "<br>";
echo $value['team1']['logo_url'] . "<br>";
echo $value['team2']['logo_url'] . "<br>";
}
*EDIT To show wanted implementation by questions author...
$json_url = "http://dailydota2.com/match-api";
$json = file_get_contents($json_url);
$decoded= json_decode($json,true); // True turns it into an array
$data = $decoded['matches'];
foreach ($data as $value) {
echo "
<img src=\"http://dailydota2.com/{$value['team1']['logo_url']}\">
<img src=\"http://dailydota2.com/{$value['team2']['logo_url']}\">
";
}
I have checked your code and have some notes and hopefully a solution:
1- You are trying to get non existing key from JSON data, that is the message telling you.
2- I am still not sure what do you get from the JSON API. But regarding to dailydota2 documentation there is nothing called image_url under team1. I guess you are looking for logo_url or something like that.
3- Do not change the format of JSON as you do in your code, therefore delete following line:
$json=str_replace('}, ]',"} ]",$json);
Just leave the main JSON output from API as default.
4- When you try to get specific key from the decoded JSON/Array just use following way:
$data = $decoded->{'matches'};
in stead of
$data=$decoded->matches[0];
Reference: http://php.net/manual/en/function.json-decode.php
5- And finally your foreach loop is working but needs the correct key:
foreach ($data as $value) {
print_r($value->team1->logo_url);
}
When all these step is done, it should works.
Here is your final corrected code:
$json_url = "http://dailydota2.com/match-api";
$json = file_get_contents($json_url);
$decoded = json_decode($json);
$data = $decoded->{'matches'};
foreach ($data as $value) {
print_r($value->team1->logo_url);
echo '<img src="http://dailydota2.com/' . $value->team1->logo_url . '">';
}
It returns following output, and I do not get any errors.
/images/logos/teams/cdecgaming.png/images/logos/teams/teamempire.png
/images/logos/teams/ehome.png/images/logos/teams/ehome.png
/images/logos/teams/fnatic.png/images/logos/teams/cloud9.png
/images/logos/teams/teamissecret.png/images/logos/teams/teamissecret.png
/images/logos/teams/natusvincere.png/images/logos/teams/fnatic.png
Again I really do not know which information you want to get from the API but here you have a base of working code that you can work further with to get the required data from the right KEYs.

Errors with json_decode in php

We are have json:
{ "list":
[
{"id":"4045","value":"Xin Kai"},
{"id":"4141","value":"YZK"},
{"id":"4099","value":"ZX"}
]
}
For get value we use next code:
$json = json_decode($result, true);
foreach($json['list'] as $item) {
print $item['value'].'<br />';
}
But now we get error: Warning: Invalid argument supplied for foreach()...
Tell me please where error in code and how will be right?
Your issue is that you're doing this in your code:
foreach($json->list as $item) {
When you should be doing this:
foreach($json['list'] as $item) {
As you decoded it as an array and not as an object.
Read More: json_decode()
Also, as zerkms said,
>>> windows-1251 <<< JSON document must be in UTF-8
Before json_decode you need get json in utf-8, in you exmple(if you get json in windows-1251) you need use next code:
EXAMPLE FIRST - if you want get array
//if you want get array
$json_obj = json_decode(iconv("windows-1251","utf-8",$result), true);
foreach($json_obj['list'] as $item) {
print $item['value'].'<br />';
}
EXAMPLE SECOND - if you wnt get object
//if you wnt get object
$json_obj = json_decode(iconv("windows-1251","utf-8",$result));
foreach($json_obj->list as $item) {
print $item->value.'<br />';
}
Enjoy!

Categories