简体   繁体   English

从 MySQL/PHP 修复 JSON 格式

[英]Fix JSON Format from MySQL/PHP

In the two versions of code below, the first produces results but note that there is no end bracket at the end of the "text" array as there should be.在下面的两个版本的代码中,第一个产生了结果,但请注意“text”数组的末尾没有结束括号。 The second output which comes from the other code example SEEMS like it should work but it fails completely.来自其他代码示例 SEEMS 的第二个输出应该可以工作,但它完全失败了。 Where did I go wrong?我哪里做错了?

when I do this;当我这样做时;

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

I get this:我明白了:

    {
   "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"
            }
         }
      }
   ]
}

But what I need is this:但我需要的是这个:

{
   "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"
         }
      }
   ]
}

I have also tried:我也试过:

 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]),
            array('text' => 
                array('text'  => $row[text]),
            array('group' =>
                array('group' => $row[callsign]))
        ; 
    }
$data = array("events" =>  $json_array);
  echo json_encode($data);

But that doesn't work at all.但这根本行不通。 What am I doing wrong.我究竟做错了什么。

So, I started by prettifying your JSON & PHP, then reformatting your code, to make it easier to understand the desired data hierarchy as well as the coded data hierarchy.因此,我开始美化您的 JSON 和 PHP,然后重新格式化您的代码,以便更容易理解所需的数据层次结构以及编码数据层次结构。 Here's the reformat:这是重新格式化:

<?php
foreach ($db_found->query($sql) as $row) {
    $json_array[] = 
    [
        'start_date' => 
        [
            'minute' => $row['min'],
            'hour' => $row['hour'],
            'month' => $row['mo'],
            'day' => $row['day'],
            'year' => $row['yr']
        ],
        'text' => 
        [
            'text' => $row['text'],
            'group' => 
            [
                'group' => $row['callsign']
            ]
        ]
    ];
}
$data = ["events" => $json_array];
echo json_encode($data);

?>

And you can see that the 'group' array is inside the 'text' array.您可以看到“group”数组位于“text”数组内。 And after reformatting your desired JSON, I think this is what you're looking for:在重新格式化你想要的 JSON 之后,我认为这就是你要找的:

And the code that produces the output I think you're looking for:以及产生我认为您正在寻找的输出的代码:

<?php
foreach ($db_found->query($sql) as $row) {
    $data["events"][] = 
    [
        'start_date' => 
        [
            'minute' => $row['min'],
            'hour' => $row['hour'],
            'month' => $row['mo'],
            'day' => $row['day'],
            'year' => $row['yr']
        ],
        'text' => 
        [
            'text' => $row['text']
        ],
        'group' => 
        [
            'group' => $row['callsign']
        ]
    ];
}

echo json_encode($data);

?>

Notes:笔记:

  • I chose this particular formatting because it was the easiest way to understand the hierarchy.我选择这种特殊格式是因为它是理解层次结构的最简单方法。 Usually I don't put the [ on it's own line like that, but it made it easier to process in this case.通常我不会像这样将[放在它自己的行上,但在这种情况下它更容易处理。

  • I chose [] for array definitions over array() because it's much less visual noise, thus easier to understand我选择[]作为数组定义而不是array()因为它的视觉噪音要少得多,因此更容易理解

  • I used $data["events"][] = in the loop because I think it makes it clearer what data you're defining.我在循环中使用了$data["events"][] = ,因为我认为它可以更清楚地说明您定义的数据。 Alternatively, I might do $events[] = or $eventsJson[] = so the variable makes it clear what information it should be holding, then do the $data=['events'=>$eventsJson];或者,我可能会执行$events[] =$eventsJson[] =以便变量明确它应该保存什么信息,然后执行$data=['events'=>$eventsJson];

  • $row[string] is not forward compatible. $row[string]不向前兼容。 See https://www.php.net/manual/en/language.types.array.php#language.types.array.foo-barhttps://www.php.net/manual/en/language.types.array.php#language.types.array.foo-bar

try this code block试试这个代码块

     $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]), // this is the change
                   'group' =>
                           array('group' => $row[callsign]) // so here matching bracket must be maintain 
               ); 

OUTP Structure is your desired one with null data because i don't have looping data OUTP 结构是您想要的带有空数据的结构,因为我没有循环数据

{
  "events": [
    {
      "start_date": {
        "minute": null,
        "hour": null,
        "month": null,
        "day": null,
        "year": null
      },
      "text": {
        "text": null
      },
      "group": {
        "group": null
      }
    }
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM