2013-04-30 39 views
0

的var_dump我的數組:無法編碼陣列JSON

array(10) { ["idcomment"]=> string(2) "26" [0]=> string(2) "26" ["parentcomment_id"]=> string(2) "25" [1]=> string(2) "25" ["comment"]=> string(531) "damnJoin Dev Center and publish your app in the Windows Phone Store. GET SDK GET SDK Download the tools to build great Windows Phone apps. VIEW SAMPLES VIEW SAMPLES View code samples from Microsoft and the community to get started. Develop games for Windows PhoneThere has never been a better time to develop games for Windows Phone. We’ve made it simple for you to get started with support for native code and in-app purchase. One third of all Windows Phone downloads and 60 percent of revenue are from games – get started now." [2]=> string(531) "damnJoin Dev Center and publish your app in the Windows Phone Store. GET SDK GET SDK Download the tools to build great Windows Phone apps. VIEW SAMPLES VIEW SAMPLES View code samples from Microsoft and the community to get started. Develop games for Windows PhoneThere has never been a better time to develop games for Windows Phone. We’ve made it simple for you to get started with support for native code and in-app purchase. One third of all Windows Phone downloads and 60 percent of revenue are from games – get started now." ["dt"]=> string(19) "2013-04-29 21:36:29" [3]=> string(19) "2013-04-29 21:36:29" ["iduser"]=> string(1) "1" [4]=> string(1) "1" } 

下面是導致json_encode($comment)

{"idcomment":"26","0":"26","parentcomment_id":"25","1":"25","comment":null,"2":null,"dt":"2013-04-29 21:36:29","3":"2013-04-29 21:36:29","iduser":"1","4":"1"} 

我失蹤comment場。它適用於較短的字符串...我做錯了什麼?

+0

這可能是單引號,請嘗試刪除它並查看它是否有效。如果它確實需要逃避它。 – shapeshifter 2013-04-30 04:52:40

回答

3

你在你的評論部分有一個單一的qoutes首先使用這樣

$newstring =str_replace('\'', '\\\'', $myString); 

str_replace函數它,然後做json_encode

json_encode($newstring, true); 
+0

+1 json_encode()對單引號和雙引號非常挑剔。 – Jared 2013-04-30 04:54:38

+0

有點偏離主題。是否有一個規範 - 爲成功的json編碼準備一個字符串? – Andrew 2013-04-30 04:56:29

+0

也許htmlentities($字符串)會有幫助嗎? – Lixas 2013-04-30 04:59:26

3

最有可能的,你的字符串是不正確地使用UTF-8編碼逃生,其中json_encode預計。有關更多信息,請參見UTF-8 all the way through

如果已經使用一些其他的編碼(例如視窗-1252)編碼的現有數據,使用函數如mb_convert_encoding到它之前轉換爲UTF-8至JSON編碼。