How to deal with backslashes in json strings php

后端 未结 5 778
慢半拍i
慢半拍i 2020-11-30 12:26

There appears to be an oddity with json_encode and/or json_decode when attempting decode a string that was produced by json_encode:

    $object = new stdClas         


        
5条回答
  •  醉酒成梦
    2020-11-30 13:19

    You will need to encode your double backslashes \\ into quadruple backslashes \\\\ because php interprets a backslash as an escape character in single and double quoted strings. This means that \\ is seen as \.

    This example illustrates what is happening

    This is the output

    $ php test.php
    {"namespace":"myCompany\package\subpackage"}
    Syntax error
    class stdClass#1 (1) {
      public $namespace =>
      string(28) "myCompany\\package\\subpackage"
    }
    No error
    

提交回复
热议问题