從TEXT包含 JSON 對象數組的列類型(在本例中為“詳細信息”列)的數據庫中返回行時遇到問題。例子:{ productid: 1 shopid: 1 title: 'Product 1'--> details: [{"name":"Brand name","value":"Brand value"},{"name":"Color","value":"blue"}] . . .}在類型列中插入數據庫的數據在TEXT插入addslashes(json_encode($array_or_object))之前像安全轉義一樣插入。返回數據時,TEXT按函數類型返回的列json_decode()沒有問題。問題當有人試圖用單一的發(fā)生'和雙引號"在details。例子:details: [{"name":"\\\"test123\\\"","value":"\\\"test\\\" \\'test\\' \\\"test \\' test \\' t\\\"est"}]返回值如下所示:"details": [ { "name": "\\\"test123\\\"", "value": "\\\"test\\\" \\'test\\' \\\"test \\' test \\' t\\\"est" }],我有不止一種JSON在數據庫中存儲數據的方法(作為對象、對象數組、對象數組數組……),我需要一種方法來轉義這些反斜杠。使用stripslashes()使用前的字符串json_decode()不起作用,它打破了JSON。創(chuàng)建一個遞歸函數是可行的,但并不像我希望的那樣漂亮。例子:function decode_json($json) { if (empty($json)) { return $json; } if (is_string($json)) { $json = json_decode($json, true); } foreach ($json as $key => $value) { if (is_array($value)) { $json[$key] = decode_json($value); continue; } $json[$key] = stripslashes($value); } return $json;}任何幫助表示贊賞。
- 2 回答
- 0 關注
- 232 瀏覽
添加回答
舉報
0/150
提交
取消