I'm learning more about parsing JSON in PHP, and I encountered a problem editing a certain file.
I wrote a code that makes it possible to edit only the values I want from a JSON file, and for some reason, when I try to edit the value from "IdList", my code replaces that value without the square brackets, invalidating the JSON structure.
How could I get my code to correctly override this value while keeping it inside the square bracket?
My JSON File
{
"isVisible": false,
"Name": "Lorem ipslum",
"Int": 0,
"IdList": [10, 30, 70],
"UseKeys": true,
}
My PHP code
<?php
$commandline = ($_GET[("string")]);
$json_object = file_get_contents('file.json');
$data = json_decode($json_object, true);
$data['IdList'] = $commandline;
$json_object = json_encode($data, JSON_UNESCAPED_UNICODE);
file_put_contents('file.json', $json_object);
echo('success');
?>
When i try to change IdList to '70, 80, 90' for example, the Json file is saved like this:
"IdList":"70, 80, 90"
It should be saving like this:
"IdList": [70, 80, 90]
When I try to modify the text from the other values, it works normally.
you are setting a string for "IdList"
this would work for you:
$data['IdList'] = array_map('intval', explode(',',$commandline));
You can replace this line:
$data['IdList'] = $commandline;
With:
$data['IdList'] = json_decode('[' . $commandline . ']', true);
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;
I want to read the json data on the page http://mattrb.com/txt.txt
For example, let's say I want to get the name "Bulbasaur." I have this code:
<?php
$file = file_get_contents("http://mattrb.com/txt.txt");
$json = json_decode($file);
echo $json->1->name;
?>
This code causes the php to simply not load. Is this because you can't use a number? Next I tried this:
<?php
$file = file_get_contents("http://mattrb.com/txt.txt");
$json = json_decode($file);
$num = 1;
echo $json->$num->name;
?>
This allows the php to load, but still returns nothing. What am I doing wrong?
Your json is invalid. Please check at http://jsonlint.com/.
Also you can access "1" in php like this: echo $json->{1}->name;
Your json file isn't valide You have a comma problem item number 135 try to delete it so you can parse the file .
"135": {, //the problem of your json data is here
"levels": [5, 15],
"probability": 4 "name": "Jolteon",
"attack": 65,
"defense": 60,
"type": "electric",
"moves": [
"tackle",
"thundershock",
"thunder"
],
"curve": 1.6
},
Your json should not contain newline and other invalid characters.
In other words - your json is invalid. json_decode does not work on the file.
$file = file_get_contents("http://mattrb.com/txt.txt");
var_dump(json_decode($file));
// NULL
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 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