php的json_decode函数返回null的问题

2014-4-4 liyangweb.com 李杨 PHP

php5.2以后自带json_decode函数,但是对json文本串的格式要求非常严格。

很可能使用该函数得到的返回值是NULL

可以使用使用json_last_error()函数获取到的返回值来帮助我们判断出问题的原因。

其中如果提示错误JSON_ERROR_SYNTAX(Syntax error),表示json串格式错误。

可以通过以下几个方式排错:

1. json字符串必须以双引号包含

$output = str_replace("'", '"', $output);

2. json字符串必须是utf8编码
$output = iconv('gbk', 'utf8', $output);

3.不能有多余的逗号 如:[1,2,]
用正则替换掉,preg_replace('/,\s*([\]}])/m', '$1', $output)

标签: PHP

评论:

jlchatlaw
2019-01-17 15:14
<a href="http://www.hotelfuat.com/jordan-retro-10-royal-blue-and-white-skoa">jordan retro 10 royal blue and white</a> <a href="http://www.pmisaltriver.com/kyrie-1-all-star-sneakere">kyrie 1 all star</a> <a href="http://www.ductambag.com/nike-lebron-13-kvinders-himmelbl%C3%A5-guld-kurpesa">nike lebron 13 kvinders himmelbl氓 guld</a> <a href="http://www.priverussia.com/nike-kd-8-hunts-hill-sunrise-on-feet-sneakerb">nike kd 8 hunts hill sunrise on feet</a> <a href="http://www.coatsfrance.com/blue-green-mens-nike-lunarglide-8-shoes-shoesg">blue green mens nike lunarglide 8 shoes</a> <a href="http://www.intoloo.com/boston-red-sox-green-cap-hats">boston red sox green cap</a>
jlchatlaw http://www.jlchatlaw.com/
172h9yel
2019-01-10 15:58
<a href="http://www.33ocbhxx.com/nike-air-jordan-8-retro-schwarz-chrome-for-verkauf-obuvb">nike air jordan 8 retro schwarz chrome for verkauf</a> <a href="http://www.ou5cxgy0.com/air-jordan-11-svart-r%C3%B8d-2001-scarpec">air jordan 11 svart r酶d 2001</a> <a href="http://www.5m9w6ysn.com/herren-nike-roshe-run-speckle-alle-rosa-schuhef">herren nike roshe run speckle alle rosa</a> <a href="http://www.6iqdfvgi.com/nike-roshe-yeezy-boost-sort-scarpeb">nike roshe yeezy boost sort</a> <a href="http://www.nfnl51ta.com/nike-jordan-6-nero-friday-schuhee">nike jordan 6 nero friday</a> <a href="http://www.p4vepcy8.com/nike-lunarglide-8-night-maroon-noble-rot-schwarz-total-crimson-butyf">nike lunarglide 8 night maroon noble rot schwarz total crimson</a>
172h9yel http://www.172h9yel.com/
malagakizomba
2018-12-30 02:04
<a href="http://www.jobbstockholm.com/bobby-massie-mens-limited-orange-jersey-nike-nfl-chicago-bears-vapor-untouchable-alternate-70-nflr">bobby massie mens limited orange jersey nike nfl chicago bears vapor untouchable alternate 70</a> <a href="http://www.johnklymshyn.com/femminile-nike-free-run-4.0-v3-butyg">femminile nike free run 4.0 v3</a> <a href="http://www.medicinennet.com/nfl-andre-smith-womens-ash-71-arizona-cardinals-nike-backer-vneck-t-shirt-nflz">nfl andre smith womens ash 71 arizona cardinals nike backer vneck t shirt</a> <a href="http://www.nereephotos.com/jets-8-teemu-selanne-bluewhite-ccm-throwback-stitched-nhl-jersey-nflx">jets 8 teemu selanne bluewhite ccm throwback stitched nhl jersey</a> <a href="http://www.nutraxtractor.com/air-jordan-11-gr%C3%A5-chaussuresa">air jordan 11 gr氓</a> <a href="http://www.resqglobal.com/nike-air-max-thea-hellgrau-lyser%C3%B8d-butya">nike air max thea hellgrau lyser酶d</a>
malagakizomba http://www.malagakizomba.com/
pmisaltriver
2018-12-28 15:17
<a href="http://www.georgesherry.com/nike-free-3.0-v5-gr%C3%B8n-glow-jordans-scarpeb">nike free 3.0 v5 gr酶n glow jordans</a> <a href="http://www.jenalgroup.com/womens-nike-huarache-utility-black-denmark-shoeso">womens nike huarache utility black denmark</a> <a href="http://www.badacamshotel.com/air-jordan-future-mujeres-cielo-azul-kurpesb">air jordan future mujeres cielo azul</a> <a href="http://www.compressmyjs.com/nike-sandals-for-toddlers-zapatost">nike sandals for toddlers</a> <a href="http://www.fetison.com/girls-high-converse-shoesz">girls high converse</a> <a href="http://www.neffloot.com/air-jordan-13-retro-low-brave-bleu-uniform-schuhec">air jordan 13 retro low brave bleu uniform</a>
[url=http://www.pmisaltriver.com/]pmisaltriver[/url]
estilomusic
2018-12-28 10:46
<a href="http://www.lifeofeighty.com/air-jordan-retro-10-hvid-and-sort-schuheb">air jordan retro 10 hvid and sort</a> <a href="http://www.planetmolly.com/nike-limited-barkevious-mingo-jersey-51-nfl-seattle-seahawks-2017-salute-to-service-womens-olive-nflr">nike limited barkevious mingo jersey 51 nfl seattle seahawks 2017 salute to service womens olive</a> <a href="http://www.potluckposter.com/denver-broncos-darian-stewart-navy-blue-nike-26-nfl-pullover-hoodie-name-number-logo-nflw">denver broncos darian stewart navy blue nike 26 nfl pullover hoodie name number logo</a> <a href="http://www.serigamp.com/air-huarache-mujeres-schuheg">air huarache mujeres</a> <a href="http://www.brendonrush.com/nike-dunk-low-dam%C3%A4nner-casual-schuh-scarpeg">nike dunk low dam盲nner casual schuh</a> <a href="http://www.cathyrogoff.com/zx-flux-core-sort-ftwr-hvid-kurpesf">zx flux core sort ftwr hvid</a>
estilomusic http://www.estilomusic.com/

发表评论:

Powered by emlog 冀ICP备13011830号-1