2013-04-30 91 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编码。