当键包含PHP期间时,如何选择JSON值

发布于 2025-02-10 09:19:27 字数 542 浏览 1 评论 0 原文

我正在从我的Cube E-Bike从Raspberry Pi到我的网站上回复此JSON,看起来像这样:

{
  "result": [
    {
      "telemetry": {
        "actual.remaining.mileage": 26.81,
        "position": {
          "altitude": 0,
          "direction": 0,
          "latitude": 34.799687,
          "longitude": 7.478863,
          "satellites": 0,
          "speed": 0
        },
        "position.altitude": 0,
        "vehicle.mileage": 624.071
      },
      "connected": true
    }
  ]
}

我该如何访问我尝试查看已经存在的线程的PHP的人,但是由于我的JSON获得了一些名称,包括”。 “我无法工作。

I'm getting this json respond from my Cube e-bike from a raspberry pi to my website looking like this:

{
  "result": [
    {
      "telemetry": {
        "actual.remaining.mileage": 26.81,
        "position": {
          "altitude": 0,
          "direction": 0,
          "latitude": 34.799687,
          "longitude": 7.478863,
          "satellites": 0,
          "speed": 0
        },
        "position.altitude": 0,
        "vehicle.mileage": 624.071
      },
      "connected": true
    }
  ]
}

How can i access those with PHP i've tried looking on already existing threads but since my json got some names including "." i can't get it wo work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2025-02-17 09:19:27

您可以使用本机PHP函数 json_decode 并像下面的那样访问它:

$data = '{"result":[{"telemetry":{"actual.remaining.mileage":26.81,"position":{"altitude":0,"direction":0,"latitude":34.799687,"longitude":7.478863,"satellites":0,"speed":0},"position.altitude":0,"vehicle.mileage":624.071},"connected":true}]}';
$data = json_decode($data, true);

return $data['result'][0]['telemetry']['actual.remaining.mileage']; //returns 26.81

You can convert the JSON to a PHP array using the native PHP function json_decode and access it like below:

$data = '{"result":[{"telemetry":{"actual.remaining.mileage":26.81,"position":{"altitude":0,"direction":0,"latitude":34.799687,"longitude":7.478863,"satellites":0,"speed":0},"position.altitude":0,"vehicle.mileage":624.071},"connected":true}]}';
$data = json_decode($data, true);

return $data['result'][0]['telemetry']['actual.remaining.mileage']; //returns 26.81
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文