First off, it's worth pointing out that PHP is not "messing up" anything. It's escaping characters, which may look weird, but it is perfectly valid and when you json_decode it later it will be just the same as it was originally. See here: https://3v4l.org/Smj2F
If you don't like the escaping though, you can use the JSON_UNESCAPED_UNICODE flag:
https://www.php.net/function.json-encode
This flag will "encode multibyte Unicode characters literally" according to https://www.php.net/manual/en/json.constants.php.
So you can do:
json_encode($array, JSON_UNESCAPED_UNICODE);
And it will give you the following output:
["M-am întîlnit ieri cu","fosta mea profă de matematică"]
Working example: https://3v4l.org/daETG
Answer from jszobody on Stack OverflowFirst off, it's worth pointing out that PHP is not "messing up" anything. It's escaping characters, which may look weird, but it is perfectly valid and when you json_decode it later it will be just the same as it was originally. See here: https://3v4l.org/Smj2F
If you don't like the escaping though, you can use the JSON_UNESCAPED_UNICODE flag:
https://www.php.net/function.json-encode
This flag will "encode multibyte Unicode characters literally" according to https://www.php.net/manual/en/json.constants.php.
So you can do:
json_encode($array, JSON_UNESCAPED_UNICODE);
And it will give you the following output:
["M-am întîlnit ieri cu","fosta mea profă de matematică"]
Working example: https://3v4l.org/daETG
For those looking for exact equivalent result PHP json_encode() v.s. JavaScript JSON.stringify() as asked in question you must use at least these 2 flags
json_encode($array, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
because of for example JS produces "https://lh3.googleusercontent.com" but PHP without JSON_UNESCAPED_SLASHES produces "https:\/\/lh3.googleusercontent.com". Is important in my case because of Im creating sha hash of both and compare it. If frontend hash is the same as backend hash it means data are synced and no traffic/syncing is needed. Im not sure if some other flag is needed.
When you save some data using JSON.stringify() and then need to read that in php. The following code worked for me.
json_decode( html_entity_decode( stripslashes ($jsonString ) ) );
Thanks to @Thisguyhastwothumbs
When you use JSON stringify then use html_entity_decode first before json_decode.
$tempData = html_entity_decode($tempData);
$cleanData = json_decode($tempData);
If you are not using dataType : 'json', you might need to do stripslashes
$.post(window.data.baseUrl, {posts : JSON.stringify(posts)});
And in php:
$posts = json_decode(stripslashes($_POST['posts']));
This helped me:
data = json_decode($this->request->data['jsarr'], true);
in your php code for accessing the record
Hope it will help someone!
First make sure that $locationinfo is exactly a json string. I suspect it is a php associative array.
Try echo $locationinfo['id'];. If value appears u don't want to decode it. Use
$locationinfo directly withot json decode.
If it is a json, Try using like this,
$arr = json_decode($locationinfo, true);
Add a stripslashes.
$data = json_decode(stripslashes($data),true);
Demo : http://codepad.org/XX9QD3iX
Answered here : https://stackoverflow.com/a/37599821/19168006
Edit : example in demo has stdClass error, this is the working one : http://codepad.org/lfJJu5yA
Change the Python script so it prints JSON instead of Python format.
print(json.dumps(out))
However, sets aren't in JSON, so change outnews to a list.
outnews = [html.unescape(currentNews["timestamp"]), html.unescape(currentNews["title"]), html.unescape(currentNews["description"]), html.unescape(currentNews["link"])]
Then the PHP script can simply return that to the client.
if ($_GET['times'] == 0) {
$command = escapeshellcmd('python3 feed.py '. $_GET['subject']);
header('Content-type: application/json');
passthru($command);
}
If passthru() isn't working, you can try with your original shell_exec(). You don't need to call json_encode() because it's already encoded.
if ($_GET['times'] == 0) {
$command = escapeshellcmd('python3 feed.py '. $_GET['subject']);
$output = shell_exec($command);
header('Content-type: application/json');
echo $output;
}
Also If I want to get back all news than change out and return it back like this:
out = []
error = 0
status = 0
nrOfResults = 0
if requestpost.status_code == 200:
status = 200
for news in response_data["news"]:
try:
currentNews = json.loads(news)
if ((html.unescape(currentNews["title"]) != "Array" and html.unescape(currentNews["title"]).lower().find(searchterm.lower()) != -1) or (html.unescape(currentNews["description"]).lower().find(searchterm.lower()) != -1)):
outnews = [html.unescape(currentNews["timestamp"]), html.unescape(currentNews["title"]), html.unescape(currentNews["description"]), html.unescape(currentNews["link"])]
out.append(outnews)
nrOfResults = nrOfResults +1
except:
error += 1
else:
status = 404
out.append(status)
out.append(nrOfResults)
#outnews = [html.unescape(currentNews["timestamp"]), html.unescape(currentNews["title"]), html.unescape(currentNews["description"]), html.unescape(currentNews["link"])]
print(json.dumps(out))
than the last element of js array will be a the number of results and the before one will be the status of the source link.