【问题标题】:PHP Iterate through JSON and save via T-SQLPHP 遍历 JSON 并通过 T-SQL 保存
【发布时间】:2021-12-09 03:44:49
【问题描述】:

我有 PHP 代码从我订阅的网站返回以下值,我的问题是如何循环遍历这个数组并回显每一行?除此之外,如果可能的话,我想将每条记录插入到 MS-SQL 数据库中,最好是通过批量插入。

{
  "count": 8,
  "messages": [
    {
      "t": 1638413506,
      "f": 1073743361,
      "tp": "evt",
      "et": "(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.026467, 121.721675\nTime: 2021-12-02 10:51:46\nhttp://maps.google.com/?q=17.026467,121.721675",
      "x": 121.721674983,
      "y": 17.0264666677,
      "rt": 0,
      "p": {}
    },
    {
      "t": 1638415192,
      "f": 1073743361,
      "tp": "evt",
      "et": "(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.023507, 121.721527\nTime: 2021-12-02 11:19:52\nhttp://maps.google.com/?q=17.023507,121.721527",
      "x": 121.721526655,
      "y": 17.0235066672,
      "rt": 0,
      "p": {}
    },
    {
      "t": 1638416233,
      "f": 1073743361,
      "tp": "evt",
      "et": "(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.023335, 121.721730\nTime: 2021-12-02 11:37:13\nhttp://maps.google.com/?q=17.023335,121.721730",
      "x": 121.721729978,
      "y": 17.0233349999,
      "rt": 0,
      "p": {}
    },
    {
      "t": 1638426180,
      "f": 1073743361,
      "tp": "evt",
      "et": "(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.023990, 121.721965\nTime: 2021-12-02 14:23:00\nhttp://maps.google.com/?q=17.023990,121.721965",
      "x": 121.721965027,
      "y": 17.0239899993,
      "rt": 0,
      "p": {}
    },
    {
      "t": 1638430480,
      "f": 1073743361,
      "tp": "evt",
      "et": "(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.014735, 121.721372\nTime: 2021-12-02 15:34:40\nhttp://maps.google.com/?q=17.014735,121.721372",
      "x": 121.721371651,
      "y": 17.0147350003,
      "rt": 0,
      "p": {}
    },
    {
      "t": 1638439534,
      "f": 1073743361,
      "tp": "evt",
      "et": "(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.014727, 121.721405\nTime: 2021-12-02 18:05:34\nhttp://maps.google.com/?q=17.014727,121.721405",
      "x": 121.721405029,
      "y": 17.0147266666,
      "rt": 0,
      "p": {}
    },
    {
      "t": 1638450807,
      "f": 1073743361,
      "tp": "evt",
      "et": "(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Dalig, Burgos, Isabela, Philippines\nCoordinates: 17.014760, 121.722102\nTime: 2021-12-02 21:13:27\nhttp://maps.google.com/?q=17.014760,121.722102",
      "x": 121.722101657,
      "y": 17.0147599995,
      "rt": 0,
      "p": {}
    },
    {
      "t": 1638467462,
      "f": 1073743361,
      "tp": "evt",
      "et": "(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.015082, 121.721735\nTime: 2021-12-03 01:51:02\nhttp://maps.google.com/?q=17.015082,121.721735",
      "x": 121.721735001,
      "y": 17.0150816669,
      "rt": 0,
      "p": {}
    }
  ]
}

【问题讨论】:

  • 您的 SQL Server 版本是多少?如果是 SQL Server 2016 或更高版本,您可以将整个 JSON 传递给存储过程,以便一次性加载所有内容,即批量插入。

标签: php sql-server


【解决方案1】:

JSON 一口气。

Fiddle

SQL

