{"id":746,"date":"2009-11-19T22:13:00","date_gmt":"2009-11-19T02:13:00","guid":{"rendered":""},"modified":"2013-11-26T14:22:09","modified_gmt":"2013-11-26T06:22:09","slug":"php%e4%b8%adjson%e7%9a%84%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/746.html","title":{"rendered":"PHP\u4e2dJSON\u7684\u5e94\u7528"},"content":{"rendered":"<p>\u4e92\u8054\u7f51\u7684\u4eca\u5929\uff0cAJAX\u5df2\u7ecf\u4e0d\u662f\u4ec0\u4e48\u964c\u751f\u7684\u8bcd\u6c47\u4e86\u3002\u8bf4\u8d77AJAX\uff0c\u53ef\u80fd\u4f1a\u7acb\u5373\u60f3\u8d77\u56e0RSS\u800c\u5174\u8d77\u7684XML\u3002XML\u7684\u89e3\u6790\uff0c\u6050\u6015\u5df2\u7ecf\u4e0d\u662f\u4ec0\u4e48\u96be\u9898\u4e86\uff0c \u7279\u522b\u662fPHP5\uff0c\u5927\u91cf\u7684XML\u89e3\u6790\u5668\u7684\u6d8c\u73b0\uff0c\u5982\u6700\u8f7b\u91cf\u7ea7\u7684SimpleXML\u3002\u4e0d\u8fc7\u5bf9\u4e8eAJAX\u6765\u8bf4\uff0cXML\u7684\u89e3\u6790\u66f4\u503e\u5411\u4e8e\u524d\u53f0Javascript\u7684\u652f \u6301\u5ea6\u3002\u6211\u60f3\u6240\u6709\u89e3\u6790\u8fc7XML\u7684\u4eba\uff0c\u90fd\u4f1a\u56e0\u6811\u548c\u8282\u70b9\u800c\u5934\u5927\u3002\u4e0d\u53ef\u5426\u8ba4\uff0cXML\u662f\u5f88\u4e0d\u9519\u7684\u6570\u636e\u5b58\u50a8\u65b9\u5f0f\uff0c\u4f46\u662f\u5176\u7075\u6d3b\u6070\u6070\u9020\u6210\u4e86\u5176\u89e3\u6790\u7684\u56f0\u96be\u3002\u5f53\u7136\uff0c\u8fd9\u91cc\u6240\u6307\u7684 \u56f0\u96be\uff0c\u662f\u76f8\u5bf9\u4e8e\u672c\u6587\u7684\u4e3b\u89d2\uff0d\uff0dJSON\u800c\u8a00\u3002<\/p>\n<p>JSON\u4e3a\u4f55\u7269\uff1f\u6211\u5c31\u4e0d\u91cd\u590d\u6982\u5ff5\u4e86\u3002\u901a\u4fd7\u7684\u8bf4\uff0c\u5b83\u662f\u4e00\u79cd\u6570\u636e\u7684\u5b58\u50a8\u683c\u5f0f\uff0c\u5c31\u50cfPHP\u5e8f\u5217\u5316\u540e\u7684\u5b57\u7b26\u4e32\u4e00\u6837\u3002\u5b83\u662f\u4e00\u79cd\u6570\u636e\u63cf\u8ff0\u3002\u6bd4\u5982\u6211\u4eec\u5c06\u4e00\u4e2a\u6570\u7ec4\u5e8f \u5217\u5316\u540e\u5b58\u653e\uff0c\u5c31\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u53cd\u5e8f\u5217\u5316\u540e\u5e94\u7528\u3002JSON\u4e5f\u662f\u5982\u6b64\uff0c\u53ea\u4e0d\u8fc7\u4ed6\u642d\u5efa\u7684\u662f\u5ba2\u6237\u7aefJavascript\u548c\u670d\u52a1\u7aefPHP\u7684\u4ea4\u4e92\u6865\u6881\u3002\u6211\u4eec\u7528PHP\u751f\u6210 JSON\u540e\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u628a\u8fd9\u4e2a\u5b57\u7b26\u4e32\u4f20\u7ed9\u524d\u53f0Javascript\uff0cJavascirpt\u5c31\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u5c06\u5176\u53cdJSON\u7136\u540e\u5e94\u7528\u3002\u8bf4\u901a\u4fd7\u70b9\uff0c\u5b83\u771f\u7684\u5f88\u50cf \u6570\u7ec4\u3002<\/p>\n<p>\u8a00\u5f52\u6b63\u4f20\uff0c\u5982\u4f55\u4f7f\u7528JSON\u3002PHP5.2\u5f00\u59cb\u5185\u7f6e\u4e86JSON\u7684\u652f\u6301\u3002\u5f53\u7136\uff0c\u5982\u679c\u4f4e\u4e8e\u8fd9\u4e2a\u7248\u672c\u7684\u8bdd\uff0c\u90a3\u4e48\u5e02\u9762\u4e0a\u6709\u5f88\u591aPHP\u7248\u672c\u7684\u5b9e\u73b0\uff0c\u968f\u4fbf\u4e0b\u4e00\u4e2a\u7528\u5c31OK\u5566\u3002\u73b0\u5728\u4e3b\u8981\u662f\u8bf4\u8bf4PHP\u5185\u7f6e\u652f\u6301\u7684JSON\u3002\u5f88\u7b80\u5355\uff0c\u4e24\u4e2a\u51fd\u6570\uff1ajson_encode\u548cjson_decode\uff08\u8ddf\u5e8f\u5217\u5316\u5f88\u50cf\u5566\uff09\u3002\u4e00\u4e2a\u7f16\u7801\uff0c\u4e00\u4e2a\u89e3\u7801\u3002\u5148\u770b\u770b\u7f16\u7801\u7684\u4f7f\u7528\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n$arr = array(\r\n    'name' =&gt; '\u9648\u6bc5\u946b',\r\n    'nick' =&gt; '\u6df1\u7a7a',\r\n    'contact' =&gt; array(\r\n        'email' =&gt; 'shenkong at qq dot com',\r\n        'website' =&gt; 'http:\/\/www.okajax.com',\r\n     )\r\n);\r\n$json_string = json_encode($arr);\r\necho $json_string;\r\n?&gt;\r\n<\/pre>\n<p>\u5f88\u7b80\u5355\u7684\u5c06\u4e00\u4e2a\u6570\u7ec4JSON\u4e86\u3002\u9700\u8981\u6307\u51fa\u7684\u662f\uff0c\u5728\u975eUTF-8\u7f16\u7801\u4e0b\uff0c\u4e2d\u6587\u5b57\u7b26\u5c06\u4e0d\u53ef\u88abencode\uff0c\u7ed3\u679c\u4f1a\u51fa\u6765\u7a7a\u503c\uff0c\u6240\u4ee5\uff0c\u5982\u679c\u4f60\u4f7f\u7528 gb2312\u7f16\u5199PHP\u4ee3\u7801\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u5c06\u5305\u542b\u4e2d\u6587\u7684\u5185\u5bb9\u4f7f\u7528iconv\u6216\u8005mb\u8f6c\u4e3aUTF-8\u518d\u8fdb\u884cjson_encode\uff0c\u4e0a\u9762\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n{&quot;name&quot;:&quot;\\u9648\\u6bc5\\u946b&quot;,&quot;nick&quot;:&quot;\\u6df1\\u7a7a&quot;,&quot;contact&quot;:{&quot;email&quot;:&quot;shenkong at qq dot com&quot;,&quot;website&quot;:&quot;http:\\\/\\\/www.okajax.com&quot;}}\r\n<\/pre>\n<p>\u6211\u90fd\u8bf4\u4e86\u548c\u5e8f\u5217\u5316\u5f88\u50cf\uff0c\u4f60\u8fd8\u4e0d\u4fe1\u3002\u7f16\u7801\u540e\u5c31\u8981\u89e3\u7801\uff0cPHP\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u51fd\u6570json_decode\uff0cjson_decode\u6267\u884c\u540e\uff0c\u5c06\u4f1a\u5f97\u5230\u4e00\u4e2a\u5bf9\u8c61\uff0c\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n$arr = array(\r\n    'name' =&gt; '\u9648\u6bc5\u946b',\r\n    'nick' =&gt; '\u6df1\u7a7a',\r\n    'contact' =&gt; array(\r\n        'email' =&gt; 'shenkong at qq dot com',\r\n        'website' =&gt; 'http:\/\/www.okajax.com',\r\n     )\r\n);\r\n$json_string = json_encode($arr);\r\n$obj = json_decode($json_string);\r\nprint_r($obj);\r\n?&gt;\r\n<\/pre>\n<p>\u8bbf\u95ee\u5bf9\u8c61\u5185\u7684\u5c5e\u6027\u4f1a\u5427\uff1f$obj-&gt;name\uff0c\u8fd9\u6837\u5b50\u7684\uff0c\u5f53\u7136\uff0c\u4e5f\u53ef\u4ee5\u628a\u5b83\u8f6c\u4f4d\u6570\u7ec4\uff0c\u65b9\u4fbf\u8c03\u7528\u5566\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$json_string = json_encode($arr);\r\n$obj = json_decode($json_string);\r\n$arr = (array) $obj;\r\nprint_r($arr);\r\n<\/pre>\n<p>PHP\u8f6c\u6765\u8f6c\u53bb\u7684\u7528\u9014\u4e0d\u662f\u7279\u522b\u5927\uff0c\u9664\u4e86\u7f13\u5b58\u751f\u6210\uff0c\u611f\u89c9\u8fd8\u4e0d\u5982\u76f4\u63a5\u5b58\u6570\u7ec4\u5462\uff0c\u4e0d\u8fc7\uff0c\u5f53\u4f60\u548c\u524d\u53f0\u4ea4\u4e92\u7684\u65f6\u5019\uff0c\u5b83\u7684\u4f5c\u7528\u5c31\u51fa\u6765\u54af\uff0c\u4e0b\u9762\u770b\u770b\u6211\u600e\u4e48\u7528Javascript\u6765\u4f7f\u7528\u8fd9\u6bb5\u5b57\u7b26\uff1a<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;script type=&quot;text\/javascript&quot;&gt;\r\nvar arr = {&quot;name&quot;:&quot;\\u9648\\u6bc5\\u946b&quot;,&quot;nick&quot;:&quot;\\u6df1\\u7a7a&quot;,&quot;contact&quot;:{&quot;email&quot;:&quot;shenkong at qq dot com&quot;,&quot;website&quot;:&quot;http:\\\/\\\/www.okajax.com&quot;}};\r\nalert(arr.name)\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>\u4e0a\u9762\u4e2d\uff0c\u76f4\u63a5\u5c06\u8fd9\u4e2a\u5b57\u7b26\u4e32\u8d4b\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u5b83\u5c31\u53d8\u6210\u4e00\u4e2aJavascript\u6570\u7ec4\u4e86\uff08\u4e13\u4e1a\u5316\u672f\u8bed\u5e94\u8be5\u4e0d\u53eb\u6570\u7ec4\uff0c\u4e0d\u8fc7\u7531\u4e8ePHP\u7684\u4e60\u60ef\u95ee\u9898\uff0c\u6211\u5c31\u4e00\u76f4\u53eb \u6570\u7ec4\u597d\u4e86\uff0c\u65b9\u4fbf\u7406\u89e3\uff09\u3002\u8fd9\u6837\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u5bf9arr\u8fdb\u884c\u904d\u5386\u6216\u8005\u4efb\u610f\u505a\u4f60\u60f3\u505a\u7684\u4e8b\u60c5\u4e86\u3002\u5199\u5230\u8fd9\u91cc\uff0c\u597d\u50cf\u90fd\u6ca1\u63d0\u5230AJAX\u54e6\uff1f\u662f\u54e6\uff0c\u8054\u60f3\u4e00\u4e0b\uff0c\u5982\u679c\u670d\u52a1\u7aef\u8fd4 \u56de\u7684responseText\u7528JSON\u8fc7\u7684\u5b57\u7b26\u4e32\u4ee3\u66ffXML\u7684\u8bdd\uff0c\u524d\u53f0Javascript\u5904\u7406\u8d77\u6765\u662f\u4e0d\u662f\u5f88\u65b9\u4fbf\u5462\uff1f\u72d7\u76ae\u818f\u836f\u5c31\u662f\u8fd9\u6837\u7528\u7684\u3002<\/p>\n<p>\u5176\u5b9e\u5199\u5230\u8fd9\u91cc\uff0c\u9664\u4e86\u6570\u636e\u7684\u5b58\u50a8\u683c\u5f0f\u4e0d\u592a\u4e00\u6837\u5916\uff0cJSON\u548cXML\u4e5f\u6ca1\u4ec0\u4e48\u592a\u5927\u533a\u522b\u54e6\uff0c\u4e0d\u8fc7\u4e0b\u9762\u6211\u8bf4\u7684\u4e00\u70b9\u3002\u867d\u7136\u548cXML\u6ca1\u591a\u5927\u5173\u7cfb\uff0c\u4e0d\u8fc7\uff0c\u53ef\u4ee5\u8bf4\u660e JSON\u66f4\u5927\u8303\u56f4\u7684\u5e94\u7528\uff0c\u90a3\u5c31\u662f\uff0c\u8de8\u57df\u7684\u6570\u636e\u8c03\u7528\u3002\u7531\u4e8e\u5b89\u5168\u6027\u95ee\u9898\uff0cAJAX\u4e0d\u652f\u6301\u8de8\u57df\u8c03\u7528\uff0c\u8fd9\u6837\u8981\u8c03\u7528\u4e0d\u540c\u57df\u540d\u4e0b\u7684\u6570\u636e\uff0c\u5f88\u9ebb\u70e6\u54e6\uff0c\u867d\u7136\u6709\u89e3\u51b3\u65b9\u6848 \uff08stone\u5728\u4ed6\u7684\u8bb2\u5ea7\u4e0a\u63d0\u5230\u8fc7\u4e86\u4ee3\u7406\u554a\u4ec0\u4e48\u7684\u867d\u7136\u542c\u4e0d\u61c2\u4f46\u662f\u77e5\u9053\u80fd\u89e3\u51b3\uff09\u3002\u6211\u5199\u4e24\u4e2a\u6587\u4ef6\uff0c\u8db3\u4ee5\u5c55\u793a\u8de8\u57df\u8c03\u7528\u4e86\u3002<\/p>\n<p>\u4e3b\u8c03\u6587\u4ef6index.html<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;script type=&quot;text\/javascript&quot;&gt;\r\nfunction getProfile(str) {\r\n     var arr = str;\r\n     document.getElementById('nick').innerHTML = arr.nick;\r\n}\r\n&lt;\/script&gt;\r\n&lt;body&gt;&lt;div id=&quot;nick&quot;&gt;&lt;\/div&gt;&lt;\/body&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;http:\/\/www.openphp.cn\/demo\/profile.php&quot;&gt;&lt;\/script&gt;\r\n<\/pre>\n<p>\u88ab\u8c03\u6587\u4ef6profile.php<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n$arr = array(\r\n    'name' =&gt; '\u9648\u6bc5\u946b',\r\n    'nick' =&gt; '\u6df1\u7a7a',\r\n    'contact' =&gt; array(\r\n        'email' =&gt; 'shenkong at qq dot com',\r\n        'website' =&gt; 'http:\/\/www.okajax.com',\r\n     )\r\n);\r\n$json_string = json_encode($arr);\r\necho &quot;getProfile($json_string)&quot;;\r\n?&gt;\r\n<\/pre>\n<p>\u5f88\u663e\u7136\uff0c\u5f53index.html\u8c03\u7528profile.php\u65f6\uff0cJSON\u5b57\u7b26\u4e32\u751f\u6210\uff0c\u5e76\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165getProfile\uff0c\u7136\u540e\u5c06\u6635\u79f0\u63d2\u5165\u5230div\u4e2d\uff0c\u8fd9\u6837\u4e00\u6b21\u8de8\u57df\u6570\u636e\u4ea4\u4e92\u5c31\u5b8c\u6210\u4e86\uff0c\u662f\u4e0d\u662f\u7279\u522b\u7b80\u5355\u3002\u65e2\u7136JSON\u8fd9\u4e48\u7b80\u5355\u6613\u7528\u800c\u4e14\u597d\u7528\uff0c\u8fd8\u7b49\u4ec0\u4e48\u5462\uff1f^_^<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e92\u8054\u7f51\u7684\u4eca\u5929\uff0cAJAX\u5df2\u7ecf\u4e0d\u662f\u4ec0\u4e48\u964c\u751f\u7684\u8bcd\u6c47\u4e86\u3002\u8bf4\u8d77AJAX\uff0c\u53ef\u80fd\u4f1a\u7acb\u5373\u60f3\u8d77\u56e0RSS\u800c\u5174\u8d77\u7684XML\u3002XML\u7684\u89e3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-746","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/746","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=746"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/746\/revisions"}],"predecessor-version":[{"id":5252,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/746\/revisions\/5252"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}