wp_json_encode

函数
wp_json_encode ( $data, $options = 0, $depth = 512 )
参数
  • (mixed) $data Variable (usually an array or object) to encode as JSON.
    Required:
  • (int) $options Optional. Options to be passed to json_encode(). Default 0.
    Required:
  • (int) $depth Optional. Maximum depth to walk through $data. Must be greater than 0. Default 512.
    Required:
    Default: 512
返回值
  • (string|false) The JSON encoded string, or false if it cannot be encoded.
定义位置
相关方法
wp_json_file_decodewp_send_json_errorwp_is_json_media_typewp_is_json_request_wp_iso_convert
引入
4.1.0
弃用
-

wp_json_encode: 这个函数用来将一个PHP变量编码为JSON字符串。它接收一个PHP变量作为参数,并返回一个JSON编码的字符串。

将一个变量编码为JSON,并进行一些合理性检查。

function wp_json_encode( $data, $options = 0, $depth = 512 ) {
	$json = json_encode( $data, $options, $depth );

	// If json_encode() was successful, no need to do more sanity checking.
	if ( false !== $json ) {
		return $json;
	}

	try {
		$data = _wp_json_sanity_check( $data, $depth );
	} catch ( Exception $e ) {
		return false;
	}

	return json_encode( $data, $options, $depth );
}

常见问题

FAQs
查看更多 >