DECLARE @json NVARCHAR(MAX) =
N'[
    {
        "count": 8,
        "messages": [
            {
                "t": 1638413506,
                "f": 1073743361,
                "tp": "evt",
                "et": "(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.026467, 121.721675\nTime: 2021-12-02 10:51:46\nhttp://maps.google.com/?q=17.026467,121.721675",
                "x": 121.721674983,
                "y": 17.0264666677,
                "rt": 0,
                "p": {}
            },
            {
                "t": 1638415192,
                "f": 1073743361,
                "tp": "evt",
                "et": "(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.023507, 121.721527\nTime: 2021-12-02 11:19:52\nhttp://maps.google.com/?q=17.023507,121.721527",
                "x": 121.721526655,
                "y": 17.0235066672,
                "rt": 0,
                "p": {}
            },
            {
                "t": 1638416233,
                "f": 1073743361,
                "tp": "evt",
                "et": "(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.023335, 121.721730\nTime: 2021-12-02 11:37:13\nhttp://maps.google.com/?q=17.023335,121.721730",
                "x": 121.721729978,
                "y": 17.0233349999,
                "rt": 0,
                "p": {}
            },
            {
                "t": 1638426180,
                "f": 1073743361,
                "tp": "evt",
                "et": "(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.023990, 121.721965\nTime: 2021-12-02 14:23:00\nhttp://maps.google.com/?q=17.023990,121.721965",
                "x": 121.721965027,
                "y": 17.0239899993,
                "rt": 0,
                "p": {}
            },
            {
                "t": 1638430480,
                "f": 1073743361,
                "tp": "evt",
                "et": "(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.014735, 121.721372\nTime: 2021-12-02 15:34:40\nhttp://maps.google.com/?q=17.014735,121.721372",
                "x": 121.721371651,
                "y": 17.0147350003,
                "rt": 0,
                "p": {}
            },
            {
                "t": 1638439534,
                "f": 1073743361,
                "tp": "evt",
                "et": "(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.014727, 121.721405\nTime: 2021-12-02 18:05:34\nhttp://maps.google.com/?q=17.014727,121.721405",
                "x": 121.721405029,
                "y": 17.0147266666,
                "rt": 0,
                "p": {}
            },
            {
                "t": 1638450807,
                "f": 1073743361,
                "tp": "evt",
                "et": "(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Dalig, Burgos, Isabela, Philippines\nCoordinates: 17.014760, 121.722102\nTime: 2021-12-02 21:13:27\nhttp://maps.google.com/?q=17.014760,121.722102",
                "x": 121.722101657,
                "y": 17.0147599995,
                "rt": 0,
                "p": {}
            },
            {
                "t": 1638467462,
                "f": 1073743361,
                "tp": "evt",
                "et": "(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.015082, 121.721735\nTime: 2021-12-03 01:51:02\nhttp://maps.google.com/?q=17.015082,121.721735",
                "x": 121.721735001,
                "y": 17.0150816669,
                "rt": 0,
                "p": {}
            }
        ]
    }
]';

SELECT ISJSON(@json);

-- INSERT INTO <target table>
SELECT message.*
FROM OPENJSON(@json) as messages
   CROSS APPLY OPENJSON(messages.value, '$.messages') 
WITH
(
    t NVARCHAR(10) '$.t'
    , f NVARCHAR(10) '$.f'
    , tp NCHAR(3) '$.tp'
    , et NVARCHAR(MAX) '$.et' 
    , x NVARCHAR(20) '$.x'
    , y NVARCHAR(20) '$.y'
    , rt INT '$.rt'
    , p NVARCHAR(100) '$.p'
) 
AS message;

【讨论】:

  • 感谢 Yitzhak,我正在使用 SQL 2018。我尝试运行脚本,但出现错误提示“$.t”附近的语法不正确。
  • 我的答案是遵循 minimal reproducible example 模式。只需将它按原样复制到 SSMS,运行,它就会工作。我在答案中添加了一个小提琴。看看吧。
  • 谢谢伊扎克!我可以看到您的解决方案通过您提供的 Fiddle 完美运行,我喜欢它!我认为我的 SQL 版本中缺少某些内容,因此我遇到了与前面提到的相同的错误。对不起,我也打错了我的 SQL 版本,应该是 2017 年。但奇怪的是,我尝试在 Fiddle 中更改 SQL 版本,它仍然可以正常工作。你知道我在这里缺少什么吗?
  • 它有效!我刚刚使用以下命令将数据库的兼容性级别从 100 更改为 140。谢谢伊扎克和斯库利!你真的很棒! ALTER DATABASE {我的数据库} SET COMPATIBILITY_LEVEL = 140;
  • @SnakeBytes,很高兴听到建议的解决方案对您有用。请不要忘记将其标记为答案。
