[英]How to import escaped data from MySQL to JSON?
在我的MySQL數據庫中,有以下一行:
\\"test\\"
| \\'test\\'
| \\'test\\'
\\"test\\"
如果將其導入JSON,我將得到:
[
"\"test\"",
"\'test\'",
"\'test\' \"test\""
]
這將在JSONLint中生成erron:
Parse error on line 2:
... "\"test\"", "\'test\'", "\'t
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
為什么以下代碼不會在JSONLint中生成錯誤?
[
"\"test\"",
"\\'test\\'",
"\\'test\\' \"test\""
]
以及如何從MySQL導入數據(通過PHP)以獲得上述結果?
在MySQL的結果集中使用json_encode
(PHP中的內置函數)。
小例子
$a = Array(
'\"test\"',
'\'test\'',
'\'test\' \"test\"'
);
echo json_encode($a);
產出
[
"\"test\"",
"'test'",
"'test' \"test\""
]
該函數在JSONLint接收數據之前清除您發送給它的所有數據。
閱讀有關json_encode的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.