JSON

json_decode解析含有特殊字符的字符串

字号+ 作者:H5之家 来源:H5之家 2017-03-17 14:10 我要评论( )

问题:当我们字符串含有特殊字符的情况下,我们该如何巧妙利用json_decode来解析字符串呢? 案例: ?php$s = '[[3807913,□iha,2012-12-16,2012-12-16,127.0.0.1,0,0,0,0,0],[3807913,大家好,2012-12-16,2012-12-16,127.0.0.1,0,0,0,0,0]]';$arr = json_decod

问题:当我们字符串含有特殊字符的情况下,我们该如何巧妙利用json_decode来解析字符串呢?

案例:

<?php $s = '[["3807913","□iha","2012-12-16","2012-12-16","127.0.0.1","0","0","0","0","0"],["3807913","大家好","2012-12-16","2012-12-16","127.0.0.1","0","0","0","0","0"]]'; $arr = json_decode($s,TRUE);//解析失败,由于$s字符串含有特殊字符 print_r($arr); ?>

解决方法

<?php $s = '[["3807913","□iha","2012-12-16","2012-12-16","127.0.0.1","0","0","0","0","0"],["3807913","大家好","2012-12-16","2012-12-16","127.0.0.1","0","0","0","0","0"]]'; $s = substr(str_replace('\"','"',json_encode($s)),1,-1); $arr = json_decode($s,TRUE);//解析成功 print_r($arr); ?>

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Excel文件怎么转JSON? excel转json的教程

    Excel文件怎么转JSON? excel转json的教程

    2017-03-17 15:00

  • jquery中JSON的解析方式

    jquery中JSON的解析方式

    2017-03-17 12:02

  • app 提升webview效率,用app本地存html模板,从服务器读json,用

    app 提升webview效率,用app本地存html模板,从服务器读json,用

    2017-03-17 09:00

  • PHP数组编码转换(UTF8/GBK/json_encode)

    PHP数组编码转换(UTF8/GBK/json_encode)

    2017-03-16 17:01

网友点评