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;
Related
I am having issues accessing the nested data from this. I can access the first top level object but not the nested data. Any suggestions, Thnx
{"userInput":{"relaSource":"DailyMed","relas":"has_EPC","drugName":"intuniv"},"rxclassDrugInfoList":{"rxclassDrugInfo":[{"minConcept":{"rxcui":"40114","name":"Guanfacine","tty":"IN"},"rxclassMinConceptItem":{"classId":"N0000175554","className":"Central alpha-2 Adrenergic Agonist","classType":"EPC"},"rela":"has_EPC","relaSource":"DAILYMED"}]}}
my code is as follows (some functions are tests) but i need to access all datapoints..... relaSource, relas, drugName, rxclassDrugInfoList-> rxclassDrugInfo, minConcept, rxcui, name, tty,
<?php
$url = 'https://rxnav.nlm.nih.gov/REST/rxclass/class/byDrugName.json?drugName=intuniv&relaSource=DailyMed&relas=has_EPC'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
echo $data;
echo "<br/>";
$jsonObj = json_decode($data);
echo "<br/>" . $jsonObj->userInput->relaSource;
echo "<br/>" . $jsonObj->userInput->relas;
echo "<br/>" . $jsonObj->userInput->drugName;
echo "<br/>";
echo "A2";
?>
<?php
foreach ($jsonObj as $data) {
$relaSource = $data->relaSource;
$content = $data->relaSource;
?>
<h1> <?php echo $relaSource; ?></h1>
<h1> <?php echo $content; ?> </h1>
<?php } ?>
$rxclassDrugInfoList = $jsonObj['rxclassDrugInfoList'];
echo $rxclassDrugInfoList['rxclassDrugInfo']."<br/>";
/////////////////////////////////////////////////////////////////////////////
<h2>TEST</h2>
<?php
$url = 'https://rxnav.nlm.nih.gov/REST/rxclass/class/byDrugName.json?drugName=intuniv&relaSource=DailyMed&relas=has_EPC'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
// JSON string
//$someJSON = '[{"name":"Jonathan Suh","gender":"male"},{"name":"William Philbin","gender":"male"},{"name":"Allison McKinnery","gender":"female"}]';
// Convert JSON string to Array
$someArray = json_decode($data, true);
print_r($someArray); // Dump all data of the Array
echo $someArray[0]["userInput"]; // Access Array data
///////////////////////////////////////////////////////////////////////////////////////////
// Convert JSON string to Object
$someObject = json_decode($data);
print_r($someObject); // Dump all data of the Object
echo $someObject[0]->userInput; // Access Object data
// Convert JSON string to Array
$someArray = $someObject[0]->userInput;
print_r($someArray); // Dump all data of the Array
echo $someArray[0]["name"]; // Access Array data
echo "<br/>";
echo "<br/>";
echo "<br/>";
?>
I know it is just a problem with addressing items that are nested incorrectly.
**Here is all the datapoints that u needed **
<?php
$json = '{"userInput":{"relaSource":"DailyMed","relas":"has_EPC","drugName":"intuniv"},"rxclassDrugInfoList":{"rxclassDrugInfo":[{"minConcept":{"rxcui":"40114","name":"Guanfacine","tty":"IN"},"rxclassMinConceptItem":{"classId":"N0000175554","className":"Central alpha-2 Adrenergic Agonist","classType":"EPC"},"rela":"has_EPC","relaSource":"DAILYMED"}]}}';
var_dump($json);
var_dump(json_decode($json)->userInput);
var_dump(json_decode($json)->rxclassDrugInfoList);
var_dump(json_decode($json)->userInput->relaSource);
var_dump(json_decode($json)->userInput->relas);
var_dump(json_decode($json)->userInput->drugName);
var_dump(json_decode($json)->rxclassDrugInfoList->rxclassDrugInfo[0]->minConcept->rxcui);
var_dump(json_decode($json)->rxclassDrugInfoList->rxclassDrugInfo[0]->minConcept->name);
var_dump(json_decode($json)->rxclassDrugInfoList->rxclassDrugInfo[0]->minConcept->tty);
var_dump(json_decode($json)->rxclassDrugInfoList->rxclassDrugInfo[0]->rxclassMinConceptItem->classId);
?>
I am new to PHP, I have some data from API in json format.
In php i tried
$response = \Httpful\Request::get($uri)->send();
$json = json_decode($response, true);
foreach($json as $k=>$val):
echo '<b>Name: '.$k.'</b></br>';
endforeach;
echo $response;
echo "<br />";
print_r($json);
echo "<br />";
echo ($json["genres"][0]);
But I am unable to read the array content. Can someone please help me how to parse this PHP array in a loop. Is there any php library to simplify this?
You can try to use the foreach loop in PHP as follows, which worked for me.
foreach(json_data as $key => $val){
echo $key . ":" . $val;
}
I hope it helps.
firstly you wrongly used php's foreach, second you have to use $json['genres'] to access the inner array
foreach($json['genres'] as $k=>$val)
{
echo $k.':'.$val['id'].'_'.$val['name'];
}
How can I parse this JSON, which is supposed to display the items a user has in their Steam inventory.
I have tried this:
$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/');
$json = json_decode($data);
echo $data;
It returns the same as just visiting the link. I can't get anything like this to work either:
$id = $json->type;
echo $type;
This is how to get type
$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/');
$json = json_decode($data);
foreach ($json->rgDescriptions as $mydata)
{
echo $mydata->type;
}
$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/');
$json = json_decode($data);
echo $data;
you are echoing $data that is your input (so you see the same as opening the link directly). To see if the json_decode is working fine you should print $json.
So instead of
echo $data;
use
echo '<pre>'
print_r($json);
echo '</pre>';
$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/');
$json = json_decode($data);
Now $json has 2 objects.
you can access like.
$json->rgInventory;
$json->success;
if you want to fetch all data from $json->rgInventory;
foreach($json->rgInventory as $e){
//var_dump($e);
echo $e->id;
echo $e->classid;
echo $e->instanceid;
}
etc.
I am pretty new to json and arrays and I have the following but I am not getting any results from a Jenkins JSON output.
Link to output - http://resonant-rise.com/mcupdater/parser.php
Code
$url = "http://jenkins.jakimfett.com/job/AlchemyPlusPlus/lastSuccessfulBuild/api/json?pretty=true";
$output = json_decode(file_get_contents($url), true);
print "<pre>";
print_r($output);
print "</pre>";
foreach ($output->actions as $item) {
echo $item->fullDisplayName . "\n";
}
<?php
$url="http://jenkins.jakimfett.com/job/AlchemyPlusPlus/lastSuccessfulBuild/api/json?pretty=true";
$output = json_decode(file_get_contents($url), true);
echo "<pre>";
print_r($output);
echo "</pre>";
/* for properties like fullDisplayName, id, keeplog etc .. directly use echo */
echo $output['fullDisplayName'];
/* for properties like actions and artifacts */
foreach($output['actions'] as $action){
print_r($action); /* actions itself is array so use print_r */
}
?>
I am using the following code to print the output of the jSON response but when I try to print
echo $obj->HotelListResponse->customerSessionId; // This is working.
echo $obj->HotelListResponse->HotelList->HotelSummary->name; // This is not working.
When the response contains only one node then its printing perfectly but when there are multiple nodes with same name then its not printing. I tried using foreach just like the below. I also tried using while loop but still I am unable to print the list of hotel names.
My jSON decoded output is like http://pastebin.com/Fr21DkEk
Total code:
$url = "https://api.eancdn.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=99&apiKey=cbrzfta369qwyrm9t5b8y8kf&locale=en_AU¤cyCode=AUD&xml=<HotelListRequest><city>Brisbane</city><stateProvinceCode>QLD</stateProvinceCode><countryCode>AU</countryCode><arrivalDate>10/16/2014</arrivalDate><departureDate>10/18/2014</departureDate><RoomGroup><Room><numberOfAdults>2</numberOfAdults></Room></RoomGroup><numberOfResults>25</numberOfResults></HotelListRequest>";
$json = file_get_contents($url);
$obj = json_decode($json);
foreach($obj as $val) {
echo $val->HotelListResponse->HotelList->HotelSummary->name;
}
Try this
foreach($obj->HotelListResponse->HotelList->HotelSummary as $val) {
echo $val->name . '<br/>';
}
HotelSummary is an array:
echo $val->HotelListResponse->HotelList->HotelSummary[0]->name;
If you want all of the hotel summaries:
foreach($obj as $val) {
foreach($val->HotelListResponse->HotelList->HotelSummary as $sum) {
echo $sum->name;
}
}
Yes you can directly access them inside the foreach. Like this:
foreach($obj->HotelListResponse->HotelList->HotelSummary as $val) {
// ^^
// since you're interested on just names, you can point it directly on that object, then each of that batch is in `$val`
echo $val->name . '<br/>';
}
// or start from the parent
foreach($obj as $values) {
$customerSessionId = $values->customerSessionId;
echo $customerSessionId . '<hr/>';
$hotelList = $values->HotelList;
foreach($hotelList->HotelSummary as $hotelsummary) {
echo $hotelsummary->name . '<br/>';
}
}