选择特定值时,PHP上不显示JSON数据

选择特定值时,PHP上不显示JSON数据,php,json,Php,Json,我有一个json文件,需要解析为php,只显示一些需要的值 <?php // Read JSON file $readjson = file_get_contents('output.json') ; //Decode JSON $data = json_decode($readjson, true); //Print data echo "<br/><br/> Employee names are: <br/>"; //Par

我有一个json文件,需要解析为php,只显示一些需要的值

<?php
// Read JSON file
$readjson = file_get_contents('output.json') ;

//Decode JSON
$data = json_decode($readjson, true);

//Print data
echo "<br/><br/> Employee names are: <br/>";

//Parse the employee name
foreach ($data as $emp) {
  echo $emp['category.name']."<br/>";
}
?>
我已经测试过了,但是没有显示任何数据,我也找不到问题。可能问题是我找不到数据的确切路径?

您的json格式错误,您可以使用任何类似的工具进行验证 另外,我为你格式化了{bizCode:10000,message:00,data:[{id:sr:contraction:27070,events:[{homeTeamName:Independent Santa Fe,awayTeamName:America de Cali,sport:{name:Football,category:{name:Colombia,Toraction:{name:Primera A,Apertura}}}}}},{homeTeamName:AD Cali,awayTeamName:Millonarios FC,sport:{name:Football,category:{name:Colombia,Toraction:{name:Primera A,Apertura}]}


您可以将其替换为json文件并打印_r$data

这正是您的问题所在。您需要的数据位于“事件”下,而不是“数据”下,因此您必须更深一层。此外,这也是无效的PHP语法:$emp['category.name']-您似乎将JS混入其中。对多个索引的正确PHP数组访问是将这些索引依次放置:$emp['category']['name']。可能还值得一提的是,您在问题中提到了员工姓名,但您的JSON似乎保存了足球比赛数据。可能您显示了错误的JSON示例?或者您的代码没有提取正确的文件?@El_Vanja JSON和代码是正确的,我只是在其中添加了一些以前工作过的其他项目的文本。我已经更改了这里的代码//读取JSON文件$readjson=file_get_contents'output.JSON';//解码JSON$events=JSON\u解码$readjson,true;//打印数据echo Print:;//将每个$events的员工名称解析为$emp{echo$emp['category']['name']。}请通过编辑问题来添加任何更改编辑链接可以在文章底部找到,而不是在评论中找到。这是因为您忽略了我评论的前半部分。您的迭代级别不正确。请阅读链接问题,它有详细说明。
    {
   "bizCode":10000,
   "message":"0#0",
   "data":[
      {
         "id":"sr:tournament:27070",
         "events":[
            {
               "homeTeamName":"Independiente Santa Fe",
               "awayTeamName":"America de Cali",
               "sport":{
                  "name":"Football",
                  "category":{
                     "name":"Colombia",
                     "tournament":{
                        "name":"Primera A, Apertura"
                     }
                  }
               },
            },
            {
               "homeTeamName":"AD Cali",
               "awayTeamName":"Millonarios FC",
               "sport":{
                  "name":"Football",
                  "category":{
                     "name":"Colombia",
                     "tournament":{
                        "name":"Primera A, Apertura"
                     }
                  }
               },
            }
         ],
         "categoryName":"Colombia",
      }
   ]
}