Black User Black User - 6 months ago 19
JSON Question

JSON Not Working on PHP Script

I got 2 JSON files extracted from the same db,

a:)

{
"hint_data": {
"locations": ["AXQDAP____8AAAAABwAAABEAAAAYAAAAIwIAAERwAgAAAAAADgyCAef7TAMCAAEB", "bOsDAP____8AAAAAAwAAAAcAAADFAQAAFAAAAEJwAgAAAAAANQeCAdzdTAMFAAEB"],
"checksum": 326195011
},
"route_name": ["", ""],
"via_indices": [0, 15],
"via_points": [
[25.299982, 55.376873],
[25.29874, 55.369179]
],
"found_alternative": false,
"route_summary": {
"end_point": "",
"start_point": "",
"total_time": 101,
"total_distance": 871
},
"route_geometry": "{_ego@m}|rhBpBaBvHuC`EuArEUtEtAlDvEnD`MlDvMli@hsEfFzn@QlTgNhwCs@fKwBjF",
"status_message": "Found route between points",
"status": 0
}


b:)

{
"alternative_names": [
["", ""]
],
"route_name": ["", ""],
"status_message": "Found route between points",
"route_geometry": "qo{o@wpslhBmFZwEpBgDzDeBlFWdGv@GCzEyBfIyEtEoNtEuMpAuHkAsCgC[mB{AgCuA{@uCa@}Cl@yBtB}@lDVhEcAtDmCdBcDx@iDDeDk@uBeBeB_CcGcXyFi^{Dg\\wBkP_Fs^wBiS_Ce[_D}UwAcKiBqIgI_TeLaUcMmP_IsHqf@_WyJeE}FMkDjIiTth@y|@loBq^vy@mu@fw@cUxOco@r^gb@zRoOjPsBfMq@rMjBdPnEpKlEnHJ~Dvn@la@nd@pT|f@lLv_@pRzLbHnb@fUxc@lMnaC~zAncBxnAkB|bB|CbmCl]rYxCzkCjwBzoBjxGl_G|fNhcMf~E~mE~pFt}EjbBlzAhvInyHfqDb~CblAw@poAzt@z}@bb@diD~dBxtEjgCfjDljBpuAfw@bpGngDhqF~yC~g@la@v~@ni@rbAjg@jPJlCFNdDGdDu@j@eAhBa@lCLdCaCjI_BtCy@vA_DvF}qAf}Bof@l{@{CnFoKdR}mAxBgM|TkJxPmLlSsf@|}@gf@x@kOzXmEfGmSx^kf@tw@mDtL}k@r~@ykCjjEau@niAee@bu@uUl\\}DpFzCrCr|@dt@|NbLroBx_BdZlV|DbDpBr~Anq@xm@r|@ls@|y@dq@}OXuMQcDhEoBgBeCCu[xXmBH",
"route_summary": {
"end_point": "",
"start_point": "",
"total_time": 824,
"total_distance": 15391
},
"found_alternative": true,
"alternative_summaries": [{
"end_point": "",
"start_point": "",
"total_time": 886,
"total_distance": 14967
}],
"via_points": [
[25.196808, 55.273754],
[25.139168, 55.187702]
],
"status": 0,
"via_indices": [0, 144],
"hint_data": {
"locations": ["TdMLAP____8AAAAADwAAAA8AAAA9AAAAbAAAAOtwBgAAAAAACXmAARxpSwMEAAEB", "4Q4AAGKyBgAAAAAACQAAAAAAAAAhAQAAAAAAAGUYAAAAAAAA4Zd_AfgYSgMGAAEB"],
"checksum": 326195011
},
"alternative_geometries": ["qo{o@wpslhBmFZwEpBgDzDeBlFWdGv@GCzEyBfIyEtEoNtEuMpAuHkAsCgC[mB{AgCuA{@uCa@}Cl@yBtB}@lDVhEcAtDmCdBcDx@iDDeDk@uBeBeB_CcGcXyFi^{Dg\\wBkP_Fs^wBiS_Ce[_D}UwAcKiBqIgI_TeLaUcMmP_IsHqf@_WyJeE}FMkDjIiTth@y|@loBq^vy@ec@vfAyeAfcCok@vtA}Wtx@uS|d@skAvfCgExJgJpTqH|O}@n~@wSve@qEhKkYdp@gB~DuUrk@_JdRsAhDmJlVqd@rcAuM|Zeg@zkAiKzUgIbS}B~EtDvBhr@a@dQ~J|rBpmAj_CfyAlAzD^lDgHnOyQlg@w[l|@}Q~d@eBB}Af@mAdAw@bBWpBJrBn@hBaAjGif@ptAk^rmAoVxy@cHjPmCHE|Bp[xRpc@ZfLvIzLlIreG~cEjrArz@rmAl|@lbDrzBjWlQ|xA~bAho@pc@d_@jWng@j^hNzJvGnEnPjLlAx@lgAhv@bGvCjkBvoAbjDr}BvfAx{@t\\rSfoDviC~zAjgAfOxIvAbAjwCtsBv|@|m@bD|BvLId_Dt|BtLlIxsAx_AfE~Crn@hc@rKvIh_BniApe@z\xIpGtoBpxAtEdDrQrMzjChlB~@lo@nwFpE|jBl|AzFzE|DfDvDrCjKfJr@n\\pLzKnBzzAdO~LhaFxaEzCdCnLpKbp@ti@nIjHdRzMjo@tg@~MvKpwCtdClw@bm@z[bWfr@vk@zCrCr|@dt@|NbLroBx_BdZlV|DbDpBr~Anq@xm@r|@ls@|y@dq@}OXuM`QcDhEoBgBeCCu[xXmBH"],
"alternative_indices": [0, 183]
}


And I am running this script on every JSON file.
Here is the script.

<?php
$json = '{"hint_data":{"locations":["AXQDAP____8AAAAABwAAABEAAAAYAAAAIwIAAERwAgAAAAAADgyCAef7TAMCAAEB","bOsDAP____8AAAAAAwAAAAcAAADFAQAAFAAAAEJwAgAAAAAANQeCAdzdTAMFAAEB"],"checksum":326195011},"route_name":["",""],"via_indices":[0,15],"via_points":[[25.299982,55.376873],[25.29874,55.369179]],"found_alternative":false,"route_summary":{"end_point":"","start_point":"","total_time":101,"total_distance":871},"route_geometry":"{_ego@m}|rhBpBaBvHuC`EuArEUtEtAlDvEnD`MlDvMli@hsEfFzn@QlTgNhwCs@fKwBjF","status_message":"Found route between points","status":0}';
$data = json_decode($json);
$totalTime = $data->route_summary->total_time;
var_dump($totalTime); // DUMPS 101


I am getting the first JSON file output correctly but I am getting an error in the 2nd one.

Please tell me what's wrong with the JSON file.

Answer

1) put data to text file

2) try again with:

<?php

$data = file_get_contents('./relative/path/to/file.json');
$data = json_decode($data);
$totalTime  = $data->route_summary->total_time;
var_dump($totalTime);
  • it would be better to write data to files and put somewhere to download them to check the file.