我如何访问多个嵌套的json数据

时间:2019-01-09 11:11:34

标签: php json nested

我在从中访问嵌套数据时遇到问题。我可以访问第一个顶级对象,但不能访问嵌套数据。有什么建议,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,名称,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 :(得分:0)

**这是您需要的所有数据点**

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