Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP JSON including "."

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading