浏览JSON并填充PHP变量

时间:2018-09-21 14:08:26

标签: php arrays json server

我在项目上工作,那里有向我的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));

结果: enter image description here 我想浏览“ $ payload”,然后将信息放入php变量中,例如:

$ applicationID = ....; $ rxInfo [] = ....;

我已经尝试过类似“ foreach”之类的方法,但是我无法成功,请帮忙,谢谢:)。

最诚挚的问候,

DIDOUH JAWAD

1 个答案:

答案 0 :(得分:-1)

您可以使用json_decode来获取JSON的PHP数组表示形式

$array = json_decode($payload, true);

var_dump($array);

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