PHP JSON字符串,转义JS输出的双引号

我正在从一个 PHP数组创建一个JSON字符串.我已经使用json_encode()编码了它.
$json_array = array(
    'title' => 'Example string\'s with "special" characters'
);

$json_decode = json_encode( $json_array );

在JS文件中,我可以通过以下方式访问信息:
注意:$json_decode使用wp_localize_script()本地化到全局变量PHP_params

var discographyJsonString = PHP_params.playlist_setup.replace( /"/g,'"' ),discographyJson = jQuery.parseJSON( discographyJsonString );

console.log(discographyJson);

输出结果为:

{ "title":"Example string's with "special" characters" }

如果将该结果输入http://jsonlint.com/,您将收到错误.如果您在“特殊”周围取出双引号,则表示有效.

PHP中创建JSON字符串的最佳方式是什么,并将其转义为正确使用在JS文件中?

注意:这是参考我的另一篇文章,我从使用PHP的API中获取信息,用于JS:Loop through JSON string and create dynamic JS output

http://www.php.net/manual/en/function.json-encode.php#100565

That said,quotes ” will produce invalid JSON,but this is only an issue if you’re using json_encode() and just expect PHP to magically escape your quotes. You need to do the escaping yourself.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...