Prevent quoting of certain values with PHP json_encode()

后端 未结 7 1520
广开言路
广开言路 2021-01-06 01:21

When using PHP\'s json_encode to encode an array as a JSON string, is there any way at all to prevent the function from quoting specific values in the returned string? The r

7条回答
  •  被撕碎了的回忆
    2021-01-06 01:49

    What we do is (and that's what Zend_Json::encode() does too), is to use a special marker class that encapsulates Javascript expressions in a special class. The encoding then walks recursively through our array-to-be-encoded, replaces all marker instances with some string. After using the built-in json_encode() we simply do a string replace to replace each special string with the __toString() value of the respective marker instance.

    You can either use Zend_Json directly (if that's possible) or check how they do it and adapt the code to your needs.

提交回复
热议问题