Get Data From Json file into Php - php

hi im trying to get data from json file using php with a url query eg http://192.168.1.104/testing_JSON/zjson2.php?name=nas
The following is my code i dont know the syntax to use for retrieving the query.
Thanks in advance
$name = ( $_GET['$name'] );
$json_string = file_get_contents("http://192.168.1.104/testing_JSON/newjson.json");
$parsed_json = json_decode($json_string, true);
$parsed_json = $parsed_json['contacts'];
//pr($parsed_json);
foreach($parsed_json as $key => $value)
{
echo $value['$name'] . '<br>';
echo $value['name'] . '<br>';
// etc
}
?>

Related

How do i access multiple nested json data

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);
?>

how to echo fixer.io json php

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;

How to parse / read json in PHP

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'];
}

Q: illegal string offset JSON -> PHP

I already view a lot of questions in stackoverflow about my problem, but I didn't find any solution. I don't know where is the problem in my code? I got illegal string offset message at this row:
echo $value["country"] . ", " . $value["competition"] . "<br>";
My full code:
<?php
// file_get_contents call instead
$str = file_get_contents('general.json');
$json = json_decode($str,true);
foreach($json as $key => $value){
echo $value["country"] . ", " . $value["competition"] . "<br>";
}
?>
my json source sample:
["{\"country\":\"America\",\"competition\":\"Copa America\",\"club\":\"BOCA JUNIORS\"}","{\"country\":\"Germany\",\"competition\":\"Bundesliga\",\"club\":\"HANNOVER\"}","{\"country\":\"Asia\",\"competition\":\"JLeague\",\"club\":\"NAGOYA\"}"]
If I view format of the data with var_dump($json) then I see it is an Array. With var_dump($value) then I see there is a string. Please help for me where is the problem in my code, why could not echo value country and value competition?
The problem is that you have json that has been encoded twice, both the array and the elements in the array.
You should avoid doing that, but if you cannot change the source, you need to decode the values as well:
<?php
// file_get_contents call instead
$str = file_get_contents('general.json');
$json = json_decode($str,true);
foreach($json as $key => $value){
// decode the $value string
$value = json_decode($value, true);
echo $value["country"] . ", " . $value["competition"] . "<br>";
}
?>
An example.

Loop JSON String response in PHP Foreach

I'm trying to print values of a JSON response in PHP. Inside the contents are:
data
local
acao
detalhes
Codes:
<?php
header('Content-type: text/html; charset=UTF-8');
$url = "http://developers.agenciaideias.com.br/correios/rastreamento/json/RJ290474594CN";
$c_url = file_get_contents($url);
$c_url = utf8_encode($c_url);
$results = json_decode($c_url);
echo $c_url;
?>
I've tried using foreach but wasn't successful in implementing them in said JSON response.
Here is a JSON String Example:
[{"data":"07\/12\/2014 11:19","local":"CHINA - CHINA\/CN","acao":"postado","detalhes":"-"},{"data":"09\/12\/2014 12:03","local":"CHINA - CHINA\/CN","acao":"encaminhado","detalhes":"Em tr\u00e2nsito para Unidade de Tratamento Internacional - BRASIL\/BR"},{"data":"29\/12\/2014 12:34","local":"UNIDADE TRAT INTERNACIONAL PARANA - Curitiba\/PR","acao":"conferido","detalhes":"Recebido\/Brasil "}]
If you just want to print values just print them accordingly using foreach. After getting the response, use json_decode(). That ut8f_encode() is superfluous.
$url = "http://developers.agenciaideias.com.br/correios/rastreamento/json/RJ290474594CN";
$data = json_decode(file_get_contents($url), true);
foreach($data as $values) {
echo $values['data'] . '<br/>';
echo $values['local'] . '<br/>';
echo $values['acao'] . '<br/>';
echo $values['detalhes'] . '<br/>';
}
Sample Output
Or:
$url = "http://developers.agenciaideias.com.br/correios/rastreamento/json/RJ290474594CN";
$data = json_decode(file_get_contents($url), true);
foreach($data as $values) {
foreach($values as $key => $value) {
echo "{$key}: $value <br/>";
}
echo '<hr/>';
}

Categories