Prevent quoting of certain values with PHP json_encode()

后端 未结 7 1456
广开言路
广开言路 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:45

    The json_encode function does not provide any functionality for controlling the quotes. The quotes are also necessary for JavaScript to properly form the object on the JavaScript side.

    In order to use the returned value to construct an object on the JavaScript side, use the json_encoded string to set flags in your association.

    For example:

    json_encode( array( "click_handler"=> "FOO" ) );
    

    JavaScript side in the AJAX:

    if( json.click_handler == "FOO" ) {
      json.click_handler = Your_Handler;
    }
    

    After these steps you can pass your object off somewhere.

提交回复
热议问题