{"id":64270,"date":"2023-11-30T22:13:15","date_gmt":"2023-11-30T14:13:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=64270"},"modified":"2023-11-30T22:13:17","modified_gmt":"2023-11-30T14:13:17","slug":"php%e5%92%8cjavascript%e5%9c%a8%e5%a4%84%e7%90%86json%e6%95%b0%e6%8d%ae%e6%97%b6%e6%9c%89%e5%93%aa%e4%ba%9b%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/64270.html","title":{"rendered":"PHP\u548cJavaScript\u5728\u5904\u7406JSON\u6570\u636e\u65f6\u6709\u54ea\u4e9b\u533a\u522b"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\">\n<p>\u672c\u6587\u8be6\u7ec6\u63a2\u8ba8\u4e86PHP\u548cJavaScript\u4e24\u79cd\u7f16\u7a0b\u8bed\u8a00\u5728\u5904\u7406JSON\u6570\u636e\u65b9\u9762\u7684\u4e3b\u8981\u533a\u522b\u3002\u8fd9\u4e9b\u533a\u522b\u5305\u62ec\uff1a1.JSON\u6570\u636e\u89e3\u6790\u548c\u751f\u6210\u7684\u5185\u7f6e\u51fd\u6570\uff1b2.\u9519\u8bef\u5904\u7406\u673a\u5236\uff1b3.\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u89c4\u5219\uff1b4.\u6027\u80fd\u8003\u91cf; 5.\u5b9e\u9645\u5e94\u7528\u573a\u666f\u3002\u7406\u89e3\u8fd9\u4e9b\u5dee\u5f02\u5bf9\u4e8e\u5f00\u53d1\u8de8\u5e73\u53f0\u7684Web\u5e94\u7528\u7a0b\u5e8f\u548cAPIs\u81f3\u5173\u91cd\u8981\uff0c\u6709\u52a9\u4e8e\u4f18\u5316\u6570\u636e\u4ea4\u6362\u548c\u5904\u7406\u6d41\u7a0b\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">1.JSON\u6570\u636e\u89e3\u6790\u548c\u751f\u6210\u7684\u5185\u7f6e\u51fd\u6570<\/h2>\n\n\n\n<p>PHP: \u4f7f\u7528json_decode()\u548cjson_encode()\u51fd\u6570\u6765\u89e3\u6790\u548c\u751f\u6210JSON\u6570\u636e\u3002json_decode()\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPHP\u6570\u7ec4\u6216\u5bf9\u8c61\uff0c\u800cjson_encode()\u5c06PHP\u6570\u7ec4\u6216\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p>JavaScript: \u4f7f\u7528JSON.parse()\u548cJSON.stringify()\u65b9\u6cd5\u3002JSON.parse()\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aJavaScript\u5bf9\u8c61\uff0c\u800cJSON.stringify()\u5219\u5c06JavaScript\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u9519\u8bef\u5904\u7406\u673a\u5236<\/h2>\n\n\n\n<p>PHP: json_decode()\u548cjson_encode()\u5728\u5904\u7406\u9519\u8bef\u65f6\u901a\u5e38\u8fd4\u56denull\uff0c\u5e76\u63d0\u4f9bjson_last_error()\u51fd\u6570\u6765\u83b7\u53d6\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>JavaScript: JSON.parse()\u548cJSON.stringify()\u5728\u9047\u5230\u9519\u8bef\u65f6\u629b\u51fa\u5f02\u5e38\uff0c\u53ef\u4ee5\u901a\u8fc7try-catch\u8bed\u53e5\u8fdb\u884c\u9519\u8bef\u5904\u7406\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u89c4\u5219<\/h2>\n\n\n\n<p>PHP: \u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0cPHP\u7684json_decode()\u51fd\u6570\u63d0\u4f9b\u4e86\u9009\u9879\u6765\u63a7\u5236\u662f\u5426\u5c06JSON\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5173\u8054\u6570\u7ec4\u3002<\/p>\n\n\n\n<p>JavaScript: JSON\u6570\u636e\u548cJavaScript\u5bf9\u8c61\u4e4b\u95f4\u7684\u8f6c\u6362\u76f8\u5bf9\u76f4\u63a5\uff0c\u56e0\u4e3aJSON\u4f7f\u7528\u4e86\u4e0eJavaScript\u5bf9\u8c61\u5b57\u9762\u91cf\u76f8\u4f3c\u7684\u683c\u5f0f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.\u6027\u80fd\u8003\u91cf<\/h2>\n\n\n\n<p>PHP\u548cJavaScript\u5904\u7406JSON\u6570\u636e\u7684\u6548\u7387\u53d6\u51b3\u4e8e\u6570\u636e\u5927\u5c0f\u548c\u590d\u6742\u6027\u3002\u5728\u670d\u52a1\u5668\u7aef\uff0cPHP\u53ef\u80fd\u5728\u5904\u7406\u5927\u578b\u6216\u590d\u6742\u7684JSON\u6570\u636e\u65f6\u66f4\u9ad8\u6548\u3002<\/p>\n\n\n\n<p>\u5728\u5ba2\u6237\u7aef\uff0cJavaScript\u7684JSON\u5904\u7406\u901a\u5e38\u66f4\u5feb\uff0c\u56e0\u4e3a\u6570\u636e\u65e0\u9700\u5728\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u4f20\u8f93\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h2>\n\n\n\n<p>PHP\u5728\u670d\u52a1\u5668\u7aef\u5904\u7406JSON\u6570\u636e\u65f6\u901a\u5e38\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\uff0c\u800cJavaScript\u5219\u5e7f\u6cdb\u7528\u4e8e\u5ba2\u6237\u7aef\uff0c\u5904\u7406\u4ece\u670d\u52a1\u5668\u63a5\u6536\u7684JSON\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u6bd4\u8f83PHP\u548cJavaScript\u5728\u5904\u7406JSON\u6570\u636e\u7684\u4e0d\u540c\uff0c\u672c\u6587\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u5728\u4e0d\u540c\u73af\u5883\u548c\u5e94\u7528\u573a\u666f\u4e0b\u9009\u62e9\u5408\u9002\u8bed\u8a00\u5904\u7406JSON\u6570\u636e\u7684\u6307\u5bfc\u3002<\/p>\n\n\n\n<p><img fetchpriority=\"high\" decoding=\"async\" width=\"484\" height=\"300\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/11\/227381-PHP\u548cJavaScript\u5728\u5904\u7406JSON\u6570\u636e\u65f6\u6709\u54ea\u4e9b\u533a\u522b.png?x-oss-process=image\/auto-orient,1\/format,webp\" class=\"aligncenter\" alt=\"PHP\u548cJavaScript\u5728\u5904\u7406JSON\u6570\u636e\u65f6\u6709\u54ea\u4e9b\u533a\u522b\" \/><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\" \/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u5e38\u89c1\u95ee\u7b54<\/strong><\/h2>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-layout-1 wp-block-group-is-layout-flex\">\n<ul>\n<li><strong>\u95ee\uff1aPHP\u548cJavaScript\u5728\u89e3\u6790JSON\u6570\u636e\u65f6\u6709\u4ec0\u4e48\u4e3b\u8981\u533a\u522b\uff1f<\/strong><\/li>\n\n\n\n<li><strong>\u7b54\uff1a<\/strong>\u5728PHP\u4e2d\uff0c\u89e3\u6790JSON\u901a\u5e38\u4f7f\u7528json_decode()\u51fd\u6570\uff0c\u5b83\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPHP\u7684\u6570\u7ec4\u6216\u5bf9\u8c61\u3002\u800c\u5728JavaScript\u4e2d\uff0c\u89e3\u6790JSON\u4f7f\u7528JSON.parse()\u65b9\u6cd5\uff0c\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aJavaScript\u5bf9\u8c61\u3002PHP\u4e2d\u7684\u89e3\u6790\u66f4\u503e\u5411\u4e8e\u670d\u52a1\u7aef\u64cd\u4f5c\uff0c\u800cJavaScript\u901a\u5e38\u5728\u5ba2\u6237\u7aef\u8fdb\u884c\u89e3\u6790\u3002<\/li>\n<\/ul>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-layout-2 wp-block-group-is-layout-flex\">\n<ul>\n<li><strong>\u95ee\uff1aPHP\u548cJavaScript\u5728\u751f\u6210JSON\u6570\u636e\u65f6\u4f7f\u7528\u7684\u65b9\u6cd5\u6709\u4ec0\u4e48\u4e0d\u540c\uff1f<\/strong><\/li>\n\n\n\n<li><strong>\u7b54\uff1a<\/strong>\u5728PHP\u4e2d\uff0c\u751f\u6210JSON\u6570\u636e\u4e3b\u8981\u4f7f\u7528json_encode()\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06PHP\u6570\u7ec4\u6216\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u5728JavaScript\u4e2d\uff0c\u751f\u6210JSON\u6570\u636e\u4f7f\u7528JSON.stringify()\u65b9\u6cd5\uff0c\u5b83\u5c06JavaScript\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u3002\u8fd9\u4e24\u79cd\u65b9\u6cd5\u5728\u8bed\u6cd5\u548c\u4f7f\u7528\u4e0a\u5404\u6709\u7279\u70b9\uff0c\u4f46\u529f\u80fd\u76f8\u4f3c\u3002<\/li>\n<\/ul>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-layout-3 wp-block-group-is-layout-flex\">\n<ul>\n<li><strong>\u95ee\uff1a\u5728\u5904\u7406JSON\u6570\u636e\u65f6\uff0cPHP\u548cJavaScript\u5728\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u65b9\u9762\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><\/li>\n\n\n\n<li><strong>\u7b54\uff1a<\/strong>PHP\u5728\u4f7f\u7528json_decode()\u65f6\uff0c\u9ed8\u8ba4\u5c06JSON\u5bf9\u8c61\u8f6c\u6362\u4e3aPHP\u7684\u6807\u51c6\u5bf9\u8c61\u3002\u5982\u679c\u5e0c\u671b\u8f6c\u6362\u4e3a\u5173\u8054\u6570\u7ec4\uff0c\u9700\u8981\u989d\u5916\u8bbe\u7f6e\u53c2\u6570\u3002\u800cJavaScript\u7684JSON.parse()\u81ea\u7136\u5730\u5c06JSON\u5bf9\u8c61\u8f6c\u6362\u4e3aJavaScript\u7684\u5bf9\u8c61\u3002\u5728\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e0a\uff0cPHP\u63d0\u4f9b\u4e86\u66f4\u591a\u7075\u6d3b\u6027\uff0c\u4f46\u4e5f\u9700\u8981\u5f00\u53d1\u8005\u66f4\u660e\u786e\u5730\u4e86\u89e3\u548c\u6307\u5b9a\u60f3\u8981\u7684\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-layout-4 wp-block-group-is-layout-flex\">\n<ul>\n<li><strong>\u95ee\uff1a\u5728\u9519\u8bef\u5904\u7406\u65b9\u9762\uff0cPHP\u548cJavaScript\u5904\u7406JSON\u7684\u65b9\u5f0f\u6709\u4ec0\u4e48\u4e0d\u540c\uff1f<\/strong><\/li>\n\n\n\n<li><strong>\u7b54\uff1a<\/strong>\u5728PHP\u4e2d\uff0c\u5982\u679cjson_decode()\u6216json_encode()\u9047\u5230\u9519\u8bef\uff0c\u5b83\u4eec\u4f1a\u8fd4\u56deNULL\uff0c\u5e76\u53ef\u4ee5\u4f7f\u7528json_last_error()\u51fd\u6570\u83b7\u53d6\u9519\u8bef\u4fe1\u606f\u3002\u800c\u5728JavaScript\u4e2d\uff0cJSON.parse()\u548cJSON.stringify()\u5728\u89e3\u6790\u6216\u5b57\u7b26\u4e32\u5316\u65e0\u6548\u7684JSON\u65f6\u4f1a\u629b\u51faSyntaxError\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728JavaScript\u4e2d\u9700\u8981\u4f7f\u7528try-catch\u8bed\u53e5\u6765\u5904\u7406\u53ef\u80fd\u7684\u9519\u8bef\u3002<\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"\u672c\u6587\u8be6\u7ec6\u63a2\u8ba8\u4e86PHP\u548cJavaScript\u4e24\u79cd\u7f16\u7a0b\u8bed\u8a00\u5728\u5904\u7406JSON\u6570\u636e\u65b9\u9762\u7684\u4e3b\u8981\u533a\u522b\u3002\u8fd9\u4e9b\u533a\u522b\u5305\u62ec\uff1a1.JSON\u6570\u636e\u89e3\u6790\u548c\u751f\u6210\u7684\u5185\u7f6e\u51fd\u6570\uff1b2.\u9519\u8bef\u5904\u7406\u673a\u5236\uff1b3.\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u89c4\u5219\uff1b4.\u6027\u80fd\u8003\u91cf; 5. ...","protected":false},"author":8,"featured_media":64273,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/64270"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=64270"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/64270\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/64273"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=64270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=64270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=64270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}