{"id":431,"date":"2018-10-23T18:03:38","date_gmt":"2018-10-23T10:03:38","guid":{"rendered":"http:\/\/blog.yyhcw.com\/?p=431"},"modified":"2018-10-23T18:03:38","modified_gmt":"2018-10-23T10:03:38","slug":"php%e5%b0%8f%e5%81%b7%e8%a7%a3%e6%9e%90http%e8%8e%b7%e5%8f%96%e7%9a%84json%e6%95%b0%e6%8d%ae%e6%80%bb%e6%98%af%e7%a9%ba%e7%99%bdnull","status":"publish","type":"post","link":"https:\/\/blog.yyhcw.com\/post\/431.html","title":{"rendered":"php\u5c0f\u5077\u89e3\u6790http\u83b7\u53d6\u7684json\u6570\u636e\u603b\u662f\u7a7a\u767dNULL"},"content":{"rendered":"<!--wp-compress-html--><!--wp-compress-html no compression--><p>\u6807\u9898\u641e\u7684\u90fd\u4e0d\u77e5\u9053\u600e\u4e48\u8868\u8fbe\u6bd4\u8f83\u6e05\u695a\u4e86\uff1b\u4f7f\u7528<a href=\"https:\/\/blog.yyhcw.com\/post\/tag\/php\" class=\"tag-tooltip\" data-toggle=\"tooltip\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8e PHP \u7684\u6587\u7ae0\" target=\"_blank\">PHP<\/a>\u5199\u4e86\u4e00\u4e2a\u5c0f\u5077\u53bb\u6293\u53d6\u522b\u4eba\u7f51\u7ad9<a href=\"https:\/\/blog.yyhcw.com\/post\/tag\/json\" class=\"tag-tooltip\" data-toggle=\"tooltip\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8e json \u7684\u6587\u7ae0\" target=\"_blank\">json<\/a>\u6570\u636e\uff0c\u6570\u636e\u6293\u53d6\u56de\u6765\u540e\u5f88\u662f\u5947\u602a\uff0c\u53ea\u8981\u4f7f\u7528<a href=\"https:\/\/blog.yyhcw.com\/post\/tag\/json\" class=\"tag-tooltip\" data-toggle=\"tooltip\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8e json \u7684\u6587\u7ae0\" target=\"_blank\">json<\/a>_decode\u8fdb\u884c\u89e3\u6790\u5c31\u76f4\u63a5\u4e3a\u7a7aNULL\uff0c\u800c\u53c8\u76f4\u63a5\u628a\u7ed3\u679c\u5b57\u7b26\u4e32\u590d\u5236\u51fa\u6765\u624b\u52a8\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\u5374\u6b63\u5e38\uff0c\u5728\u524d\u7aefjs\u4e5f\u80fd\u89e3\u6790\uff0c\u501f\u52a9\u7f51\u4e0ajson\u6570\u636e\u76d1\u6d4b\u4e5f\u662f\u6b63\u5e38\u7684json\u6570\u636e\uff0c\u6ca1\u9519\u554a\uff1f\u641e\u4e86\u534a\u5929\u4e0d\u5f97\u5176\u89e3\u3002<\/p>\n<p>\u5728\u6d4b\u8bd5\u7684\u8fc7\u7a0b\u4e2d\u4f7f\u7528 json_last_error() \u51fd\u6570\u6253\u5370\u4e00\u4e0b\u9519\u8bef\uff0c\u9875\u9762\u663e\u793a4\uff0c\u6839\u636e\u5b98\u65b9\u89e3\u91ca\u4e5f\u5c31\u662f\u8bed\u6cd5\u9519\u8bef\u3002\u5f52\u6839\u7ed3\u5e95\u8fd8\u662f\u91c7\u96c6\u8fc7\u6765\u7684\u6570\u636e\u95ee\u9898\uff0c\u90a3\u4e48\u5c31\u53ea\u80fd\u4ece\u8fd9\u91cc\u4e0b\u624b\u4e86\u3002\u4e07\u80fd\u7684\u641c\u7d22\u5f15\u64ce\u8fd8\u662f\u6709\u70b9\u9760\u8c31\uff0c\u5728\u7ffb\u9605\u4e86N\u591a\u6587\u7ae0\u540e\uff0c\u5bf9\u6bd4\u6211\u83b7\u53d6\u6765\u7684\u6570\u636e\u4fe1\u606f\uff0c\u53ef\u80fd\u5c31\u662f\u7f16\u7801\u95ee\u9898\u4e86\uff08json\u4e0d\u652f\u6301gbk\u7f16\u7801\uff0c\u4e5f\u5c31\u662f\u8bf4\u6570\u636e\u7684\u7f16\u7801\u9519\u8bef\uff09\u6700\u7ec8\u6211\u7684\u65b9\u6cd5\u5f97\u4ee5\u89e3\u51b3\uff0c\u76f4\u63a5\u4e0a\u4ee3\u7801\uff1a<\/p>\n<pre class=\"lang:php decode:true\">$result = @iconv(\"UTF-8\",\"GBK\/\/IGNORE\",$result);\r\n$result = @iconv(\"GBK\",\"UTF-8\/\/IGNORE\",$result);\r\nprint_r(json_decode($result, true));<\/pre>\n<p>\u7531\u4e8e\u6211\u6240\u91c7\u96c6\u7684\u6570\u636e\u4e2d\u6709\u7279\u6b8a\u5b57\u7b26\u4e4b\u7c7b\u7684\u4fe1\u606f\uff0c\u53ef\u80fd\u662f\u8fd9\u4e2a\u65b9\u6cd5\u5bfc\u81f4\uff1b<\/p>\n<p>\u53e6\u5916\u9644\u4e0a\u5176\u5b832\u4e2d\u65b9\u6cd5\uff0c\u4e0b\u9762\u7684\u4e24\u79cd\u65b9\u6cd5\u5e76\u672a\u5b9e\u6d4b\uff0c\u6548\u679c\u672a\u77e5\uff1a<\/p>\n<pre class=\"lang:php decode:true \">$result = trim($result, \"\\xEF\\xBB\\xBF\");\r\nprint_r(json_decode($result, true));\r\nexit;<\/pre>\n<p>\u7b2c\u4e09\u79cd\uff1a<\/p>\n<pre class=\"lang:php decode:true \">if (substr($return, 0,3) == pack(\"CCC\",0xef,0xbb,0xbf)) { \r\n    $return = substr($return, 3); \r\n}\r\n$data = json_decode($return,true);<\/pre>\n<p>\u5982\u679c\u4e0a\u9762\u7684\u65b9\u5f0f\u8fd8\u662f\u4e0d\u884c\uff0c\u518d\u8bd5\u8bd5\u4f7f\u7528htmlspecialchars_decode\u8fdb\u884c\u8f6c\u4e49\u540e\u518d\u4f7f\u7528json_decode\u8fdb\u884c\u89e3\u6790\uff1a<\/p>\n<pre class=\"lang:php decode:true\">$content = htmlspecialchars_decode($content);\r\n$content = json_decode($content,true);\r\n<\/pre>\n<p>\u8fd9\u6837\u4e00\u822c\u90fd\u53ef\u4ee5\u89e3\u51b3\u95ee\u9898\u4e86\uff0c\u4ee5\u540e\u9047\u5230\u95ee\u9898\u518d\u8fdb\u884c\u66f4\u65b0\u6b64\u6587\u7ae0\uff01<\/p>\n<!--wp-compress-html no compression--><!--wp-compress-html-->","protected":false},"excerpt":{"rendered":"\u6807\u9898\u641e\u7684\u90fd\u4e0d\u77e5\u9053\u600e\u4e48\u8868\u8fbe\u6bd4\u8f83\u6e05\u695a\u4e86\uff1b\u4f7f\u7528PHP\u5199\u4e86\u4e00\u4e2a\u5c0f\u5077\u53bb\u6293\u53d6\u522b\u4eba\u7f51\u7ad9json\u6570\u636e\uff0c\u6570\u636e\u6293\u53d6\u56de\u6765\u540e\u5f88\u662f\u5947\u602a\uff0c \u00b7\u00b7\u00b7","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[74,14,75],"views":528,"_links":{"self":[{"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/posts\/431"}],"collection":[{"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/comments?post=431"}],"version-history":[{"count":0,"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/posts\/431\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/media?parent=431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/categories?post=431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/tags?post=431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}