Как получить доступ к нескольким вложенным данным JSON - PullRequest
0 голосов
/ 09 января 2019

У меня проблемы с доступом к вложенным данным из этого. Я могу получить доступ к первому объекту верхнего уровня, но не к вложенным данным. Любые предложения, 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"}]}}

мой код выглядит следующим образом (некоторые функции являются тестами), но мне нужно получить доступ ко всем точкам данных ..... 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/>";


?>

Я знаю, что это просто проблема с адресацией элементов, которые вложены неправильно.

1 Ответ

0 голосов
/ 09 января 2019

** Вот все точки данных, которые вам нужны **

<?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);
?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...