经常使用json_decode()转换json字符串,一直都很顺利,然而今天碰到了一个头疼的问题,json字符串转换过之后一直为null
上网搜索了下,可以使用json_last_error()函数来查看json转换最后发生的错误,json_last_error_msg()用来显示最后发生的错误详情。可以分别参考json_last_errorjson_last_error_msg
一般错误都是为4,意思是编码错误。因为json_decode()只能够转换UTF-8编码的json字符串。
然而,我的错误显示是3控制字符错误,可能是编码不对
尝试过以下方法:

一: json 字符串中反斜杠被转义,使用 htmlspecialchars_decode() 函数处理;
二:在保存 json 数据时使用 urlencode() 函数;(因为json数据不是在我这边控制的,而且将得到的json保存为文本再转换是没问题的,故没验证该方法)
三:将编码转换为UTF-8$content = mb_detect_encoding($json, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));       //查看编码
$json= mb_convert_encoding($content tent , "UTF-8", "GBK");                //将字符串转为UTF-8
四:在文件头加
五:查找文件中是否存在`bom`,如果存在也会出现该问题;
六:检查json字符串中是否多逗号;
七:检查json中的名称和值是否用的是双引号,在json里只能用双引号表示字符串,不要用单引号;
八:检查json中的值是否有数字类型的,并且首位为零;(这个貌似没问题)
九:字符的表示方式方式有很多种,但下面的都不支持 ,如:`\x06` ,可以将这些字符换为空,如:

json=strreplace("\x06","", json);

因为php也支持这种十六进制方式,所以可以用eval来达到转义效果。

我的问题是因为使用了AES进行加密、解密,所以可能这个过程中出现了问题,最后把不支持的字符去掉就OK了。这个还是在overstockflow上找到的。

Author:leedaning
本文地址:http://blog.csdn.net/leedaning/article/details/70210770

GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
f06604fc * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me> Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 3 天前
d23291ba * add a ci step for Json_Diagnostic_Positions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * Update ci.cmake to address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typo in the comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typos in ci.cmake Signed-off-by: Harinath Nampally <harinath922@gmail.com> * invoke the new ci step from ubuntu.yml Signed-off-by: Harinath Nampally <harinath922@gmail.com> * issue4561 - use diagnostic positions for exceptions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci check failures for unit-diagnostic-postions.cpp Signed-off-by: Harinath Nampally <harinath922@gmail.com> * improvements based on review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix const correctness string Signed-off-by: Harinath Nampally <harinath922@gmail.com> * further refinements based on reviews Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add one more test case for full coverage Signed-off-by: Harinath Nampally <harinath922@gmail.com> * ci check fix - add const Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add unit tests for json_diagnostic_postions only Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_diagnostics Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_build_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> --------- Signed-off-by: Harinath Nampally <harinath922@gmail.com> 3 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