No more than 3 backslashes are allowed in json_encode output.
P粉561749334
2023-08-08 21:02:30
<p>I have a piece of code that handles a very large amount of data and converts it using json_encode (around 255,000 characters). But I noticed that each time json_encode returns no more than 3 backslashes in the result. Is this intentional, a bug, or something else? </p>
<pre class="brush:php;toolbar:false;"><?php
header("content-type: application/json");
function Json_Zip($dir, $data) {
if ($dh = opendir($dir)) {
while (($entry2 = readdir($dh)) !== false) {
if ($entry2 != "." && $entry2 != "..") {
$entry2 = $dir . $entry2;
if (is_dir($entry2)) {
$data[$entry2] = 0;
$data = Json_Zip($entry2."/", $data);
} else {
$fileContent = file_get_contents($entry2);
$data[$entry2] = $fileContent;
}
}
}
}
return $data;
}
file_put_contents("content.json",json_encode(Json_Zip("./", []), JSON_UNESCAPED_UNICODE));</pre>
<p>When I use a script to convert a file to a directory, this happens: <br /><br /> (starting with: "hercher "Nom : Le mei") (after json_encode After: "hercher "Nom : Le mei"), I tried updating the PHP version, but nothing changed. </p><p><br /></p>
I didn't validate the function in PHP, but your JSON input is wrong.
JSON consists of key-value pairs, for example:
Or multiple key-value pairs separated by commas:
You can adjust the arrangement of the content slightly for readability, but this is not technically necessary: