首页 > 解决方案 > 浏览 JSON 并填充 PHP 变量

问题描述

我在项目上工作,那里有服务器发送到我的 php 应用程序 json 数据,然后我将其放入数据库中。我收到来自服务器的 POST 请求,我可以使用以下命令获取 json:receive_json.php:

$payload = file_get_contents('php://input');

并且有 JSON 的形式:

{
"applicationID": "123",
"applicationName": "temperature-sensor",
"deviceName": "garden-sensor",
"devEUI": "0202020202020202",
"rxInfo": [
    {
        "gatewayID": "0303030303030303",          // ID of the receiving gateway
        "name": "rooftop-gateway",                 // name of the receiving gateway
        "time": "2016-11-25T16:24:37.295915988Z",  // time when the package was received (GPS time of gateway, only set when available)
        "rssi": -57,                               // signal strength (dBm)
        "loRaSNR": 10,                             // signal to noise ratio
        "location": {
            "latitude": 52.3740364,  // latitude of the receiving gateway
            "longitude": 4.9144401,  // longitude of the receiving gateway
            "altitude": 10.5,        // altitude of the receiving gateway
        }
    }
],
"txInfo": {
    "frequency": 868100000,  // frequency used for transmission
    "dr": 5                  // data-rate used for transmission
},
"adr": false,                  // device ADR status
"fCnt": 10,                    // frame-counter
"fPort": 5,                    // FPort
"data": "...",                 // base64 encoded payload (decrypted)
"object": {                    // decoded object (when application coded has been configured)
    "temperatureSensor": {"1": 25},
    "humiditySensor": {"1": 32}
 }
}

我是 JSON 的新用户,所以我直接将文本 JSON 放入我的数据库,如下所示:

 $payload = file_get_contents('php://input');

 $req = $bdd->prepare( '                             
                           INSERT INTO nods(payload)                               
                           VALUES (:payload);
                      ' );

$req->execute(array('payload' => $payload));

结果: 在此处输入图像描述 我想浏览“$payload”并将信息放入 php 变量中,例如:

$应用程序ID = .... ; $rxInfo[] = .... ;

我已经尝试过类似“foreach”的东西,但我无法成功,请你帮帮我,谢谢:)。

最良好的问候,

迪杜·贾瓦德

标签: phparraysjsonserver

解决方案


You can use json_decode to get a PHP array representation of the JSON

$array = json_decode($payload, true);

var_dump($array);

http://php.net/manual/en/function.json-decode.php


推荐阅读