【解决方案2】:

您从网站收到的响应是 JSON 格式,您可以将其转换为 PHP 中的数组并遍历每条消息并回显您喜欢的任何变量,并将其保存到数据库中。

<?php

// The JSON below is fetched from the website's API endpoint.
$json = '{"count":8,"messages":[{"t":1638413506,"f":1073743361,"tp":"evt","et":"(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.026467, 121.721675\nTime: 2021-12-02 10:51:46\nhttp:\/\/maps.google.com\/?q=17.026467,121.721675","x":121.721674983,"y":17.0264666677,"rt":0,"p":{}},{"t":1638415192,"f":1073743361,"tp":"evt","et":"(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.023507, 121.721527\nTime: 2021-12-02 11:19:52\nhttp:\/\/maps.google.com\/?q=17.023507,121.721527","x":121.721526655,"y":17.0235066672,"rt":0,"p":{}},{"t":1638416233,"f":1073743361,"tp":"evt","et":"(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.023335, 121.721730\nTime: 2021-12-02 11:37:13\nhttp:\/\/maps.google.com\/?q=17.023335,121.721730","x":121.721729978,"y":17.0233349999,"rt":0,"p":{}},{"t":1638426180,"f":1073743361,"tp":"evt","et":"(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.023990, 121.721965\nTime: 2021-12-02 14:23:00\nhttp:\/\/maps.google.com\/?q=17.023990,121.721965","x":121.721965027,"y":17.0239899993,"rt":0,"p":{}},{"t":1638430480,"f":1073743361,"tp":"evt","et":"(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.014735, 121.721372\nTime: 2021-12-02 15:34:40\nhttp:\/\/maps.google.com\/?q=17.014735,121.721372","x":121.721371651,"y":17.0147350003,"rt":0,"p":{}},{"t":1638439534,"f":1073743361,"tp":"evt","et":"(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.014727, 121.721405\nTime: 2021-12-02 18:05:34\nhttp:\/\/maps.google.com\/?q=17.014727,121.721405","x":121.721405029,"y":17.0147266666,"rt":0,"p":{}},{"t":1638450807,"f":1073743361,"tp":"evt","et":"(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Dalig, Burgos, Isabela, Philippines\nCoordinates: 17.014760, 121.722102\nTime: 2021-12-02 21:13:27\nhttp:\/\/maps.google.com\/?q=17.014760,121.722102","x":121.722101657,"y":17.0147599995,"rt":0,"p":{}},{"t":1638467462,"f":1073743361,"tp":"evt","et":"(NotificationNo: GPS0006)\nAlert!\n\nMOTION DETECTION: Tractor #16\n\nSuspicious movement of vehicle is detected while engine is off! \n\nCurrent Location: Bacnor East, Burgos, Isabela, Philippines\nCoordinates: 17.015082, 121.721735\nTime: 2021-12-03 01:51:02\nhttp:\/\/maps.google.com\/?q=17.015082,121.721735","x":121.721735001,"y":17.0150816669,"rt":0,"p":{}}]}';

$allMessages = json_decode($json, true); // Convert to associative array.

foreach($allMessages["messages"] as $i => $message) // Iterate through every message.
{
    // $message here now references each message within the data.
    // For this example, output the 'tp' of the message on a new line.
    echo "[Message " . $i . "] " . $message["tp"] . "<br><br>";

    // Insert further code here to save the $message into your SQL database.
}

要使用 Microsoft SQL 服务器将信息保存到数据库,请参阅:Microsoft SQL Server for PHP - Getting Started

【讨论】:

  • 感谢 Skully,它成功了!只需弄清楚批量插入以将记录附加到我的表中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多