search

Home  >  Q&A  >  body text

No more than 3 backslashes are allowed in json_encode output.

<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>
P粉561749334P粉561749334482 days ago616

reply all(1)I'll reply

  • P粉551084295

    P粉5510842952023-08-09 17:11:46

    I didn't validate the function in PHP, but your JSON input is wrong.

    JSON consists of key-value pairs, for example:


    {"hercher Nom": "Le mei"}

    Or multiple key-value pairs separated by commas:

    {"hercher Nom": "Le mei", "hercher Nom 2": "Le mei 2"}

    You can adjust the arrangement of the content slightly for readability, but this is not technically necessary: ​​

    {
        "hercher Nom": "Le mei",
        "hercher Nom 2": "Le mei 2"
    }

    reply
    0
  • Cancelreply