Home > Software engineering >  PHP JSON including "."
PHP JSON including "."

Time:06-25

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.

CodePudding user response:

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
  •  Tags:  
  • php
  • Related