亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 MySQL/PHP 修復 JSON 格式

從 MySQL/PHP 修復 JSON 格式

PHP
人到中年有點甜 2022-05-27 16:54:41
在下面的兩個版本的代碼中,第一個產生結果,但請注意“文本”數組的末尾沒有結束括號,因為應該有。來自其他代碼示例的第二個輸出看起來應該可以工作,但它完全失敗了。我哪里做錯了?當我這樣做時;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"            }         }      }   ]}
查看完整描述

2 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

因此,我首先美化您的 JSON 和 PHP,然后重新格式化您的代碼,以便更容易理解所需的數據層次結構以及編碼的數據層次結構。這是重新格式化:


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


?>

您可以看到“組”數組位于“文本”數組內。在重新格式化您想要的 JSON 之后,我認為這就是您要尋找的內容:


以及產生我認為您正在尋找的輸出的代碼:


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


?>

筆記:

  • 我選擇了這種特殊的格式,因為它是理解層次結構的最簡單方法。通常我不會[像那樣把它放在自己的線上,但在這種情況下它更容易處理。

  • 我選擇[]了數組定義,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


查看完整回答
反對 回復 2022-05-27
?
達令說

TA貢獻1821條經驗 獲得超6個贊

試試這個代碼塊


     $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 結構是您想要的帶有空數據的結構,因為我沒有循環數據


{

  "events": [

    {

      "start_date": {

        "minute": null,

        "hour": null,

        "month": null,

        "day": null,

        "year": null

      },

      "text": {

        "text": null

      },

      "group": {

        "group": null

      }

    }

  ]

}


查看完整回答
反對 回復 2022-05-27
  • 2 回答
  • 0 關注
  • 247 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號