PHP json_encode 数据带双引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34769665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP json_encode data with double quotes
提问by Bram Wijns
I'm using this simple code to transform database query results into JSON format:
我正在使用这个简单的代码将数据库查询结果转换为 JSON 格式:
$result = $mysqli->query("
SELECT
date as a
, sum(sales) as b
, product as c
FROM
default_dataset
GROUP BY
date
, product
ORDER BY
date
");
$data = $result->fetch_all(MYSQLI_ASSOC);
echo stripslashes(json_encode($data));
The problem is that if there are double quotes in the data (e.g. in the product column) returned by this query. The json_encode function does not encode the data in a good JSON format.
问题是如果此查询返回的数据(例如在产品列中)有双引号。json_encode 函数不会以良好的 JSON 格式对数据进行编码。
Could someone help me how to escape the double quotes that are returned by the query? Thank you.
有人可以帮助我如何转义查询返回的双引号吗?谢谢你。
采纳答案by Steve
json_encode
already takes care of this, you are breaking the result by calling stripslashes
:
json_encode
已经解决了这个问题,您正在通过调用来破坏结果stripslashes
:
echo json_encode($data); //properly formed json
回答by Naqash Malik
You will need htmlspecialchars
instead of stripslashes
with proper encoding (UTF-8, if your page uses UTF-8 charset) and ENT_QUOTES
which will escape double quotes preventing data to break. See the code below:
您将需要htmlspecialchars
而不是stripslashes
使用正确的编码(UTF-8,如果您的页面使用 UTF-8 字符集)并且ENT_QUOTES
将转义双引号以防止数据中断。请参阅下面的代码:
echo htmlspecialchars(json_encode($data), ENT_QUOTES, 'UTF-8');
回答by devpro
Example with a simple array with double quotes
string value.
带有double quotes
字符串值的简单数组的示例。
$yourArr = array(
'title' => 'This is an example with "double quote" check it'
);
// add htmlspecialchars as UTF-8 after encoded
$encodeData = htmlspecialchars(json_encode($yourArr), ENT_QUOTES, 'UTF-8');
echo $encodeData;
Result:
结果:
{"title":"This is an example with \"double quote\" check it"}
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.
也就是说,引号 " 会产生无效的 JSON,但这只是在您使用 json_encode() 并且只是希望 PHP 神奇地转义您的引号时才会出现的问题。您需要自己进行转义。
回答by Saddam H
Yes, PHP json_encode wouldn't "escape double" quotes, we need to escape those manually in this super simple way-
是的,PHP json_encode 不会“转义双”引号,我们需要以这种超级简单的方式手动转义这些引号-
array_walk($assoc_array, function(&$v, $k) {
if(is_string($v) && strpos($v, '"') !== false) {
$v = str_replace('"', '\"', $v);
}
});
// After escaping those '"', you can simply use json_enocde then.
$json_data = json_encode($assoc_array);
回答by Imtiaz Pabel
to encode the quotes using htmlspecialchars:
使用htmlspecialchars对引号进行编码:
$json_array = array(
'title' => 'Example string\'s with "special" characters'
);
$json_decode = htmlspecialchars(json_encode($json_array), ENT_QUOTES, 'UTF-8');
回答by Raghunath Raut
You can done that using base64_encode and base64_decode
你可以使用 base64_encode 和 base64_decode
To store in database first convert it to base64_encode and stored into database and if you want that data then you can decrypt that data using base64_decode
要存储在数据库中,首先将其转换为 base64_encode 并存储到数据库中,如果您想要该数据,则可以使用 base64_decode 解密该数据
$var['cont'] = base64_encode($_POST['data']);
$dd = json_encode($var['cont']);
echo base64_decode ( json_decode($dd) );