am having issues trying to get contents of this json, am using php.
{"song":{"returncode":"200","returnmsg":"OK","title":"Despacito Ft. Justin Bieber","artist":"Luis Fonsi, Daddy Yankee","album":"","size":"9193600","url":"http://sami-server.info/Bita6/04.96/Billboard%20Hot%20100%20Singles/Billboard%20Hot%20100%20Singles/01.%20Luis%20Fonsi%2C%20Daddy%20Yankee%20-%20Despacito%20ft.%20Justin%20Bieber.mp3","time":"1499079781","date":"Jul 3, 2017","source":"","active":"1","albumart":"https://images-na.ssl-images-amazon.com/images/I/5150NxehQtL._AC_US160_.jpg","speed":"21","counter":"850850"}
this is my function, that returns the json
public function getSong($id) {
$song_url = 'http://databrainz.com/api/data_api_new.cgi?jsoncallback=&id='.$id.'&r=mpl&format=json&_=';
$api = Api::getSpotOn($song_url);
$song = $api->{'song'};
return $song;
}
I want to get all it content.
Thanks!
In the json example you provided a final '}' is missing and the json_decode will return Null without it as it's non a valid json.
But, since you have the getSong method the following should work:
$retObject = json_decode(getSong($songId));
$song = $retObject->song;
print_r($song);
echo 'returncode:'.$song->returncode.'<br/>';
echo 'returnmsg:'.$song->returnmsg.'<br/>';
echo 'title:'.$song->title.'<br/>';
echo 'artist:'.$song->artist.'<br/>';
echo 'album:'.$song->album.'<br/>';
echo 'size:'.$song->size.'<br/>';
echo 'url:'.$song->url.'<br/>';
echo 'time:'.$song->time.'<br/>';
echo 'date:'.$song->date.'<br/>';
echo 'source:'.$song->source.'<br/>';
echo 'active:'.$song->active.'<br/>';
echo 'albumart:'.$song->albumart.'<br/>';
echo 'speed:'.$song->speed.'<br/>';
echo 'counter:'.$song->counter.'<br/>';
try the following:
$result = json_decode(
'{
"song": {
"returncode": "200",
"returnmsg": "OK",
"title": "Despacito Ft. Justin Bieber",
"artist": "Luis Fonsi, Daddy Yankee",
"album": "",
"size": "9193600",
"url": "http://sami-server.info/Bita6/04.96/Billboard%20Hot%20100%20Singles/Billboard%20Hot%20100%20Singles/01.%20Luis%20Fonsi%2C%20Daddy%20Yankee%20-%20Despacito%20ft.%20Justin%20Bieber.mp3",
"time": "1499079781",
"date": "Jul 3, 2017",
"source": "",
"active": "1",
"albumart": "https://images-na.ssl-images-amazon.com/images/I/5150NxehQtL._AC_US160_.jpg",
"speed": "21",
"counter": "850850"
}
}'
);
var_dump($result);
echo $result->song->title;
Related
This is the code, it use a foreach to access to department and workers .
EDIT: Will correct several paste errors
<?php
$json = '{
"boss": "Jeff",
"department": [{
"office": "1111",
"workers": "[{\"id_work\":\"123\",\"name\":\"mike\",\"mobile\":\"12345\"}]"
}]}';
$json_data = json_decode($json);
echo "Boss:".$json_data->boss;
echo "<br>";
foreach($json_data->deparment as $dep)
{
echo "Office number:".$dep->office."<br>";
foreach($dep->workers as $worker){
echo "Worker ID: ".$worker->id_work."<br>";
echo "Worker name : ".$worker->name."<br>";
echo "Worker mobil: ".$worker->mobil."<br>";
}
}
?>
I cannot access to internal array when I try do a foreach() this happens :
Invalid argument supplied for foreach()
How I can access to the information of the nested array
The json data inside $json is wrong. The decode didn't gave errors but the array you get back is not how you want it to be, that's why you get errors after json_decode.
The error you got was because the worker value is in string format.
You should update that:
From: "workers": "[{\"id_work\":\"123\",\"name\":\"mike\",\"mobile\":\"12345\"}]"
To: "workers": [{ "id_work": "123", "name": "mike", "mobile":"12345"}]
End result:
$json = '{
"boss": "Jeff",
"department": [{
"office": "1111",
"workers": [{ "id_work": "123", "name": "mike", "mobile":"12345"}]
}]}';
$json_data = json_decode($json);
echo "Boss:".$json_data->boss;
echo "<br>";
foreach($json_data->department as $dep)
{
echo "Office number:".$dep->office."<br>";
foreach($dep->workers as $worker){
echo "Worker ID: ".$worker->id_work."<br>";
echo "Worker name : ".$worker->name."<br>";
echo "Worker mobil: ".$worker->mobile."<br>";
}
}
Okay so bascially I have this structure as JSon from an API:
{
"tag": "8L9L9GL",
"name": "Jo͛hͥn̽",
"trophies": 5338,
"rank": 291,
"arena": {
"name": "Master II",
"arena": "League 5",
"arenaID": 17,
"trophyLimit": 5200
},
"clan": {
"tag": "2U2GGQJ",
"name": "Reddit Bravo",
"role": "coLeader",
"donations": 0,
"donationsReceived": 0,
"donationsDelta": 0,
"badge": {
"name": "A_Char_Rocket_02",
"category": "03_Royale",
"id": 16000167,
"image": "https://cr-api.github.io/cr-api-assets/badges/A_Char_Rocket_02.png"
}
},
I'm trying to get the Clan -> Name but it isn't working with this code in PHP:
echo "<p style='font-size:75;'>".$json->clan['name']."</p>";
How could I make it work?
Try this:
$json->clan->name
<?php
$json = '{
"tag": "8L9L9GL",
"name": "Jo͛hͥn̽",
"trophies": 5338,
"rank": 291,
"arena": {
"name": "Master II",
"arena": "League 5",
"arenaID": 17,
"trophyLimit": 5200
},
"clan": {
"tag": "2U2GGQJ",
"name": "Reddit Bravo",
"role": "coLeader",
"donations": 0,
"donationsReceived": 0,
"donationsDelta": 0,
"badge": {
"name": "A_Char_Rocket_02",
"category": "03_Royale",
"id": 16000167,
"image": "https://cr-api.github.io/cr-api-assets/badges/A_Char_Rocket_02.png"
}
}
}';
$json = json_decode($json);
echo $json->clan->name;
// Reddit Bravo
If you want to use $json['clan']['name']; then you have to set the assoc param like:
$json = json_decode($json, true);
echo $json['clan']['name'];
// Reddit Bravo
Depending on your particular call to json_decode(), if you've used...
$json = json_decode($jsonData,true);
then use
echo "<p style='font-size:75;'>".$json['clan']['name']."</p>";
Else if you've used...
$json = json_decode($jsonData);
then use
echo "<p style='font-size:75;'>".$json->clan->name."</p>";
If using the first call, then you create an associative array and so use array access method. If you don't pass true as the second parameter, this creates the structure as objects and so use the second method of access.
You will have to json_decode the same and use it.
Following is the piece of code
echo json_decode($json)->clan->name;
Stitch it into your HTML as
echo "<p style='font-size:75;'>".json_decode($json)->clan->name."</p>";
Tested this snippet. Should work for you too.
I am accessing an API where the JSON data is returned as below.
{
"name": "",
"count": 4,
"frequency": "",
"version": 3,
"newdata": true,
"lastrunstatus": "success",
"thisversionstatus": "success",
"thisversionrun": "",
"results": {
"collection": [{
"textlink": {
"href": "http://text1.com",
"text": "Text 1"
},
"index": 1,
"url": ""
}, {
"textlink": {
"href": "http://text2.com",
"text": "Text 2"
},
"index": 2,
"url": ""
}, {
"textlink": {
"href": "http://text3.com",
"text": "Text 3"
},
"index": 3,
"url": ""
}, {
"textlink": {
"href": "http://text4.com",
"text": "Text 4"
},
"index": 4,
"url": ""
}]
}}
As you can see the JSON tree returned has multi-step levels. I am wanting to be able to take some of the deeper levels, in PHP, and insert into a database.
Currently I am using this code to try and echo the data (once I am able to I can then work on inserting it to a database no problem)
<?php
$request = "API.REQUEST.NET";
$response = file_get_contents($request);
$results = json_decode($response, TRUE);
foreach($results as $item) {
echo $item->results[0]->collection[0]->textlink[0]->href;
echo "<br>";
echo $item->results->collection['href'];
echo "<br>";
echo $item->results->collection['text'];
}
?>
As you can see above I have tried several ways to access the deeper levels f data that are being displayed but with no avail.
I am currently getting errors of 'trying to get property of a non-object'. How is it possible to reach the data in this array?
try:
echo $results['results']['collection'][0]['textlink']['href'];
$obj = json_decode( $json, true ); foreach ( $obj['key'] as $key => $value ) { echo $value; }
foreach ($response['results']['collection'] as $textlink) {
$row = $textlink['textlink'];
echo $row['href'];
echo "<br>";
echo $row['text'];
echo "<br>";
}
you can do foreach like this, which loops only items in collection
I would suggest to do something like this (According to me, correct way to fetch results from API responses):
<?php
$request = "API.REQUEST.NET";
$response = file_get_contents($request);
$response = json_decode($response);
foreach($response->results->collection as $item) {
echo $item->textlink->href;
echo "<br>";
echo $item->textlink->text;
echo "<br>";
echo $item->index;
echo "<br>";
echo $item->url;
}
?>
I need some help accessing an api. http://market.huobi.com/staticmarket/detail.html
in PHP.
$json = file_get_contents("http://market.huobi.com/staticmarket/detail.html");
$obj = json_decode($json);
Below is a small sample of the api response.
$obj =
{
"sells": [
{
"price": 3840,
"level": 0,
"amount": 1
},
{
"price": "3840.05",
"level": 0,
"amount": 0.287
},
{
"price": 3841,
"level": 0,
"amount": 0.1
} ],
"p_new": 3838,
"level": -72.12,
"amount": 82792,
"total": 321774060.34653,
"amp": -2,
"p_open": 3910.12,
"p_high": 3925,
"p_low": 3809.99,
"p_last": 3910.12
}
echo "Ask " . $obj->sells[0]->price; // does not work
echo "Volume" . $obj->amount;// does not work
Help will be appreciated.
The API you're calling is returning JSONP data, not JSON data.
JSONP data looks like:
somefunction(JSONdata)
You need to remove the function call wrapper.
$jsonp = file_get_contents("http://market.huobi.com/staticmarket/detail.html");
preg_match('/^.*?\((.*)\)/s', $jsonp, $match);
$json = $match[1];
$obj = json_decode($json);
echo "Ask " . $obj->sells[0]->price . '<br>';
echo "Volume " . $obj->amount;
I would like to know how to count how many people follows someone in Instagram and place the number in a var, Instagram gives you this link:
https://api.instagram.com/v1/users/3/followed-by?access_token=xxxxxxxxx.xxxxxxxxxxxxxxxxxxxx
And displays a result like so
{
"data": [{
"username": "meeker",
"first_name": "Tom",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_6623_75sq.jpg",
"id": "6623",
"last_name": "Meeker"
},
{
"username": "Mark",
"first_name": "Mark",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_29648_75sq_1294520029.jpg",
"id": "29648",
"last_name": "Shin"
},
{
"username": "nancy",
"first_name": "Nancy",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_13096_75sq_1286441317.jpg",
"id": "13096",
"last_name": "Smith"
}]
}
How can I count how many are there and place it in a var, lets say:
<? echo "You are been follow by ".$followers." users!"; ?>
To display: You are been follow by 3 users!
You would need to use json_decode to to decode the JSON response, then access the resulting object's data attribute (an array of 'follower' objects), and count that:
$json = '{
"data": [{
"username": "meeker",
"first_name": "Tom",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_6623_75sq.jpg",
"id": "6623",
"last_name": "Meeker"
},
{
"username": "Mark",
"first_name": "Mark",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_29648_75sq_1294520029.jpg",
"id": "29648",
"last_name": "Shin"
},
{
"username": "nancy",
"first_name": "Nancy",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_13096_75sq_1286441317.jpg",
"id": "13096",
"last_name": "Smith"
}]
}';
$json = json_decode($json);
echo "You have " .count($json->data) ." followers"
OR
$json = json_decode($json,true);
echo "You have " .count($json['data']) ." followers"
You are getting as a json string, you need to decode it using json_decode.
$data = json_decode($string,true);
$followers = count($data['data']);
CodePad Demo.
You need to use json_decode() which will return you a PHP array. Then all you need to do is to count() all the values in the array with 'data' key.
You can use json_decode
$array = json_decode($str);
Then give
echo count($array);
It will give total count of users
simple way to count entries returned asJSON
echo count(json_decode($followers);
Use json_decode() to create a PHP array from the JSON. Then you can simply do a count() on that:
$jsonData = json_decode($yourAPIResult);
echo count($jsonData->data);
Be aware, however, that you probably should set up some error handling in case the API did not return a proper JSON string. So something like this might be better:
if (is_null($jsonData) || !property_exists($jsonData, 'data')) {
echo '?';
} else {
echo count($jsonData->data);
}