无法将数组编码为JSON

时间:2013-04-30 04:50:00

标签: php json

我的数组的var_dump:

   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字段。它适用于较短的字符串......我做错了什么?

2 个答案:

答案 0 :(得分:3)

您的评论部分中有一个引号首先使用str_replace将其转义为

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

然后执行json_encode

json_encode($newstring, true);

答案 1 :(得分:3)

最有可能的是,您的字符串未使用json_encode期望的UTF-8进行正确编码。有关详细信息,请参阅UTF-8 all the way through

如果您使用其他编码(例如 Windows-1252)编码现有数据,请使用mb_convert_encoding之类的函数在JSON编码之前将其转换为UTF-8。 / p>

相关问题