Hello am trying to use json_decode to decode a json object with multiple same arrays
I have tried Json _decode to access member elements but am receiving blank details
$json_string =file_get_contents('php://input');
$date='19/08/2019';
$time_rec='9:07 pm';
$device=$json_string['ident'];
$imei=$json_string['ident'];
$speed=$json_string['position.speed'];
$longitude=$json_string['position.longitude'];
$latitudedirection=$json_string['position.direction'];
$latitude=$json_string['position.latitude'];
$ain1=$json_string['ain.1'];
$ignition=$json_string['engine.ignition.status'];
$voltage=$json_string['external.powersource.voltage'];
$signaldis='0';
$powerdis='0';
$brand='PGL';
$car_plate='KCL 364V';
/*
Json element i receive looks like
[
{"ain.1":8.279,
"channel.id":12192,
"custom.param.329":68,
"device.id":388228,
"device.name":"PGL SPEED LIMITER",
"device.type.id":102,
"engine.ignition.status":true,
"event.enum":0,
"event.priority.enum":0,
"external.powersource.voltage":19.69,
"gnss.status":true,
"ident":"358480088504651",
"peer":"197.182.172.51:63370",
"position.altitude":0,
"position.latitude":-1.277276,
"position.longitude":36.829203,
"position.speed":0,
"position.valid":false,
"protocol.id":14,
"server.timestamp":1566241472.410389,
"timestamp":1566241439
},
{"ain.1":8.315,"channel.id":12192,"custom.param.329":0,"device.id":388228,"device.name":"PGL SPEED LIMITER","device.type.id":102,"engine.ignition.status":true,"event.enum":0,"event.priority.enum":0,"external.powersource.voltage":19.69,"gnss.status":true,"ident":"358480088504651","peer":"197.182.172.51:63370","position.altitude":0,"position.latitude":-1.277175,"position.longitude":36.829066,"position.speed":0,"position.valid":false,"protocol.id":14,"server.timestamp":1566241472.410389,"timestamp":1566241444
},
{"ain.1":8.169,"channel.id":12192,"custom.param.329":68,"device.id":388228,"device.name":"PGL SPEED LIMITER","device.type.id":102,"engine.ignition.status":true,"event.enum":0,"event.priority.enum":0,"external.powersource.voltage":19.648,"gnss.status":true,"ident":"358480088504651","peer":"197.182.172.51:63370","position.altitude":0,"position.latitude":-1.277175,"position.longitude":36.829066,"position.speed":0,"position.valid":false,"protocol.id":14,"server.timestamp":1566241472.410389,"timestamp":1566241449
},
{"ain.1":8.033,"channel.id":12192,"custom.param.329":68,"device.id":388228,"device.name":"PGL SPEED LIMITER","device.type.id":102,"engine.ignition.status":true,"event.enum":0,"event.priority.enum":0,"external.powersource.voltage":19.67,"gnss.status":true,"ident":"358480088504651","peer":"197.182.172.51:63370","position.altitude":0,"position.latitude":-1.277175,"position.longitude":36.829066,"position.speed":0,"position.valid":false,"protocol.id":14,"server.timestamp":1566241472.410389,"timestamp":1566241454
},
{"ain.1":7.767,"channel.id":12192,"custom.param.329":68,"device.id":388228,"device.name":"PGL SPEED LIMITER","device.type.id":102,"engine.ignition.status":true,"event.enum":0,"event.priority.enum":0,"external.powersource.voltage":19.674,"gnss.status":true,"ident":"358480088504651","peer":"197.182.172.51:63370","position.altitude":0,"position.latitude":-1.277175,"position.longitude":36.829066,"position.speed":0,"position.valid":false,"protocol.id":14,"server.timestamp":1566241472.410389,"timestamp":1566241459
}
]
i need to parse data only for the first array element only
First you have to convert the JSON String into a PHP data type using json_decode().
<?php
$in = '[
{"ain.1":8.279,"channel.id":12192,"custom.param.329":68,
"device.id":388228,"device.name":"PGL SPEED LIMITER",
"device.type.id":102,"engine.ignition.status":true,
"event.enum":0,"event.priority.enum":0,"external.powersource.voltage":19.69,
"gnss.status":true,"ident":"358480088504651",
"peer":"197.182.172.51:63370","position.altitude":0,
"position.latitude":-1.277276,"position.longitude":36.829203,
"position.speed":0,"position.valid":false,
"protocol.id":14,"server.timestamp":1566241472.410389,
"timestamp":1566241439
},
{"ain.1":8.315,"channel.id":12192,"custom.param.329":0,"device.id":388228,"device.name":"PGL SPEED LIMITER","device.type.id":102,"engine.ignition.status":true,"event.enum":0,"event.priority.enum":0,"external.powersource.voltage":19.69,"gnss.status":true,"ident":"358480088504651","peer":"197.182.172.51:63370","position.altitude":0,"position.latitude":-1.277175,"position.longitude":36.829066,"position.speed":0,"position.valid":false,"protocol.id":14,"server.timestamp":1566241472.410389,"timestamp":1566241444
},
{"ain.1":8.169,"channel.id":12192,"custom.param.329":68,"device.id":388228,"device.name":"PGL SPEED LIMITER","device.type.id":102,"engine.ignition.status":true,"event.enum":0,"event.priority.enum":0,"external.powersource.voltage":19.648,"gnss.status":true,"ident":"358480088504651","peer":"197.182.172.51:63370","position.altitude":0,"position.latitude":-1.277175,"position.longitude":36.829066,"position.speed":0,"position.valid":false,"protocol.id":14,"server.timestamp":1566241472.410389,"timestamp":1566241449
},
{"ain.1":8.033,"channel.id":12192,"custom.param.329":68,"device.id":388228,"device.name":"PGL SPEED LIMITER","device.type.id":102,"engine.ignition.status":true,"event.enum":0,"event.priority.enum":0,"external.powersource.voltage":19.67,"gnss.status":true,"ident":"358480088504651","peer":"197.182.172.51:63370","position.altitude":0,"position.latitude":-1.277175,"position.longitude":36.829066,"position.speed":0,"position.valid":false,"protocol.id":14,"server.timestamp":1566241472.410389,"timestamp":1566241454
},
{"ain.1":7.767,"channel.id":12192,"custom.param.329":68,"device.id":388228,"device.name":"PGL SPEED LIMITER","device.type.id":102,"engine.ignition.status":true,"event.enum":0,"event.priority.enum":0,"external.powersource.voltage":19.674,"gnss.status":true,"ident":"358480088504651","peer":"197.182.172.51:63370","position.altitude":0,"position.latitude":-1.277175,"position.longitude":36.829066,"position.speed":0,"position.valid":false,"protocol.id":14,"server.timestamp":1566241472.410389,"timestamp":1566241459
}
]';
$json = json_decode($in);
$device=$json[0]->ident;
echo $device.PHP_EOL;
$speed=$json[0]->{'position.speed'};
echo $speed.PHP_EOL;
$longitude=$json[0]->{'position.longitude'};
echo $longitude . PHP_EOL;
Related
I've the following PHP code with a JSON variable...
$route_geometry_json = "{
\"route_geometry\": [
[44.911537, 7.671326],
[44.911481, 7.671462],
[44.911455, 7.671531],
[44.911434, 7.671602],
[44.911358, 7.671859],
[44.911273, 7.672175],
[44.911198, 7.672458],
[44.91113, 7.672617],
[44.911069, 7.67275],
[44.911003, 7.672821],
[44.910945, 7.672881],
[44.910869, 7.672954],
[44.910868, 7.673046],
[44.91091, 7.673109],
[44.91095, 7.67319],
[44.910964, 7.673266],
[44.910958, 7.673407],
[44.910955, 7.6735],
[44.910947, 7.673632],
[44.910922, 7.673871],
[44.910828, 7.674786],
[44.910711, 7.675816],
[44.910606, 7.676364],
[44.910467, 7.676322],
[44.910368, 7.676308],
[44.910051, 7.676253],
[44.9097, 7.676162],
[44.90944, 7.676041],
[44.909297, 7.675958],
[44.909174, 7.67583],
[44.909107, 7.675722],
[44.908993, 7.675583],
[44.908758, 7.675448],
[44.90796, 7.675037]
]
}";
print "Route geometry -->" + json_encode($route_geometry_json);
The print return "0": any suggestion / example?
I'd like also to extract / print the coords couples like
44.908993, 7.675583
44.908758, 7.675448
Any suggestion will be appreciated ...
Thanks
Cesare
Use this code:
$route_geometry = json_decode($route_geometry_json);
foreach ($route_geometry->route_geometry as $value) {
echo $value[0].', '.$value[1].'<br />';
}
You need to print_r(json_decode($route_geometry_json)) it and not json_encode
json_encode is for creating a JSON string. But since you already have a JSON string already, you need to Decode it to make it an Array/Object.
UPDATE
Your requirement
echo "Route geometry -->";
print_r(json_decode($route_geometry_json));
You cannot concat a String and an Object, so you were getting that Parse error.
I'm trying to decode JSON format
What I am sending is:
{
"id": 123,
"name": "John",
“surname”: “Smith”,
“department”: 3
}
I am sending POST with data via Postman, in the picture.
So, this is the data I want to decode:
"data_serever_received": "{ \"id\": 123, \"name\": \"john\", “surname”: “Smith”, “department”: 3 }"
I tried
$this->input->data["id"]
but it is not working.
How can I get the id, name, surname and etc. values?
Your JSON is invalid “ and ” are not ".
(Zoom in with your browser if you can't see the difference).
You need to start with valid JSON before you can parse it.
You can use json_decode to make it an array.
$json_array = json_decode($json_object);//$json_object will need to contain your json string.
then you can access like this:
echo $json_array["data"][“surname”];
PHP Doc on json_decode: http://php.net/manual/en/function.json-decode.php
I want to decode json encoded data, My code output is
[ItemVariant] => [{"VariantID1":"36","VariantID2":"1","RevisionNumber":1,"LineNo":1},{"VariantID1":"47","VariantID2":"44","RevisionNumber":1,"LineNo":1}],
and i am using following code to decode it in controller file in save function
$variantdata = json_decode($this->request->data['ItemVariant']); ,
but not getting expected output,
please suggest me proper solution
There is a unrequired "," at the end. Please remove it.
This should help:
<?php
$str = '[
{
"VariantID1": "36",
"VariantID2": "1",
"RevisionNumber": 1,
"LineNo": 1
},
{
"VariantID1": "47",
"VariantID2": "44",
"RevisionNumber": 1,
"LineNo": 1
}
]';
$json = json_decode($str, true);
print_r($json);
?>
Run the code here:
http://codepad.org/GZcCdkd2
The result of the call to the api server is a json file, which begins with this string:
{
"result": "success"
, "data": {"total":16080,"pageCount":161,"result":[{"packWidth":250,"itemNo"
How do I remove the part that I do not care?
that is, this
{
"result": "success"
, "data": {"total":16080,"pageCount":161,"result":
The complete result is:
{
"result": "success"
, "data": {"total":16080,"pageCount":161,"result": [{"packWidth":250,"itemNo":"1203945","groupItemNo":"1203945","status":1,"categoryId":105096,"packType":"Color Box","barcode":"6922833439687","modelLabel":"Color","packQty":24,"packInclude":"USB Cable, User Manual, USB Charger, Earphone, 1pcs Li-Battery, LCD Protector, Leather Case, Plastic Case","clearance":false,"id":103928,"packWeight":"12.500","price":"181.2800","packLength":400,"description":"description test","unitWeight":"0.726","packHeight":300}]}}
I use the PHP language
I have to remove the initial part:
{
"result": "success"
, "data": {"total":16080,"pageCount":161,"result":
and the final:
}}
If you want to use part of a JSON to populate a CSV file, then parse the json using json_decode method and access the necessary information.
Try something like this:
var jsonObject = json_decode(myJson);
var interestingPart = jsonObject.data.result;
You can now access the data in an Object manner. Or if you want to get a json back from it, then use:
var interestingJson = json_encode(interestingPart);
Not tested, but it should work
preg_replace("/^.*?:(?=\w*\[) | (\w*}\w*}\w*$)/", "", $str);
Edit: i wrote this before I knew of json_decode, you should really use the json functions like suggested in fazovskys answer.
I have a ajax call calling a php file who runs a long php function which returns a JSON encoded array/object. Now I need to send HTML also into the ajax response. I thought about sending the HTML inside the array.
Is that a good practise?
Right now I cannot get it working, I get a NULL as value for that property. Don't know why.
$statHTML = '<table>';
foreach ($toHTML as $key=>$value) {
$statHTML.= '
<tr class="'.$key.'">
<td class="side">'.$value[0].'</td>
<td>'.$value[2].' '.$value[1].'</td>
</tr>';
}
$statHTML.= '</table>';
// echo $statHTML; // - this works
//function return
$answer = array('mostSearched'=>$mostSearched,
'timeOfDay' => $timeOfDay,
'mostSearchedDays'=>$mostSearchedDays,
'statHTML' => $statHTML
);
return json_encode($answer);
The ajax response from the console before the JSON.parse():
{
"mostSearched": {
"title": "Most serached houses",
"colNames": [21],
"rowNames": [2013],
"rows": [1]
},
"timeOfDay": {
"title": "Time of search",
"colNames": ["07:30"],
"rowNames": ["07:30"],
"rows": [
[1]
]
},
"mostSearchedDays": {
"title": "Most searched days",
"colNames": ["2013-12-21", "2013-12-22", "2013-12-23", "2013-12-24", "2013-12-25", "2013-12-26", "2013-12-27"],
"rowNames": ["2013-12-21", "2013-12-22", "2013-12-23", "2013-12-24", "2013-12-25", "2013-12-26", "2013-12-27"],
"rows": [
[1, 1, 1, 1, 1, 1, 1]
]
},
"statHTML": null
}
From php.net:
Parameters
value
The value being encoded. Can be any type except a resource.
All string data must be UTF-8 encoded.
So use:
$answer = array('mostSearched'=>$mostSearched,
'timeOfDay' => $timeOfDay,
'mostSearchedDays'=>$mostSearchedDays,
'statHTML' => utf8_encode($statHTML)
);
return json_encode($answer);
Most likely the build-in JSON parser used by PHP cannot properly parse the HTML, the easiest way to solve the issue is to base64 encode the html on the server, and then decode it on the client, using either the newer atob and btoa base64 methods, or one of the many polyfills out there.
use base64_enccode in this at the time of conversion
$answer = array('statHTML'=>base64_encode('<h1>html in here</h1>'));
echo json_encode($answer);exit;
And at the time of getting response from ajax
atob(response.statHTML);
Hope you understand how it works