php - 浏览 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”的东西,但我无法成功,请你帮帮我,谢谢:)。
最良好的问候,
迪杜·贾瓦德
解决方案
You can use json_decode
to get a PHP array representation of the JSON
$array = json_decode($payload, true);
var_dump($array);
推荐阅读
- c++ - 仅在 2 的幂次上错过了 clang 中的优化
- nginx - nginx 位置指令附加到 url 路径
- c++ - c++ 11 原子变量上的哪些操作实际上是原子的?
- c# - 如果我在 asp.net 中为同步方法调用异步方法时不等待异步方法怎么办?
- php - 无法使用 symfony 进程运行 python 脚本
- python-3.x - 如何循环记录用户输入
- sed - sed 替换文本文件或 powershell 窗口中的 NUL 或 CRLF(如果不在行尾)
- vue.js - 如何使用 Vue 显示数据库中特定类型的内容
- javascript - Bootstrap4:修复和淡入页面底部的页脚
- python - Why do I get this failure and why is mysql set to false?