从 MySQL/PHP 修复 JSON 格式

在下面的两个版本的代码中,第一个产生结果,但请注意“文本”数组的末尾没有结束括号,因为应该有。来自其他代码示例的第二个输出看起来应该可以工作,但它完全失败了。我哪里做错了?


当我这样做时;


foreach($db_found->query($sql) as $row) {

        $json_array[] = 


            array('start_date' => 

                array('minute' => $row[min], 'hour' => $row[hour], 

                    'month' => $row[mo], 'day' => $row[day], 'year' => $row[yr]),

              'text' => 

                array('text'  => $row[text],

              'group' =>

                array('group' => $row[callsign])))

        ; 

    }

$data = array("events" =>  $json_array);

  echo json_encode($data);

我明白了:


    {

   "events":[

      {

         "start_date":{

            "minute":"42",

            "hour":"18",

            "month":"11",

            "day":"11",

            "year":"2019"

         },

         "text":{

            "text":"BILL SWEENEY Opened the net from 65.255.143.178 on 146.655MHz, PL94.8Hz",

            "group":{

               "group":"W0WTS"

            }

         }

      },

      {

         "start_date":{

            "minute":"42",

            "hour":"18",

            "month":"11",

            "day":"11",

            "year":"2019"

         },

         "text":{

            "text":"Peculiar: Clear, 19.9F, wind: N @ 15, humidity: 54%",

            "group":{

               "group":"GENCOMM"

            }

         }

      }

   ]

}


人到中年有点甜
浏览 226回答 2
2回答

慕容3067478

因此,我首先美化您的 JSON 和 PHP,然后重新格式化您的代码,以便更容易理解所需的数据层次结构以及编码的数据层次结构。这是重新格式化:<?phpforeach ($db_found->query($sql) as $row) {&nbsp; &nbsp; $json_array[] =&nbsp;&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'start_date' =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'minute' => $row['min'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'hour' => $row['hour'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'month' => $row['mo'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'day' => $row['day'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'year' => $row['yr']&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; 'text' =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'text' => $row['text'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'group' =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'group' => $row['callsign']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; ];}$data = ["events" => $json_array];echo json_encode($data);?>您可以看到“组”数组位于“文本”数组内。在重新格式化您想要的 JSON 之后,我认为这就是您要寻找的内容:以及产生我认为您正在寻找的输出的代码:<?phpforeach ($db_found->query($sql) as $row) {&nbsp; &nbsp; $data["events"][] =&nbsp;&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'start_date' =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'minute' => $row['min'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'hour' => $row['hour'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'month' => $row['mo'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'day' => $row['day'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'year' => $row['yr']&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; 'text' =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'text' => $row['text']&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; 'group' =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'group' => $row['callsign']&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; ];}echo json_encode($data);?>笔记:我选择了这种特殊的格式,因为它是理解层次结构的最简单方法。通常我不会[像那样把它放在自己的线上,但在这种情况下它更容易处理。我选择[]了数组定义,array()因为它的视觉噪音要少得多,因此更容易理解我$data["events"][] =在循环中使用了,因为我认为它使您定义的数据更加清晰。或者,我可能会这样做,$events[] =或者$eventsJson[] =让变量清楚地表明它应该持有什么信息,然后执行$data=['events'=>$eventsJson];$row[string]不向前兼容。见https://www.php.net/manual/en/language.types.array.php#language.types.array.foo-bar

达令说

试试这个代码块&nbsp; &nbsp; &nbsp;$json_array[] =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'start_date' =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'minute' => $row[min],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'hour' => $row[hour],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'month' => $row[mo],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'day' => $row[day],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'year' => $row[yr]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'text' =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;array('text'&nbsp; => $row[text]), // this is the change&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'group' =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;array('group' => $row[callsign]) // so here matching bracket must be maintain&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;);&nbsp;OUTP 结构是您想要的带有空数据的结构,因为我没有循环数据{&nbsp; "events": [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "start_date": {&nbsp; &nbsp; &nbsp; &nbsp; "minute": null,&nbsp; &nbsp; &nbsp; &nbsp; "hour": null,&nbsp; &nbsp; &nbsp; &nbsp; "month": null,&nbsp; &nbsp; &nbsp; &nbsp; "day": null,&nbsp; &nbsp; &nbsp; &nbsp; "year": null&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; "text": {&nbsp; &nbsp; &nbsp; &nbsp; "text": null&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; "group": {&nbsp; &nbsp; &nbsp; &nbsp; "group": null&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; ]}
打开App,查看更多内容
随时随地看视频慕课网APP