{"id":223336,"date":"2021-08-05T08:17:48","date_gmt":"2021-08-05T00:17:48","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=223336"},"modified":"2021-07-26T10:18:24","modified_gmt":"2021-07-26T02:18:24","slug":"learn-json-parse","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/learn-json-parse.html","title":{"rendered":"\u5b66\u4e60JSON.parse()"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nJSON(JavaScript Object Notation, JS \u5bf9\u8c61\u7b80\u8c31) \u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\u3002\u5b83\u57fa\u4e8e ECMAScript (\u6b27\u6d32\u8ba1\u7b97\u673a\u534f\u4f1a\u5236\u5b9a\u7684js\u89c4\u8303)\u7684\u4e00\u4e2a\u5b50\u96c6\uff0c\u91c7\u7528\u5b8c\u5168\u72ec\u7acb\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u6587\u672c\u683c\u5f0f\u6765\u5b58\u50a8\u548c\u8868\u793a\u6570\u636e\u3002\u7b80\u6d01\u548c\u6e05\u6670\u7684\u5c42\u6b21\u7ed3\u6784\u4f7f\u5f97 JSON \u6210\u4e3a\u7406\u60f3\u7684\u6570\u636e\u4ea4\u6362\u8bed\u8a00\u3002 \u6613\u4e8e\u4eba\u9605\u8bfb\u548c\u7f16\u5199\uff0c\u540c\u65f6\u4e5f\u6613\u4e8e\u673a\u5668\u89e3\u6790\u548c\u751f\u6210\uff0c\u5e76\u6709\u6548\u5730\u63d0\u5347\u7f51\u7edc\u4f20\u8f93\u6548\u7387\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\nJSON \u901a\u5e38\u7528\u4e8e\u4e0e\u670d\u52a1\u7aef\u4ea4\u6362\u6570\u636e\u3002<\/p>\n

\u5728\u63a5\u6536\u670d\u52a1\u5668\u6570\u636e\u65f6\u4e00\u822c\u662f\u5b57\u7b26\u4e32\u3002<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 JSON.parse() \u65b9\u6cd5\u5c06\u6570\u636e\u8f6c\u6362\u4e3a JavaScript \u5bf9\u8c61\u3002<\/p>\n

\u8bed\u6cd5<\/strong><\/div>\n
JSON.parse(text[, reviver])<\/pre>\n

\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n

1.text:\u5fc5\u9700\uff0c \u4e00\u4e2a\u6709\u6548\u7684 JSON \u5b57\u7b26\u4e32\u3002<\/p>\n

2.reviver: \u53ef\u9009\uff0c\u4e00\u4e2a\u8f6c\u6362\u7ed3\u679c\u7684\u51fd\u6570\uff0c \u5c06\u4e3a\u5bf9\u8c61\u7684\u6bcf\u4e2a\u6210\u5458\u8c03\u7528\u6b64\u51fd\u6570\u3002<\/p>\n

JSON \u89e3\u6790\u5b9e\u4f8b<\/strong><\/div>\n

\u4f8b\u5982\u6211\u4eec\u4ece\u670d\u52a1\u5668\u63a5\u6536\u4e86\u4ee5\u4e0b\u6570\u636e\uff1a<\/p>\n

{ \"name\":\"runoob\", \"alexa\":10000, \"site\":\"lrxjmw.cn\" }<\/pre>\n

\u6211\u4eec\u4f7f\u7528 JSON.parse() \u65b9\u6cd5\u5904\u7406\u4ee5\u4e0a\u6570\u636e\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a JavaScript \u5bf9\u8c61\uff1a<\/p>\n

var obj = JSON.parse('{ \"name\":\"runoob\", \"alexa\":10000, \"site\":\"www.runoob.com\" }');<\/pre>\n

\u89e3\u6790\u524d\u8981\u786e\u4fdd\u4f60\u7684\u6570\u636e\u662f\u6807\u51c6\u7684 JSON \u683c\u5f0f\uff0c\u5426\u5219\u4f1a\u89e3\u6790\u51fa\u9519\u3002<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\nvar obj = JSON.parse('{ \"name\":\"runoob\", \"alexa\":10000, \"site\":\"lrxjmw.cn\" }');\r\ndocument.getElementById(\"demo\").innerHTML = obj.name + \"\uff1a\" + obj.site;<\/pre>\n
\u4ece\u670d\u52a1\u7aef\u63a5\u6536 JSON \u6570\u636e<\/strong><\/div>\n

\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 AJAX \u4ece\u670d\u52a1\u5668\u8bf7\u6c42 JSON \u6570\u636e\uff0c\u5e76\u89e3\u6790\u4e3a JavaScript \u5bf9\u8c61\u3002<\/p>\n

\u5b9e\u4f8b<\/p>\n

var xmlhttp = new XMLHttpRequest();\r\nxmlhttp.onreadystatechange = function() {\r\n    if (this.readyState == 4 && this.status == 200) {\r\n        myObj = JSON.parse(this.responseText);\r\n        document.getElementById(\"demo\").innerHTML = myObj.name;\r\n    }\r\n};\r\nxmlhttp.open(\"GET\", \"\/try\/ajax\/json_demo.txt\", true);\r\nxmlhttp.send();<\/pre>\n
\u4ece\u670d\u52a1\u7aef\u63a5\u6536\u6570\u7ec4\u7684 JSON \u6570\u636e<\/strong><\/div>\n

\u5982\u679c\u4ece\u670d\u52a1\u7aef\u63a5\u6536\u7684\u662f\u6570\u7ec4\u7684 JSON \u6570\u636e\uff0c\u5219 JSON.parse \u4f1a\u5c06\u5176\u8f6c\u6362\u4e3a JavaScript \u6570\u7ec4\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

var xmlhttp = new XMLHttpRequest();\r\nxmlhttp.onreadystatechange = function() {\r\n    if (this.readyState == 4 && this.status == 200) {\r\n        myArr = JSON.parse(this.responseText);\r\n        document.getElementById(\"demo\").innerHTML = myArr[1];\r\n    }\r\n};\r\nxmlhttp.open(\"GET\", \"\/try\/ajax\/json_demo_array.txt\", true);\r\nxmlhttp.send();<\/pre>\n
\u5f02\u5e38<\/strong><\/div>\n

\u89e3\u6790\u6570\u636e
\nJSON \u4e0d\u80fd\u5b58\u50a8 Date \u5bf9\u8c61\u3002<\/p>\n

\u5982\u679c\u4f60\u9700\u8981\u5b58\u50a8 Date \u5bf9\u8c61\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n

\u4e4b\u540e\u518d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a Date \u5bf9\u8c61\u3002<\/p>\n

\u5b9e\u4f8b<\/p>\n

var text = '{ \"name\":\"Runoob\", \"initDate\":\"2013-12-14\", \"site\":\"www.runoob.com\"}';\r\nvar obj = JSON.parse(text);\r\nobj.initDate = new Date(obj.initDate);\r\n \r\ndocument.getElementById(\"demo\").innerHTML = obj.name + \"\u521b\u5efa\u65e5\u671f: \" + obj.initDate;<\/pre>\n

\u6211\u4eec\u53ef\u4ee5\u542f\u7528 JSON.parse \u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570 reviver\uff0c\u4e00\u4e2a\u8f6c\u6362\u7ed3\u679c\u7684\u51fd\u6570\uff0c\u5bf9\u8c61\u7684\u6bcf\u4e2a\u6210\u5458\u8c03\u7528\u6b64\u51fd\u6570\u3002<\/p>\n

\u5b9e\u4f8b<\/p>\n

var text = '{ \"name\":\"Runoob\", \"initDate\":\"2013-12-14\", \"site\":\"www.runoob.com\"}';\r\nvar obj = JSON.parse(text, function (key, value) {\r\n    if (key == \"initDate\") {\r\n        return new Date(value);\r\n    } else {\r\n        return value;\r\n}});\r\n \r\ndocument.getElementById(\"demo\").innerHTML = obj.name + \"\u521b\u5efa\u65e5\u671f\uff1a\" + obj.initDate;\r\n<\/pre>\n
\u89e3\u6790\u51fd\u6570<\/strong><\/div>\n

JSON \u4e0d\u5141\u8bb8\u5305\u542b\u51fd\u6570\uff0c\u4f46\u4f60\u53ef\u4ee5\u5c06\u51fd\u6570\u4f5c\u4e3a\u5b57\u7b26\u4e32\u5b58\u50a8\uff0c\u4e4b\u540e\u518d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u51fd\u6570\u3002<\/p>\n

\u5b9e\u4f8b<\/p>\n

var text = '{ \"name\":\"Runoob\", \"alexa\":\"function () {return 10000;}\", \"site\":\"www.runoob.com\"}';\r\nvar obj = JSON.parse(text);\r\nobj.alexa =  […]<\/p>\n","protected":false},"author":1481,"featured_media":223326,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-223336","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/223336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=223336"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/223336\/revisions"}],"predecessor-version":[{"id":223342,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/223336\/revisions\/223342"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/223326"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=223336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=223336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=223336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}