2017-09-28 25 views
1

我有一個問題,存儲在mysql文本字段中的json,事情是,一切都OK,直到我使用'(單引號)json然後保存\',每次我將它保存回來追加一個斜槓,如:\ '\\' \\」等從MySQL的JSON打破

則有些字符編碼不正確\ u00e0

<div class="wpbs-calendar-legend" data-info="{&quot;default&quot;:{&quot;name&quot;:{&quot;default&quot;:&quot;Available&quot;,&quot;hr&quot;:&quot;Slobodno&quot;,&quot;cs&quot;:&quot;Volno&quot;,&quot;da&quot;:&quot;Ledigt&quot;,&quot;nl&quot;:&quot;Vrij&quot;,&quot;en&quot;:&quot;Available&quot;,&quot;fr&quot;:&quot;Libre&quot;,&quot;de&quot;:&quot;Frei&quot;,&quot;hu&quot;:&quot;Szabad&quot;,&quot;it&quot;:&quot;Libero&quot;,&quot;ro&quot;:&quot;Disponobil&quot;,&quot;ru&quot;:&quot;\u0414\u043e\u0441\u0442\u0443\u043f\u043d\u043e&quot;,&quot;sk&quot;:&quot;Vo\u013en\u00fd&quot;,&quot;es&quot;:&quot;Libre&quot;,&quot;sv&quot;:&quot;Ledigt&quot;,&quot;uk&quot;:&quot;B\u0456\u043b\u044c\u043d\u043e&quot;,&quot;no&quot;:&quot;&quot;},&quot;color&quot;:&quot;#f1ffcc&quot;,&quot;splitColor&quot;:false,&quot;bookable&quot;:&quot;yes&quot;,&quot;auto-pending&quot;:&quot;no&quot;,&quot;sync&quot;:&quot;no&quot;},&quot;1&quot;:{&quot;name&quot;:{&quot;default&quot;:&quot;Booked&quot;,&quot;hr&quot;:&quot;Zauzeto&quot;,&quot;cs&quot;:&quot;Obsazeno&quot;,&quot;da&quot;:&quot;Booket&quot;,&quot;nl&quot;:&quot;Bezet&quot;,&quot;en&quot;:&quot;Booked&quot;,&quot;fr&quot;:&quot;Occup\u00e9&quot;,&quot;de&quot;:&quot;Belegt&quot;,&quot;hu&quot;:&quot;Foglalt&quot;,&quot;it&quot;:&quot;Prenotato&quot;,&quot;ro&quot;:&quot;Rezervat&quot;,&quot;ru&quot;:&quot;\u0417\u0430\u043d\u044f\u0442\u043e&quot;,&quot;sk&quot;:&quot;Obsaden\u00fd&quot;,&quot;es&quot;:&quot;Reservado&quot;,&quot;sv&quot;:&quot;Bokat&quot;,&quot;uk&quot;:&quot;\u0417\u0430\u0439\u043d\u044f\u0442\u043e&quot;,&quot;no&quot;:&quot;&quot;},&quot;color&quot;:&quot;#ff524c&quot;,&quot;splitColor&quot;:false,&quot;bookable&quot;:false,&quot;auto-pending&quot;:&quot;yes&quot;,&quot;sync&quot;:&quot;yes&quot;},&quot;2&quot;:{&quot;name&quot;:{&quot;en&quot;:&quot;>14H&quot;,&quot;nl&quot;:&quot;&quot;,&quot;et&quot;:&quot;&quot;,&quot;fi&quot;:&quot;&quot;,&quot;fr&quot;:&quot;D\u00e8s 14H&quot;,&quot;de&quot;:&quot;&quot;,&quot;pt&quot;:&quot;&quot;,&quot;ro&quot;:&quot;&quot;,&quot;default&quot;:&quot;D\u00e8s 14H&quot;},&quot;color&quot;:&quot;#f1ffcc&quot;,&quot;splitColor&quot;:&quot;#ff524c&quot;,&quot;bookable&quot;:&quot;yes&quot;,&quot;hide&quot;:false,&quot;auto-pending&quot;:&quot;no&quot;,&quot;sync&quot;:&quot;no&quot;},&quot;3&quot;:{&quot;name&quot;:{&quot;en&quot;:&quot;<12H&quot;,&quot;nl&quot;:&quot;&quot;,&quot;et&quot;:&quot;&quot;,&quot;fi&quot;:&quot;&quot;,&quot;fr&quot;:&quot;Jusqu\\" \u00e0="" 10h","de":"","pt":"","ro":"","default":"jusqu\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\u00e0="" 10h"},"color":"#ff524c","splitcolor":"#f1ffcc","bookable":"yes","hide":false,"auto-pending":"no","sync":"no"}}'=""></div> 

,在我insepct窗口如下所示:

developer console

我使用的是WordPress,所以這個json_encoded字符串使用update_option保存到數據庫中。我試過對strip_heshes()在update_option上的json編碼字符串,但沒有解決問題。

有人可以幫助我嗎?

感謝您的時間!

+0

的數據信息是htmlentity編碼。 – Daniel

回答

1

它看起來像你的SQL插入是逃避 - 非常正確 - 數據。

現在,當您需要從SQL獲取數據時,您需要使用反斜槓刪除轉義。

因此,如果你的變量是$jsonResult使用它作爲stripslashes($jsonResult)

+0

我已經試過了stripslashes,並沒有解決我的問題,因爲json_encoded字符串使用WordPress的update_option函數保存(需要編輯問題以反映我使用wordpress) –

+0

轉義不起作用。添加斜槓,將結果發送到數據庫,斜槓由SQL解析器使用,不包含在數據庫中存儲的數據中。 – Quentin

1

不,不是細節,不是細線。如果從json_encode()得到了JSON,使用一個額外的參數,以避免「統一」的編碼:在

$x = json_encode($s, JSON_UNESCAPED_UNICODE); 

見例2 http://php.net/manual/en/function.json-encode.php (需要PHP 5.4.0)