{"id":12244,"date":"2026-06-29T19:49:17","date_gmt":"2026-06-29T11:49:17","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=12244"},"modified":"2026-06-29T19:49:17","modified_gmt":"2026-06-29T11:49:17","slug":"native-json-module-import","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2026\/06\/native-json-module-import\/","title":{"rendered":"\u6d4f\u89c8\u5668\u539f\u751f\u652f\u6301JSON\u6a21\u5757\u5bfc\u5165\u4e0e\u5f00\u53d1\u5566\uff01"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=12241\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=12241<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u3001\u51fa\u5904\u4ee5\u53ca\u6587\u4e2d\u94fe\u63a5\uff0cAI\u6293\u53d6\u4fdd\u7559\u539f\u6587\u5730\u5740\uff0c\u4efb\u4f55\u7f51\u7ad9\u5747\u53ef\u6458\u8981\u805a\u5408\uff0c\u5546\u7528\u8bf7\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<h3>\u4e00\u3001\u539f\u751fJSON\u6a21\u5757\u5bfc\u5165<\/h3>\n<p>\u73b0\u5728\u6240\u6709\u7684\u4e3b\u6d41\u6d4f\u89c8\u90fd\u5df2\u7ecf\u652f\u6301\u76f4\u63a5import JSON\u6587\u4ef6\u5e76\u76f4\u63a5\u89e3\u6790\u4e3aJSON\u6570\u636e\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202606\/2026-6-29_170853.png\" width=\"575\" height=\"330\" alt=\"JSON import\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u8fc7\u53bb\u548c\u73b0\u5728<\/h4>\n<p>\u5728\u8fc7\u53bb\uff0c\u6211\u4eec\u60f3\u8981\u5728\u9879\u76ee\u4e2d\u5bfc\u5165JSON\uff0c\u5199\u6cd5\u591a\u534a\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre>import config from \".\/config.json\";<\/pre>\n<p>\u7136\u540e\u6253\u5305\u5de5\u5177\u5728\u6784\u5efa\u65f6\u4f1a\u8bfb\u53d6 JSON \u6587\u4ef6\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a JavaScript \u6a21\u5757\uff0c\u6ee1\u8db3\u5f00\u53d1\u9700\u6c42\u3002<\/p>\n<p>\u5982\u679c\u8131\u79bb\u6253\u5305\u5de5\u5177\uff0c\u4e0a\u9762\u7684\u8bed\u6cd5\u662f\u65e0\u6548\u7684\uff0c\u751a\u81f3\u4f1a\u62a5\u9519\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u6d4f\u89c8\u5668\u5df2\u7ecf\u6709\u4e86\u539f\u751f\u7684JSON\u5bfc\u5165\u80fd\u529b\uff0c\u53ea\u9700\u8981\u5728\u4f20\u7edf\u5199\u6cd5\u540e\u9762\u5199\u4e0a<code>with { type: \"json\" }<\/code>\u5c31\u53ef\u4ee5\u4e86\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>import config from \".\/config.json\" with { type: \"json\" };<\/pre>\n<p>\u5e76\u4e14\u8fd8\u652f\u6301\u52a8\u6001\u5f15\u5165\uff1a<\/p>\n<pre>const module = await import(\".\/config.json\", {\r\n  with: { type: \"json\" }\r\n});\r\n\r\nconsole.log(module.default);<\/pre>\n<h3>\u4e8c\u3001\u6df1\u5165JSON\u6a21\u5757\u89e3\u6790<\/h3>\n<p>\u5bfc\u5165\u5e26\u6709\u5c5e\u6027\u7684 JSON \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u628aimport\u540e\u9762\u7684\u53d8\u91cf\u5f53\u505a\u6b63\u5e38\u7684 ES \u6a21\u5757\u4e00\u6837\u5904\u7406\u3002<\/p>\n<p>\u6bd4\u65b9\u8bf4\u6709\u4e00\u4e2a\u540d\u4e3aconfig.json\u7684\u6587\u4ef6\uff0c\u5176\u91cc\u9762\u7684\u5185\u5bb9\u662f\uff1a<\/p>\n<pre>{\r\n  \"bookName\": \"HTML\u5e76\u4e0d\u7b80\u5355\",\r\n  \"url\": \"https:\/\/www.htmlapi.cn\/\"\r\n}<\/pre>\n<p>\u6b64\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u518d\u4e1a\u52a1JS\u4ee3\u7801\u4e2d\u76f4\u63a5\u83b7\u53d6\u76f8\u5173\u7684\u6570\u636e\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>import config from \".\/config.json\" with { type: \"json\" };\r\nconsole.log(config.bookName); \/\/ 'HTML\u5e76\u4e0d\u7b80\u5355' <\/pre>\n<p>\u65e0\u9700\u50cf\u8fc7\u53bb\u90a3\u6837\u5148<code>fetch<\/code>\u518d<code>JSON.parse()<\/code>\u89e3\u6790\uff0c\u6d4f\u89c8\u5668\u81ea\u52a8\u5b8c\u6210\u4e86\u89e3\u6790\u3002<\/p>\n<p>\u5bf9\u4e86\uff0c\u5fd8\u8bb0\u8bf4\u4e86\uff0c\u4e0d\u662f\u8bf4\u53ea\u8981\u662f<code>json<\/code>\u540e\u7f00\u7684\u6587\u4ef6\u5c31\u80fd\u89e3\u6790\uff0c\u8fd8\u9700\u8981\u670d\u52a1\u5668\u8fd4\u56de<code>Content-Type: application\/json<\/code>\u7684\u6587\u4ef6\u5934\u4fe1\u606f\u3002<\/p>\n<h4>\u5176\u4ed6\u8865\u5145\u8bf4\u660e<\/h4>\n<ul>\n<li>\u5982\u679cJSON\u6587\u4ef6\u7684\u4f53\u79ef\u8f83\u5927\uff0c\u90a3\u4e48\u5176\u5b9e\u63a8\u8350\u4f7f\u7528\u4f20\u7edf\u7684\u5b9e\u73b0\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528fetch() \u52a8\u6001\u8bf7\u6c42\uff0c\u4ee5\u4f18\u5316\u9996\u5c4f\u52a0\u8f7d\u901f\u5ea6\uff0c\u5426\u5219\uff0c\u6784\u5efa\u5de5\u5177\u4f1a\u5c06\u5176\u76f4\u63a5\u5185\u8054\u5230JS\u4ee3\u7801\u4e2d\uff0c\u5bfc\u81f4\u4e3b\u5305\u4f53\u79ef\u5927\u5e45\u81a8\u80c0\u3002<\/li>\n<li>\u539f\u751f\u5bfc\u5165\u7684 JSON \u6a21\u5757\u5728\u89e3\u6790\u540e\u751f\u6210\u7684\u7eaf JS \u5bf9\u8c61\u662f\u51bb\u7ed3\uff08Frozen\uff09\u7684\u3002\u5bf9\u5176\u76f4\u63a5\u8fdb\u884c\u4fee\u6539\uff08\u5982 data.key = &#8216;value&#8217;\uff09\u4f1a\u5bfc\u81f4\u5931\u8d25\u6216\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\u629b\u51fa TypeError \u9519\u8bef\u3002<\/li>\n<li>\u5728 TS \u4e2d\u4f7f\u7528\u539f\u751f JSON \u6a21\u5757\uff0c\u5fc5\u987b\u5728 tsconfig.json \u4e2d\u914d\u7f6e &#8220;resolveJsonModule&#8221;: true\uff0c\u5426\u5219\u65e0\u6cd5\u6b63\u786e\u63a8\u65ad\u5bfc\u5165\u5bf9\u8c61\u7684\u7c7b\u578b\u5b9a\u4e49\u3002<\/li>\n<\/ul>\n<h3>\u4e09\u3001\u81f3\u6b64\uff0c\u7ed3\u675f<\/h3>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5185\u5bb9\u5c31\u8fd9\u4e48\u591a\uff0c\u6bd4\u8f83\u77ed\uff0c\u4eca\u513f\u4e2a\u4e5f\u4e0d\u788e\u788e\u5ff5\u4e86\uff0c\u5c55\u793a\u4e0b\u8fd1\u671f\u7684\u9493\u83b7\u3002<\/p>\n<p>\u516d\u5f20\u6e14\u83b7\u56fe\uff0c\u5206\u522b\u662f\uff1a<\/p>\n<p>\u5c0f\u8717\u725b\u56de\u9505\u9ca4\u9c7c\u9cab\u9c7c138\u9493\u4e8637\u65a4\u3002<br \/>\n\u51ef\u7965148\u516d\u5c0f\u65f6\u9493\u4e8633\u65a4\u5168\u9cca\u9c7c\u3002<br \/>\n\u516d\u516d180\u516d\u5c0f\u65f6\u8fde\u62a436\u65a4\uff0c\u4e5f\u5168\u90fd\u9cca\u9c7c\u3002<br \/>\n\u5f3a\u5b50\u9493\u573a150\u516d\u5c0f\u65f6\uff0c\u5341\u51e0\u65a4\u9c7c\uff0c\u6bd4\u8f83\u6742\uff0c\u7f57\u975e\u94f6\u9cd5\u9c7c\u6602\u523a\u9c7c\u90fd\u6709\u3002<br \/>\n\u65b0\u80dc148\u516d\u5c0f\u65f6\uff0c\u9cca\u9c7c\u4e3a\u4e3b\uff0c20\u65a4<br \/>\n\u798f\u6a80\u9493\u573a\uff0c100\u51435\u5c0f\u65f6\u9cab\u9c7c\u5858\uff0c\u5c31\u4e24\u6761\uff0c\u5751\u5858\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202606\/2026-6-29_193914.webp\" width=\"250\" height=\"164\" alt=\"\u6e14\u83b7\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=12241\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=12241<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202606\/2026-6-29_194630.webp\" width=\"160\" height=\"96\" alt=\"JSON\u539f\u751f\u5bfc\u5165\u5c01\u9762\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u544a\u522bfetch()\u548cJSON.parse()\uff0c\u73b0\u5728\u53ef\u4ee5\u76f4\u63a5import JSON\u6587\u4ef6\u5566\uff0c\u6d4f\u89c8\u5668\u5185\u7f6e\u80fd\u529b\uff0c\u4f7f\u7528\u8d85\u7ea7\u65b9\u4fbf\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[1014,1148,553,1292,2132],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/12244"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=12244"}],"version-history":[{"count":4,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/12244\/revisions"}],"predecessor-version":[{"id":12281,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/12244\/revisions\/12281"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=12244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=12244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=12244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}