{"id":2077,"date":"2025-02-26T23:37:13","date_gmt":"2025-02-26T15:37:13","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2077"},"modified":"2025-02-26T23:37:13","modified_gmt":"2025-02-26T15:37:13","slug":"javascript-json","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/26\/javascript-json\/","title":{"rendered":"JavaScript JSON"},"content":{"rendered":"\n<p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u57fa\u4e8e\u6587\u672c\uff0c\u5e76\u4e14\u6613\u4e8e\u4eba\u7c7b\u8bfb\u53d6\u548c\u7f16\u5199\uff0c\u4e5f\u6613\u4e8e\u673a\u5668\u89e3\u6790\u548c\u751f\u6210\u3002JSON \u662f\u4e0e\u8bed\u8a00\u65e0\u5173\u7684\uff0c\u4f46\u5b83\u7684\u8bed\u6cd5\u7531 JavaScript \u7684\u5bf9\u8c61\u8868\u793a\u6cd5\u884d\u751f\u51fa\u6765\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. JSON \u57fa\u672c\u683c\u5f0f<\/strong><\/h3>\n\n\n\n<p>JSON \u7684\u6570\u636e\u7ed3\u6784\u662f\u952e\u503c\u5bf9\u5f62\u5f0f\u7684\u96c6\u5408\uff0c\u8fd9\u4e0e JavaScript \u4e2d\u7684\u5bf9\u8c61\u975e\u5e38\u76f8\u4f3c\u3002JSON \u7684\u6570\u636e\u662f\u7531 <code>{}<\/code> \u5305\u88f9\u7684\u952e\u503c\u5bf9\u7ec4\u6210\u3002\u952e\uff08Key\uff09\u5fc5\u987b\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u503c\uff08Value\uff09\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5bf9\u8c61\u3001\u6570\u7ec4\u3001\u5e03\u5c14\u503c\u6216 <code>null<\/code>\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"name\": \"Alice\",\n  \"age\": 25,\n  \"isStudent\": false,\n  \"address\": {\n    \"street\": \"123 Main St\",\n    \"city\": \"Wonderland\"\n  },\n  \"phoneNumbers\": &#91;\"123-456-7890\", \"987-654-3210\"]\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. JSON \u6570\u636e\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p>JSON \u652f\u6301\u4ee5\u4e0b\u6570\u636e\u7c7b\u578b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5b57\u7b26\u4e32\uff08String\uff09\uff1a<\/strong> \u7531\u53cc\u5f15\u53f7\u62ec\u8d77\u6765\u7684\u5b57\u7b26\u5e8f\u5217\u3002<\/li>\n\n\n\n<li><strong>\u6570\u5b57\uff08Number\uff09\uff1a<\/strong> \u65e0\u9700\u5f15\u53f7\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u662f\u6574\u6570\u6216\u6d6e\u52a8\u6570\u5b57\u3002<\/li>\n\n\n\n<li><strong>\u5bf9\u8c61\uff08Object\uff09\uff1a<\/strong> \u952e\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u952e\u5fc5\u987b\u662f\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li><strong>\u6570\u7ec4\uff08Array\uff09\uff1a<\/strong> \u6709\u5e8f\u7684\u503c\u5217\u8868\uff0c\u503c\u53ef\u4ee5\u662f\u4efb\u610f\u6570\u636e\u7c7b\u578b\u3002<\/li>\n\n\n\n<li><strong>\u5e03\u5c14\u503c\uff08Boolean\uff09\uff1a<\/strong> <code>true<\/code> \u6216 <code>false<\/code>\u3002<\/li>\n\n\n\n<li><strong><code>null<\/code>\uff1a<\/strong> \u8868\u793a\u7a7a\u503c\u6216\u65e0\u6548\u7684\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. JSON \u4e0e JavaScript \u5bf9\u8c61\u7684\u533a\u522b<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u952e\uff1a<\/strong> JSON \u4e2d\u7684\u952e\u5fc5\u987b\u662f\u53cc\u5f15\u53f7\u62ec\u8d77\u6765\u7684\u5b57\u7b26\u4e32\uff0c\u800c JavaScript \u5bf9\u8c61\u4e2d\u7684\u952e\u53ef\u4ee5\u662f\u672a\u52a0\u5f15\u53f7\u7684\u6807\u8bc6\u7b26\uff08\u5982\u679c\u7b26\u5408\u6807\u8bc6\u7b26\u89c4\u5219\uff09\u3002<\/li>\n\n\n\n<li><strong>\u503c\uff1a<\/strong> JSON \u4e2d\u7684\u503c\u53ea\u80fd\u662f\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5bf9\u8c61\u3001\u6570\u7ec4\u3001\u5e03\u5c14\u503c\u6216 <code>null<\/code>\uff0c\u800c JavaScript \u5bf9\u8c61\u4e2d\u7684\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u6709\u6548\u7684 JavaScript \u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u51fd\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ JSON \u793a\u4f8b\n{\n  \"name\": \"Alice\",\n  \"age\": 25\n}\n\n\/\/ JavaScript \u5bf9\u8c61\u793a\u4f8b\nlet person = {\n  name: \"Alice\", \/\/ \u952e\u4e0d\u9700\u8981\u5f15\u53f7\n  age: 25\n};<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. JavaScript \u4e2d\u7684 JSON \u64cd\u4f5c<\/strong><\/h3>\n\n\n\n<p>JavaScript \u63d0\u4f9b\u4e86\u4e24\u4e2a\u4e3b\u8981\u7684\u5168\u5c40\u65b9\u6cd5\u6765\u64cd\u4f5c JSON \u6570\u636e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>JSON.parse()<\/code><\/strong>\uff1a\u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a JavaScript \u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><strong><code>JSON.stringify()<\/code><\/strong>\uff1a\u5c06 JavaScript \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong><code>JSON.parse()<\/code><\/strong><\/h5>\n\n\n\n<p><code>JSON.parse()<\/code> \u65b9\u6cd5\u5c06\u4e00\u4e2a\u6709\u6548\u7684 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a JavaScript \u5bf9\u8c61\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>const jsonString = '{\"name\": \"Alice\", \"age\": 25}';\nconst person = JSON.parse(jsonString);\nconsole.log(person); \/\/ \u8f93\u51fa { name: \"Alice\", age: 25 }<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong><code>JSON.stringify()<\/code><\/strong><\/h5>\n\n\n\n<p><code>JSON.stringify()<\/code> \u65b9\u6cd5\u5c06\u4e00\u4e2a JavaScript \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>const person = { name: \"Alice\", age: 25 };\nconst jsonString = JSON.stringify(person);\nconsole.log(jsonString); \/\/ \u8f93\u51fa '{\"name\":\"Alice\",\"age\":25}'<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. JSON \u4e0e Ajax<\/strong><\/h3>\n\n\n\n<p>JSON \u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u7ecf\u5e38\u4e0e Ajax\uff08\u5f02\u6b65 JavaScript \u548c XML\uff09\u4e00\u8d77\u4f7f\u7528\u3002\u5728\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u4f20\u8f93\u6570\u636e\u65f6\uff0cJSON \u683c\u5f0f\u901a\u5e38\u88ab\u7528\u6765\u63d0\u9ad8\u6570\u636e\u4f20\u8f93\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5047\u8bbe\u4ece\u670d\u52a1\u5668\u83b7\u53d6 JSON \u6570\u636e\nconst xhr = new XMLHttpRequest();\nxhr.open(\"GET\", \"\/data.json\", true);\nxhr.onreadystatechange = function() {\n  if (xhr.readyState == 4 &amp;&amp; xhr.status == 200) {\n    const data = JSON.parse(xhr.responseText);\n    console.log(data); \/\/ \u8f93\u51fa\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u7684 JSON \u5bf9\u8c61\n  }\n};\nxhr.send();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. JSON \u5e38\u89c1\u95ee\u9898<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>JSON \u683c\u5f0f\u9519\u8bef\uff1a<\/strong> JSON \u5b57\u7b26\u4e32\u5fc5\u987b\u4e25\u683c\u7b26\u5408\u683c\u5f0f\u8981\u6c42\uff0c\u4f8b\u5982\uff0c\u952e\u5fc5\u987b\u7528\u53cc\u5f15\u53f7\u62ec\u8d77\u6765\uff0c\u4e0d\u80fd\u6709\u5c3e\u968f\u7684\u9017\u53f7\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u5faa\u73af\u5f15\u7528\uff1a<\/strong> \u5728\u5c06\u5bf9\u8c61\u8f6c\u4e3a JSON \u5b57\u7b26\u4e32\u65f6\uff0c\u4e0d\u80fd\u5305\u542b\u5faa\u73af\u5f15\u7528\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4 <code>TypeError<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>const obj = { name: \"Alice\" };\nobj.self = obj; \/\/ \u5f15\u5165\u5faa\u73af\u5f15\u7528\nconst jsonString = JSON.stringify(obj); \/\/ \u4f1a\u629b\u51fa TypeError: Converting circular structure to JSON<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>JSON \u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e Web \u5f00\u53d1\u4e2d\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>JSON.parse()<\/code> \u53ef\u4ee5\u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a JavaScript \u5bf9\u8c61\uff0c\u4f7f\u7528 <code>JSON.stringify()<\/code> \u53ef\u4ee5\u5c06 JavaScript \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li>JSON \u6570\u636e\u7531\u952e\u503c\u5bf9\u7ec4\u6210\uff0c\u652f\u6301\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u3001\u5bf9\u8c61\u3001\u6570\u7ec4\u548c <code>null<\/code> \u7b49\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u7406\u89e3 JSON \u7684\u8bed\u6cd5\u548c\u64cd\u4f5c\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6709\u6548\u5730\u5904\u7406\u6570\u636e\u4ea4\u6362\u548c\u5b58\u50a8\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u57fa\u4e8e\u6587\u672c\uff0c\u5e76\u4e14\u6613\u4e8e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2077","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2077","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2077"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2077\/revisions"}],"predecessor-version":[{"id":2078,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2077\/revisions\/2078"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}