{"id":922765,"date":"2024-02-21T02:40:09","date_gmt":"2024-02-21T02:40:09","guid":{"rendered":"https:\/\/proxyelite.info\/?p=922765"},"modified":"2024-02-21T02:40:10","modified_gmt":"2024-02-21T02:40:10","slug":"reading-amp-parsing-json-data-with-python-guide","status":"publish","type":"post","link":"https:\/\/proxyelite.info\/cn\/reading-amp-parsing-json-data-with-python-guide\/","title":{"rendered":"\u4f7f\u7528 Python \u8bfb\u53d6\u548c\u89e3\u6790 JSON \u6570\u636e\uff1a\u6307\u5357"},"content":{"rendered":"<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>\u76ee\u5f55<\/h2><nav><ul><li><a href=\"#brief-explanation-of-json-java-script-object-notation\">JSON\uff08JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff09\u7b80\u8981\u8bf4\u660e<\/a><\/li><li><a href=\"#why-json-is-important-in-python-programming\">\u4e3a\u4ec0\u4e48 JSON \u5728 Python \u7f16\u7a0b\u4e2d\u5f88\u91cd\u8981<\/a><ul><li><a href=\"#1-data-exchange\">1. \u6570\u636e\u4ea4\u6362<\/a><\/li><li><a href=\"#2-api-integration\">2.API\u96c6\u6210<\/a><\/li><li><a href=\"#3-configuration-files\">3. \u914d\u7f6e\u6587\u4ef6<\/a><\/li><li><a href=\"#4-data-storage\">4. \u6570\u636e\u5b58\u50a8<\/a><\/li><li><a href=\"#5-data-transformation\">5. \u6570\u636e\u8f6c\u6362<\/a><\/li><\/ul><\/li><li><a href=\"#overview-of-the-articles-content\">\u6587\u7ae0\u5185\u5bb9\u6982\u8ff0<\/a><\/li><li><a href=\"#basic-knowledge-of-python\">Python\u57fa\u7840\u77e5\u8bc6<\/a><\/li><li><a href=\"#understanding-of-data-structures-and-dictionaries\">\u5bf9\u6570\u636e\u7ed3\u6784\u548c\u5b57\u5178\u7684\u7406\u89e3<\/a><\/li><li><a href=\"#installing-required-libraries\">\u5b89\u88c5\u6240\u9700\u7684\u5e93<\/a><ul><li><a href=\"#using-import-json-to-access-json-functionalities\">\u4f7f\u7528 import json \u8bbf\u95ee JSON \u529f\u80fd<\/a><\/li><li><a href=\"#alternative-libraries-for-json-parsing\">JSON \u89e3\u6790\u7684\u66ff\u4ee3\u5e93<\/a><\/li><\/ul><\/li><li><a href=\"#creating-json-data-manually\">\u624b\u52a8\u521b\u5efa JSON \u6570\u636e<\/a><\/li><li><a href=\"#loading-json-data-from-a-file\">\u4ece\u6587\u4ef6\u52a0\u8f7d JSON \u6570\u636e<\/a><\/li><li><a href=\"#accessing-json-elements\">\u8bbf\u95ee JSON \u5143\u7d20<\/a><ul><li><a href=\"#accessing-json-object-properties\">\u8bbf\u95ee JSON \u5bf9\u8c61\u5c5e\u6027<\/a><\/li><\/ul><\/li><li><a href=\"#accessing-json-arrays-and-elements\">\u8bbf\u95ee JSON \u6570\u7ec4\u548c\u5143\u7d20<\/a><\/li><li><a href=\"#working-with-nested-objects-and-arrays\">\u4f7f\u7528\u5d4c\u5957\u5bf9\u8c61\u548c\u6570\u7ec4<\/a><\/li><li><a href=\"#techniques-for-navigating-nested-json\">\u5d4c\u5957 JSON \u5bfc\u822a\u6280\u672f<\/a><\/li><li><a href=\"#parsing-json-to-python-objects\">\u5c06 JSON \u89e3\u6790\u4e3a Python \u5bf9\u8c61<\/a><ul><li><a href=\"#using-json-loads\">\u4f7f\u7528 json.loads()<\/a><\/li><li><a href=\"#handling-json-exceptions-and-errors\">\u5904\u7406 JSON \u5f02\u5e38\u548c\u9519\u8bef<\/a><\/li><\/ul><\/li><li><a href=\"#parsing-json-files\">\u89e3\u6790 JSON \u6587\u4ef6<\/a><ul><li><a href=\"#python-read-json-file\">Python \u8bfb\u53d6 JSON \u6587\u4ef6<\/a><\/li><\/ul><\/li><li><a href=\"#parsing-json-arrays\">\u89e3\u6790 JSON \u6570\u7ec4<\/a><ul><li><a href=\"#extracting-data-from-json-arrays\">\u4ece JSON \u6570\u7ec4\u4e2d\u63d0\u53d6\u6570\u636e<\/a><\/li><li><a href=\"#iterating-through-json-arrays\">\u904d\u5386 JSON \u6570\u7ec4<\/a><\/li><\/ul><\/li><li><a href=\"#updating-json-properties\">\u66f4\u65b0 JSON \u5c5e\u6027<\/a><ul><li><a href=\"#modifying-values-in-a-json-object\">\u4fee\u6539 JSON \u5bf9\u8c61\u4e2d\u7684\u503c<\/a><\/li><li><a href=\"#adding-new-properties-to-a-json-object\">\u5411 JSON \u5bf9\u8c61\u6dfb\u52a0\u65b0\u5c5e\u6027<\/a><\/li><\/ul><\/li><li><a href=\"#modifying-json-arrays\">\u4fee\u6539 JSON \u6570\u7ec4<\/a><ul><li><a href=\"#adding-updating-and-deleting-elements-in-a-json-array\">\u6dfb\u52a0\u3001\u66f4\u65b0\u548c\u5220\u9664 JSON \u6570\u7ec4\u4e2d\u7684\u5143\u7d20<\/a><\/li><li><a href=\"#reordering-json-array-elements\">\u91cd\u65b0\u6392\u5e8f JSON \u6570\u7ec4\u5143\u7d20<\/a><\/li><\/ul><\/li><li><a href=\"#handling-json-syntax-errors\">\u5904\u7406 JSON \u8bed\u6cd5\u9519\u8bef<\/a><ul><li><a href=\"#common-json-syntax-errors-and-how-to-fix-them\">\u5e38\u89c1\u7684 JSON \u8bed\u6cd5\u9519\u8bef\u4ee5\u53ca\u5982\u4f55\u4fee\u590d\u5b83\u4eec<\/a><\/li><li><a href=\"#using-try-except-for-error-handling\">\u4f7f\u7528 try- except \u8fdb\u884c\u9519\u8bef\u5904\u7406<\/a><\/li><\/ul><\/li><li><a href=\"#best-practices\">\u6700\u4f73\u5b9e\u8df5<\/a><ul><li><a href=\"#tips-for-efficient-json-data-handling-in-python\">Python \u4e2d\u9ad8\u6548 JSON \u6570\u636e\u5904\u7406\u7684\u6280\u5de7<\/a><\/li><li><a href=\"#performance-considerations\">\u6027\u80fd\u8003\u8651\u56e0\u7d20<\/a><\/li><\/ul><\/li><li><a href=\"#working-with-large-json-files\">\u5904\u7406\u5927\u578b JSON \u6587\u4ef6<\/a><ul><li><a href=\"#strategies-for-handling-large-json-files-efficiently\">\u9ad8\u6548\u5904\u7406\u5927\u578b JSON \u6587\u4ef6\u7684\u7b56\u7565<\/a><\/li><\/ul><\/li><li><a href=\"#combining-json-data\">\u7ec4\u5408 JSON \u6570\u636e<\/a><ul><li><a href=\"#merging-multiple-json-objects-or-arrays\">\u5408\u5e76\u591a\u4e2a JSON \u5bf9\u8c61\u6216\u6570\u7ec4<\/a><\/li><li><a href=\"#handling-duplicates-and-conflicts\">\u5904\u7406\u91cd\u590d\u548c\u51b2\u7a81<\/a><\/li><\/ul><\/li><li><a href=\"#real-world-examples-of-using-json-parsing-in-python-projects\">\u5728 Python \u9879\u76ee\u4e2d\u4f7f\u7528 JSON \u89e3\u6790\u7684\u771f\u5b9e\u793a\u4f8b<\/a><ul><li><a href=\"#configuration-files\">\u914d\u7f6e\u6587\u4ef6<\/a><\/li><li><a href=\"#web-ap-is-and-data-exchange\">Web API \u548c\u6570\u636e\u4ea4\u6362<\/a><\/li><li><a href=\"#serialization-and-deserialization\">\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316<\/a><\/li><\/ul><\/li><li><a href=\"#conclusion\">\u7ed3\u8bba<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"brief-explanation-of-json-java-script-object-notation\"><strong>JSON\uff08JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff09\u7b80\u8981\u8bf4\u660e<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JSON\uff08\u5373 JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u5df2\u6210\u4e3a\u73b0\u4ee3 Web \u6570\u636e\u4ea4\u6362\u7684\u57fa\u77f3\u3002\u5b83\u662f\u4e00\u79cd\u57fa\u4e8e\u6587\u672c\u7684\u683c\u5f0f\uff0c\u6613\u4e8e\u4eba\u7c7b\u9605\u8bfb\u548c\u7f16\u5199\uff0c\u4e5f\u6613\u4e8e\u673a\u5668\u89e3\u6790\u548c\u751f\u6210\u3002 JSON \u6570\u636e\u88ab\u6784\u9020\u4e3a\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u4f7f\u5176\u5177\u6709\u9ad8\u5ea6\u901a\u7528\u6027\u5e76\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u5b58\u50a8\u3001\u4f20\u8f93\u548c\u914d\u7f6e\u8bbe\u7f6e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">JSON \u901a\u5e38\u4e0e\u53e6\u4e00\u79cd\u6d41\u884c\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f XML \u8fdb\u884c\u6bd4\u8f83\uff0c\u4f46\u5b83\u7531\u4e8e\u5176\u7b80\u5355\u6027\u548c\u6613\u7528\u6027\u800c\u53d7\u5230\u91cd\u89c6\u3002\u5b83\u53d7\u5230\u5305\u62ec Python \u5728\u5185\u7684\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u7684\u5e7f\u6cdb\u652f\u6301\uff0c\u4f7f\u5176\u6210\u4e3a Python \u7f16\u7a0b\u4e2d\u6570\u636e\u64cd\u4f5c\u548c\u4ea4\u6362\u7684\u91cd\u8981\u5de5\u5177\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"679\" src=\"https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/Qpn7tu3EM4CEVxfQ1j2cBk5H6FL70yZy95ugQv49-2x-1200x679.jpg\" alt=\"\" class=\"wp-image-922767\" title=\"\" srcset=\"https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/Qpn7tu3EM4CEVxfQ1j2cBk5H6FL70yZy95ugQv49-2x-1200x679.jpg 1200w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/Qpn7tu3EM4CEVxfQ1j2cBk5H6FL70yZy95ugQv49-2x-600x339.jpg 600w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/Qpn7tu3EM4CEVxfQ1j2cBk5H6FL70yZy95ugQv49-2x-150x85.jpg 150w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/Qpn7tu3EM4CEVxfQ1j2cBk5H6FL70yZy95ugQv49-2x-768x434.jpg 768w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/Qpn7tu3EM4CEVxfQ1j2cBk5H6FL70yZy95ugQv49-2x-1536x869.jpg 1536w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/Qpn7tu3EM4CEVxfQ1j2cBk5H6FL70yZy95ugQv49-2x.jpg 2048w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/Qpn7tu3EM4CEVxfQ1j2cBk5H6FL70yZy95ugQv49-2x-18x10.jpg 18w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"why-json-is-important-in-python-programming\"><strong>\u4e3a\u4ec0\u4e48 JSON \u5728 Python \u7f16\u7a0b\u4e2d\u5f88\u91cd\u8981<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JSON \u5728 Python \u7f16\u7a0b\u4e2d\u53d1\u6325\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\uff0c\u539f\u56e0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"1-data-exchange\"><strong>1. \u6570\u636e\u4ea4\u6362<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u4e2d JSON \u7684\u4e3b\u8981\u7528\u4f8b\u4e4b\u4e00\u662f\u6570\u636e\u4ea4\u6362\u3002 JSON \u662f\u5728\u670d\u52a1\u5668\u548c Web \u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u6216 Web \u5e94\u7528\u7a0b\u5e8f\u7684\u4e0d\u540c\u90e8\u5206\u4e4b\u95f4\u4f20\u8f93\u6570\u636e\u7684\u9996\u9009\u683c\u5f0f\u3002 Python \u8f7b\u677e\u89e3\u6790\u548c\u751f\u6210 JSON \u7684\u80fd\u529b\u4f7f\u5176\u6210\u4e3a\u5904\u7406\u6570\u636e\u901a\u4fe1\u7684\u7edd\u4f73\u9009\u62e9\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"2-api-integration\"><strong>2.API\u96c6\u6210<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 Web API\uff08\u5e94\u7528\u7a0b\u5e8f\u7f16\u7a0b\u63a5\u53e3\uff09\u65f6\uff0c\u6570\u636e\u901a\u5e38\u4ee5 JSON \u683c\u5f0f\u4ea4\u6362\u3002\u7528\u4e8e\u5904\u7406 JSON \u7684 Python \u5e93\u7b80\u5316\u4e86\u53d1\u51fa API \u8bf7\u6c42\u3001\u89e3\u6790\u54cd\u5e94\u548c\u63d0\u53d6\u6240\u9700\u6570\u636e\u7684\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"3-configuration-files\"><strong>3. \u914d\u7f6e\u6587\u4ef6<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bb8\u591a Python \u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528 JSON \u6587\u4ef6\u6765\u5b58\u50a8\u914d\u7f6e\u8bbe\u7f6e\u3002 JSON \u7684\u4eba\u7c7b\u53ef\u8bfb\u8bed\u6cd5\u4f7f\u624b\u52a8\u6216\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u7f16\u8f91\u914d\u7f6e\u6587\u4ef6\u53d8\u5f97\u5bb9\u6613\u3002 Python \u53ef\u4ee5\u5feb\u901f\u5c06\u8fd9\u4e9b JSON \u914d\u7f6e\u6587\u4ef6\u52a0\u8f7d\u5230\u53ef\u7528\u7684\u6570\u636e\u7ed3\u6784\u4e2d\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"4-data-storage\"><strong>4. \u6570\u636e\u5b58\u50a8<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">JSON \u901a\u5e38\u7528\u4e8e\u5b58\u50a8\u7ed3\u6784\u5316\u6570\u636e\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u8f7b\u91cf\u7ea7\u4e14\u7075\u6d3b\u7684\u683c\u5f0f\u65f6\u3002 Python \u63d0\u4f9b\u4e86\u4e0e JSON \u6570\u636e\u5e93\u548c\u6587\u4ef6\u5b58\u50a8\u4ea4\u4e92\u7684\u5e93\uff0c\u4f7f\u5b58\u50a8\u548c\u68c0\u7d22\u6570\u636e\u53d8\u5f97\u5bb9\u6613\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"5-data-transformation\"><strong>5. \u6570\u636e\u8f6c\u6362<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u7684 JSON \u529f\u80fd\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u8f7b\u677e\u5730\u5c06\u6570\u636e\u4ece\u4e00\u79cd\u683c\u5f0f\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u683c\u5f0f\u3002\u8fd9\u5728\u5904\u7406\u6570\u636e\u9884\u5904\u7406\u548c\u8f6c\u6362\u4efb\u52a1\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"overview-of-the-articles-content\"><strong>\u6587\u7ae0\u5185\u5bb9\u6982\u8ff0<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u672c\u7efc\u5408\u6307\u5357\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u7814\u7a76\u4f7f\u7528 Python \u8fdb\u884c JSON \u6570\u636e\u64cd\u4f5c\u7684\u4e16\u754c\u3002\u65e0\u8bba\u60a8\u662f\u60f3\u8981\u4e86\u89e3\u57fa\u7840\u77e5\u8bc6\u7684\u521d\u5b66\u8005\uff0c\u8fd8\u662f\u5bfb\u6c42\u9ad8\u7ea7\u6280\u672f\u7684\u7ecf\u9a8c\u4e30\u5bcc\u7684 Python \u5f00\u53d1\u4eba\u5458\uff0c\u672c\u6587\u90fd\u9002\u5408\u6bcf\u4e2a\u4eba\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u5c06\u9996\u5148\u8ba8\u8bba\u5148\u51b3\u6761\u4ef6\uff0c\u4ee5\u786e\u4fdd\u60a8\u62e5\u6709\u65c5\u7a0b\u6240\u9700\u7684\u57fa\u7840\u77e5\u8bc6\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5728 Python \u4e2d\u4f7f\u7528 JSON \u7684\u5404\u4e2a\u65b9\u9762\uff0c\u5305\u62ec\u8bfb\u53d6\u3001\u89e3\u6790\u3001\u4fee\u6539\u548c\u5904\u7406\u9519\u8bef\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u5c06\u4ecb\u7ecd\u6700\u4f73\u5b9e\u8df5\u548c\u5148\u8fdb\u6280\u672f\uff0c\u8ba9\u60a8\u5168\u9762\u4e86\u89e3 Python \u4e2d\u7684 JSON \u6570\u636e\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u90a3\u4e48\uff0c\u8ba9\u6211\u4eec\u8e0f\u4e0a\u8fd9\u6bb5\u65c5\u7a0b\uff0c\u638c\u63e1\u4f7f\u7528 Python \u8bfb\u53d6\u548c\u89e3\u6790 JSON \u6570\u636e\u7684\u827a\u672f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"prerequisites\"><strong>\u5148\u51b3\u6761\u4ef6<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"basic-knowledge-of-python\"><strong>Python\u57fa\u7840\u77e5\u8bc6<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6211\u4eec\u6df1\u5165\u7814\u7a76 JSON \u64cd\u4f5c\u4e4b\u524d\uff0c\u6709\u5fc5\u8981\u5bf9 Python \u7f16\u7a0b\u6709\u4e00\u4e2a\u57fa\u672c\u7684\u4e86\u89e3\u3002\u60a8\u5e94\u8be5\u719f\u6089 Python \u7684\u8bed\u6cd5\u3001\u6570\u636e\u7c7b\u578b\u3001\u53d8\u91cf\u4ee5\u53ca\u5faa\u73af\u548c\u6761\u4ef6\u7b49\u57fa\u672c\u63a7\u5236\u7ed3\u6784\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"understanding-of-data-structures-and-dictionaries\"><strong>\u5bf9\u6570\u636e\u7ed3\u6784\u548c\u5b57\u5178\u7684\u7406\u89e3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u6570\u636e\u7ed3\u6784\u7684\u6df1\u5165\u7406\u89e3\u5bf9\u4e8e\u6709\u6548\u4f7f\u7528 JSON \u81f3\u5173\u91cd\u8981\u3002 JSON \u6570\u636e\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u7ec4\u7ec7\uff0c\u5f88\u50cf Python \u5b57\u5178\u3002\u56e0\u6b64\uff0c\u719f\u6089\u5b57\u5178\u4ee5\u53ca\u5982\u4f55\u5728 Python \u4e2d\u8bbf\u95ee\u3001\u4fee\u6539\u548c\u8fed\u4ee3\u5b83\u4eec\u662f\u6709\u76ca\u7684\u3002\u5982\u679c\u60a8\u8fd8\u4e0d\u592a\u719f\u6089\u5b57\u5178\uff0c\u8bf7\u8003\u8651\u5728\u7ee7\u7eed\u8fdb\u884c JSON \u64cd\u4f5c\u4e4b\u524d\u6e29\u4e60\u4e00\u4e0b\u8be5\u4e3b\u9898\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"getting-started-with-parsing-json-in-python\"><strong>\u5f00\u59cb\u4f7f\u7528 Python \u89e3\u6790 JSON<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Python \u7f16\u7a0b\u9886\u57df\uff0c\u4f7f\u7528 JSON\uff08JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff09\u662f\u4e00\u9879\u57fa\u672c\u6280\u80fd\u3002 JSON \u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u5728\u73b0\u4ee3 Web \u5f00\u53d1\u4e2d\u65e0\u5904\u4e0d\u5728\uff0cPython \u4e3a\u89e3\u6790 JSON \u6570\u636e\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u652f\u6301\u3002\u5728\u8fd9\u4efd\u7efc\u5408\u6307\u5357\u4e2d\uff0c\u6211\u4eec\u5c06\u5f15\u5bfc\u60a8\u5b8c\u6210\u5f00\u59cb\u5728 Python \u4e2d\u8bfb\u53d6 JSON \u4ee5\u53ca\u4f7f\u7528 Python \u89e3\u6790 JSON \u7684\u57fa\u672c\u6b65\u9aa4\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"installing-required-libraries\"><strong>\u5b89\u88c5\u6240\u9700\u7684\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6211\u4eec\u6df1\u5165\u4e86\u89e3 Python \u89e3\u6790 JSON \u548c\u4f7f\u7528 Python \u8bfb\u53d6 JSON \u7684\u4e16\u754c\u4e4b\u524d\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86\u5fc5\u8981\u7684\u5e93\u81f3\u5173\u91cd\u8981\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0cPython \u4e2d\u7684\u6807\u51c6\u5e93\u5305\u542b\u5bf9 JSON \u7684\u652f\u6301\uff0c\u4f46\u60a8\u53ef\u80fd\u5e0c\u671b\u63a2\u7d22\u7279\u5b9a\u7528\u4f8b\u7684\u66ff\u4ee3\u5e93\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"using-import-json-to-access-json-functionalities\"><strong>\u4f7f\u7528 import json \u8bbf\u95ee JSON \u529f\u80fd<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u7684\u6807\u51c6\u5e93\u5305\u542b\u4e00\u4e2a\u540d\u4e3a json \u7684\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u5904\u7406 JSON \u6570\u636e\u6240\u9700\u7684\u6240\u6709\u5de5\u5177\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u5c06\u5176\u5bfc\u5165 Python \u811a\u672c\u6765\u8bbf\u95ee\u5176\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u6267\u884c\u6b64\u64cd\u4f5c\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bfc\u5165 json<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u6b64 import \u8bed\u53e5\uff0c\u60a8\u53ef\u4ee5\u8bbf\u95ee\u5c06 Python \u6570\u636e\u7ed3\u6784\u7f16\u7801\uff08\u5e8f\u5217\u5316\uff09\u4e3a JSON \u683c\u5f0f\u4ee5\u53ca\u5c06 JSON \u6570\u636e\u89e3\u7801\uff08\u53cd\u5e8f\u5217\u5316\uff09\u4e3a Python \u6570\u636e\u7ed3\u6784\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"alternative-libraries-for-json-parsing\"><strong>JSON \u89e3\u6790\u7684\u66ff\u4ee3\u5e93<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u867d\u7136 Python \u7684\u5185\u7f6e json \u6a21\u5757\u975e\u5e38\u901a\u7528\u5e76\u4e14\u8db3\u4ee5\u5b8c\u6210\u5927\u591a\u6570\u4efb\u52a1\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u60a8\u53ef\u80fd\u5e0c\u671b\u63a2\u7d22\u4f7f\u7528 Python \u89e3\u6790 JSON \u7684\u66ff\u4ee3\u5e93\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u5bf9\u7279\u5b9a\u7528\u4f8b\u6709\u76ca\u7684\u9644\u52a0\u529f\u80fd\u6216\u4f18\u5316\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u4e9b\u6d41\u884c\u7684\u66ff\u4ee3\u5e93\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>simplejson\uff1a\u4e0e Python \u7684 json \u6a21\u5757\u517c\u5bb9\u7684\u5916\u90e8\u5e93\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u63d0\u4f9b\u6027\u80fd\u6539\u8fdb\u3002<\/li>\n\n\n\n<li>ujson\uff1aPython \u7684\u8d85\u5feb\u901f JSON \u7f16\u7801\u5668\u548c\u89e3\u7801\u5668\uff0c\u4e13\u6ce8\u4e8e\u901f\u5ea6\u548c\u6548\u7387\u3002\u5bf9\u4e8e\u6027\u80fd\u8981\u6c42\u4e25\u683c\u7684\u5e94\u7528\u6765\u8bf4\uff0c\u5b83\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/li>\n\n\n\n<li>rapidjson\uff1aRapidJSON C++ \u5e93\u7684 Python \u5305\u88c5\u5668\uff0c\u4ee5\u5176\u901f\u5ea6\u548c\u4f4e\u5185\u5b58\u5360\u7528\u800c\u95fb\u540d\u3002\u5b83\u662f\u9ad8\u6027\u80fd\u5e94\u7528\u7684\u7edd\u4f73\u9009\u62e9\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u4ecb\u7ecd\u4e86\u8bbe\u7f6e Python \u73af\u5883\u4ee5\u5728 Python \u4e2d\u89e3\u6790 JSON \u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u8ba9\u6211\u4eec\u7ee7\u7eed\u4e0b\u4e00\u6b65\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-and-loading-json\"><strong>\u521b\u5efa\u5e76\u52a0\u8f7d JSON<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"679\" src=\"https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/JLL4ZsnlJi4H0WmBDQeJZipgSSfiDydplieaETNf-2x-1200x679.jpg\" alt=\"\" class=\"wp-image-922768\" title=\"\" srcset=\"https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/JLL4ZsnlJi4H0WmBDQeJZipgSSfiDydplieaETNf-2x-1200x679.jpg 1200w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/JLL4ZsnlJi4H0WmBDQeJZipgSSfiDydplieaETNf-2x-600x340.jpg 600w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/JLL4ZsnlJi4H0WmBDQeJZipgSSfiDydplieaETNf-2x-150x85.jpg 150w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/JLL4ZsnlJi4H0WmBDQeJZipgSSfiDydplieaETNf-2x-768x435.jpg 768w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/JLL4ZsnlJi4H0WmBDQeJZipgSSfiDydplieaETNf-2x-1536x870.jpg 1536w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/JLL4ZsnlJi4H0WmBDQeJZipgSSfiDydplieaETNf-2x.jpg 2048w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/JLL4ZsnlJi4H0WmBDQeJZipgSSfiDydplieaETNf-2x-18x10.jpg 18w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u51c6\u5907\u597d\u5fc5\u8981\u7684\u5e93\u540e\uff0c\u5728 Python \u4e2d\u4f7f\u7528 JSON \u7684\u4e0b\u4e00\u6b65\u662f\u4e86\u89e3\u5982\u4f55\u521b\u5efa JSON \u6570\u636e\u5e76\u5c06\u5176\u52a0\u8f7d\u5230 Python \u811a\u672c\u4e2d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"creating-json-data-manually\"><strong>\u624b\u52a8\u521b\u5efa JSON \u6570\u636e<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Python \u4e2d\u624b\u52a8\u521b\u5efa JSON \u6570\u636e\u975e\u5e38\u7b80\u5355\u3002 JSON \u6570\u636e\u7531\u952e\u503c\u5bf9\u7ec4\u6210\uff0cPython \u5b57\u5178\u662f\u8fd9\u79cd\u7ed3\u6784\u7684\u81ea\u7136\u8868\u793a\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u624b\u52a8\u521b\u5efa\u7b80\u5355 JSON \u5bf9\u8c61\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\"># Create a JSON object manually using a Python dictionary\n\nperson_info = {\n\n\u00a0\"name\": \"John Doe\",\n\n\u00a0\"age\": 30,\n\n\u00a0\"city\": \"New York\"\n\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6b64\u793a\u4f8b\u4e2d\uff0cperson_info \u662f\u4e00\u4e2a\u53cd\u6620 JSON \u5bf9\u8c61\u7ed3\u6784\u7684 Python \u5b57\u5178\u3002\u5b57\u5178\u4e2d\u7684\u6bcf\u4e2a\u952e\u503c\u5bf9\u5bf9\u5e94\u4e8eJSON\u5bf9\u8c61\u4e2d\u7684\u4e00\u4e2a\u952e\u503c\u5bf9\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"loading-json-data-from-a-file\"><strong>\u4ece\u6587\u4ef6\u52a0\u8f7d JSON \u6570\u636e<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u5e38\uff0c\u60a8\u9700\u8981\u4f7f\u7528\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\u7684 JSON \u6570\u636e\u3002 Python \u63d0\u4f9b\u4e86\u4e00\u79cd\u4ece\u6587\u4ef6\u8bfb\u53d6 Python \u4e2d\u7684 JSON \u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u53ef\u7528\u7684 Python \u6570\u636e\u7ed3\u6784\u7684\u7b80\u5355\u65b9\u6cd5\u3002\u60a8\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\"># Load JSON data from a file\n\nwith open('data.json', 'r') as file:\n\n\u00a0data = json.load(file)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6b64\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u6211\u4eec\u4ee5\u8bfb\u53d6\u6a21\u5f0f\uff08\u201cr\u201d\uff09\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a\u201cdata.json\u201d\u7684\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 json.load() \u4f7f\u7528 Python \u5c06 JSON \u4ece\u8be5\u6587\u4ef6\u89e3\u6790\u5230\u6570\u636e\u53d8\u91cf\u4e2d\u3002\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u5728 Python \u811a\u672c\u4e2d\u64cd\u4f5c\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"reading-json-data\"><strong>\u8bfb\u53d6 JSON \u6570\u636e<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6570\u636e\u64cd\u4f5c\u548c\u4ea4\u6362\u9886\u57df\uff0cJSON\uff08JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff09\u5df2\u6210\u4e3a\u6570\u5b57\u4e16\u754c\u7684\u901a\u7528\u8bed\u8a00\u3002\u5176\u8f7b\u91cf\u7ea7\u3001\u4eba\u7c7b\u53ef\u8bfb\u7684\u683c\u5f0f\u4f7f\u5176\u6210\u4e3a\u5728 Python \u4e2d\u8bfb\u53d6 JSON \u548c\u4f20\u8f93\u7ed3\u6784\u5316\u6570\u636e\u7684\u7406\u60f3\u9009\u62e9\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u7814\u7a76\u4f7f\u7528 Python \u89e3\u6790 JSON \u7684\u827a\u672f\u5e76\u63a2\u7d22\u5176\u5de8\u5927\u6f5c\u529b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"accessing-json-elements\"><strong>\u8bbf\u95ee JSON \u5143\u7d20<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JSON \u6570\u636e\u88ab\u6784\u9020\u4e3a\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u4e86\u89e3\u5982\u4f55\u8bbf\u95ee\u8fd9\u4e9b\u5143\u7d20\u662f\u5728 Python \u7a0b\u5e8f\u4e2d\u5229\u7528 JSON \u5f3a\u5927\u529f\u80fd\u7684\u7b2c\u4e00\u6b65\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"accessing-json-object-properties\"><strong>\u8bbf\u95ee JSON \u5bf9\u8c61\u5c5e\u6027<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">JSON \u5bf9\u8c61\u7531\u952e\u503c\u5bf9\u7ec4\u6210\uff0c\u5176\u4e2d\u952e\u662f\u5b57\u7b26\u4e32\uff0c\u503c\u53ef\u4ee5\u662f\u5404\u79cd\u7c7b\u578b\uff0c\u5305\u62ec\u5176\u4ed6\u5bf9\u8c61\u3001\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u6216 null\u3002\u8981\u5728 Python \u4e2d\u8bbf\u95ee JSON \u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u65b9\u62ec\u53f7 ([]) \u6216\u70b9\u7b26\u53f7 (.)\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u4f7f\u7528\u65b9\u62ec\u53f7\u8bbf\u95ee JSON \u5bf9\u8c61\u5c5e\u6027\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\"># Accessing a JSON object property using square brackets\n\njson_data = {\n\n\u00a0\"name\": \"John\",\n\n\u00a0\"age\": 30,\n\n\u00a0\"city\": \"New York\"\n\n}\n\nname = json_data&#91;\"name\"]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u70b9\u7b26\u53f7\u8bbf\u95ee\u76f8\u540c\u7684\u5c5e\u6027\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\"># Accessing a JSON object property using the dot notation\n\nname = json_data.name<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"accessing-json-arrays-and-elements\"><strong>\u8bbf\u95ee JSON \u6570\u7ec4\u548c\u5143\u7d20<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JSON \u6570\u7ec4\u662f\u6709\u5e8f\u7684\u503c\u5217\u8868\uff0c\u6bcf\u4e2a\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u3002\u8981\u5728 Python \u4e2d\u8bbf\u95ee JSON \u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\uff0c\u7c7b\u4f3c\u4e8e\u8bbf\u95ee Python \u5217\u8868\u4e2d\u7684\u5143\u7d20\u7684\u65b9\u5f0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\"># Accessing elements within a JSON array\n\njson_array = &#91;1, 2, 3, 4, 5]\n\nthird_element = json_array&#91;2]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\u60a8\u5df2\u7ecf\u638c\u63e1\u4e86\u8bbf\u95ee JSON \u5143\u7d20\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u8ba9\u6211\u4eec\u63a2\u8ba8\u4e00\u4e0b\u5904\u7406\u5d4c\u5957 JSON \u7ed3\u6784\u7684\u590d\u6742\u6027\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"handling-nested-json\"><strong>\u5904\u7406\u5d4c\u5957 JSON<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">JSON \u7684\u7075\u6d3b\u6027\u5141\u8bb8\u5bf9\u8c61\u548c\u6570\u7ec4\u76f8\u4e92\u5d4c\u5957\uff0c\u4ece\u800c\u521b\u5efa\u5206\u5c42\u4e14\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002\u5728\u5904\u7406\u73b0\u5b9e\u4e16\u754c\u7684\u6570\u636e\u65f6\uff0c\u6709\u6548\u5730\u4f7f\u7528\u5d4c\u5957 JSON \u662f\u4e00\u9879\u5b9d\u8d35\u7684\u6280\u80fd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"working-with-nested-objects-and-arrays\"><strong>\u4f7f\u7528\u5d4c\u5957\u5bf9\u8c61\u548c\u6570\u7ec4<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8bb8\u591a JSON \u6570\u636e\u96c6\u4e2d\uff0c\u60a8\u4f1a\u9047\u5230\u5d4c\u5957\u5bf9\u8c61\u6216\u6570\u7ec4\u3002\u8fd9\u4e9b\u5d4c\u5957\u7ed3\u6784\u53ef\u4ee5\u5305\u542b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\uff0c\u4f46\u8bbf\u95ee\u5b83\u4eec\u7684\u5143\u7d20\u9700\u8981\u66f4\u6df1\u5165\u5730\u4e86\u89e3 JSON \u904d\u5386\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8003\u8651\u4e00\u4e2a\u5e26\u6709\u5d4c\u5957\u5bf9\u8c61\u7684 JSON \u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">{\n\n\u00a0\"person\": {\n\n\u00a0\"name\": \"Alice\",\n\n\u00a0\"age\": 25,\n\n\u00a0\"address\": {\n\n\u00a0\"city\": \"Boston\",\n\n\u00a0\"zipcode\": \"02101\"\n\n\u00a0}\n\n\u00a0}\n\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u8bbf\u95ee\u5d4c\u5957\u5c5e\u6027\uff0c\u60a8\u53ef\u4ee5\u94fe\u63a5\u65b9\u62ec\u53f7\u6216\u70b9\u7b26\u53f7\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">person_name = json_data&#91;\"person\"]&#91;\"name\"]\n\nzipcode = json_data.person.address&#91;\"zipcode\"]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"techniques-for-navigating-nested-json\"><strong>\u5d4c\u5957 JSON \u5bfc\u822a\u6280\u672f<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u867d\u7136\u5d4c\u5957 JSON \u7ed3\u6784\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f3a\u5927\u7684\u6570\u636e\u7ec4\u7ec7\u65b9\u5f0f\uff0c\u4f46\u5bfc\u822a\u5b83\u4eec\u53ef\u80fd\u4f1a\u53d8\u5f97\u590d\u6742\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u7b80\u5316 JSON \u904d\u5386\u7684\u6280\u672f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u5faa\u73af\uff1a\u4f7f\u7528 for \u5faa\u73af\u6765\u8fed\u4ee3 JSON \u6570\u636e\u4e2d\u7684\u6570\u7ec4\u6216\u641c\u7d22\u5d4c\u5957\u5bf9\u8c61\u4e2d\u7684\u7279\u5b9a\u503c\u3002<\/li>\n\n\n\n<li>\u9012\u5f52\u51fd\u6570\uff1a\u5b9e\u73b0\u9012\u5f52\u51fd\u6570\u4ee5\u6df1\u5165\u5d4c\u5957\u7ed3\u6784\u3002\u5f53\u5d4c\u5957\u7ea7\u522b\u672a\u77e5\u65f6\uff0c\u6b64\u6280\u672f\u7279\u522b\u6709\u7528\u3002<\/li>\n\n\n\n<li>JSONPath\uff1aJSONPath \u662f\u4e00\u79cd JSON \u67e5\u8be2\u8bed\u8a00\uff0c\u5141\u8bb8\u60a8\u4f7f\u7528\u7b80\u6d01\u7684\u8bed\u6cd5\u4ece\u590d\u6742\u7684 JSON \u6587\u6863\u4e2d\u63d0\u53d6\u6570\u636e\u3002 jsonpath-ng \u7b49 Python \u5e93\u53ef\u4ee5\u5e2e\u52a9\u60a8\u4f7f\u7528 JSONPath \u8868\u8fbe\u5f0f\u3002<\/li>\n\n\n\n<li>\u7b2c\u4e09\u65b9\u5e93\uff1a\u8003\u8651\u4f7f\u7528 pandas \u7b49\u7b2c\u4e09\u65b9\u5e93\u6765\u6267\u884c\u66f4\u9ad8\u7ea7\u7684\u6570\u636e\u64cd\u4f5c\u4efb\u52a1\u3002 pandas \u4e3a\u6709\u6548\u5904\u7406\u5d4c\u5957 JSON \u6570\u636e\u63d0\u4f9b\u4e86\u5e7f\u6cdb\u7684\u652f\u6301\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"parsing-json-data\"><strong>\u89e3\u6790 JSON \u6570\u636e<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6570\u636e\u64cd\u4f5c\u548c\u4ea4\u6362\u7684\u5e7f\u9614\u9886\u57df\u4e2d\uff0cJSON\uff08JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff09\u6beb\u65e0\u7591\u95ee\u5df2\u6210\u4e3a\u4e00\u79cd\u65e0\u5904\u4e0d\u5728\u7684\u683c\u5f0f\u3002\u5b83\u5e7f\u6cdb\u7528\u4e8e\u5b58\u50a8\u548c\u4f20\u8f93\u7ed3\u6784\u5316\u6570\u636e\uff0c\u800cPython\u4f5c\u4e3a\u4e00\u79cd\u901a\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u4e0d\u4ec5\u63d0\u4f9b\u539f\u751f\u652f\u6301\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e00\u5957\u7528\u4e8e\u89e3\u6790JSON\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5728\u8fd9\u7bc7\u7efc\u5408\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u7814\u7a76\u4f7f\u7528 Python \u89e3\u6790 JSON \u7684\u590d\u6742\u7ec6\u5fae\u5dee\u522b\uff0c\u63ed\u793a\u4ee5\u6700\u9ad8\u6548\u7387\u5c06 JSON \u65e0\u7f1d\u8f6c\u6362\u4e3a\u672c\u673a Python \u5bf9\u8c61\u7684\u6280\u672f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"parsing-json-to-python-objects\"><strong>\u5c06 JSON \u89e3\u6790\u4e3a Python \u5bf9\u8c61<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Python \u4e2d\u89e3\u6790 JSON \u6570\u636e\u6d89\u53ca\u5c06 JSON \u683c\u5f0f\u7684\u6587\u672c\u8f6c\u6362\u4e3a\u539f\u751f Python \u6570\u636e\u7ed3\u6784\u3002\u6b64\u8fc7\u7a0b\u6709\u52a9\u4e8e\u5c06 JSON \u6570\u636e\u65e0\u7f1d\u96c6\u6210\u5230 Python \u7a0b\u5e8f\u4e2d\uff0c\u4ece\u800c\u8f7b\u677e\u64cd\u4f5c\u548c\u5229\u7528\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"using-json-loads\"><strong>\u4f7f\u7528 json.loads()<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u4e30\u5bcc\u7684\u6807\u51c6\u5e93\u4e2d\u6709\u4e0d\u53ef\u6216\u7f3a\u7684 json \u6a21\u5757\uff0c\u5b83\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u5b9d\u8d35\u7684 json.loads() \u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u662f\u89e3\u6790 JSON \u6570\u636e\u7684\u57fa\u77f3\u3002 json.loads() \u63a5\u6536 JSON \u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u4f5c\u4e3a\u8f93\u5165\uff0c\u5e76\u5c06\u5176\u4f18\u96c5\u5730\u8f6c\u6362\u4e3a\u672c\u673a Python \u5bf9\u8c61\u3002\u751f\u6210\u7684 Python \u5bf9\u8c61\u901a\u5e38\u8868\u73b0\u4e3a\u5b57\u5178\u6216\u5217\u8868\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u6240\u8003\u8651\u7684 JSON \u7ed3\u6784\u7684\u590d\u6742\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u662f\u4e00\u4e2a\u751f\u52a8\u7684\u4f8b\u5b50\uff0c\u9610\u660e\u4e86 json.loads() \u5728\u7528 Python \u89e3\u6790 JSON \u65f6\u7684\u5b9e\u7528\u6027\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">import json\n\njson_string = '{\"name\": \"John\", \"age\": 30, \"city\": \"New York\"}'\n\nparsed_data = json.loads(json_string)\n\nprint(parsed_data&#91;\"name\"]) # Accessing a parsed JSON object property<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"handling-json-exceptions-and-errors\"><strong>\u5904\u7406 JSON \u5f02\u5e38\u548c\u9519\u8bef<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4f7f\u7528 json.loads() \u89e3\u6790 JSON \u6570\u636e\u65f6\uff0c\u4e3a\u4ee3\u7801\u6ce8\u5165\u5f3a\u5927\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u81f3\u5173\u91cd\u8981\u3002 JSON \u89e3\u6790\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u7cfb\u5217\u9519\u8bef\uff0c\u5305\u62ec\u683c\u5f0f\u9519\u8bef\u7684 JSON\u3001\u610f\u5916\u7684\u6570\u636e\u7c7b\u578b\u6216\u7f3a\u5c11\u952e\u3002\u4e3a\u4e86\u719f\u7ec3\u5730\u9a7e\u9a6d\u8fd9\u4e9b\u5371\u9669\u7684\u6c34\u57df\uff0c\u5f3a\u70c8\u5efa\u8bae\u660e\u667a\u5730\u4f7f\u7528 try- except \u5757\u3002\u901a\u8fc7\u8fd9\u6837\u505a\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u6355\u83b7\u5e76\u59a5\u5584\u5904\u7406\u8fd9\u4e9b\u6f5c\u5728\u7684\u7834\u574f\u6027\u95ee\u9898\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">import json\n\njson_string = '{\"name\": \"John\", \"age\": 30, \"city\": \"New York\"}'\n\ntry:\n\n\u00a0parsed_data = json.loads(json_string)\n\nexcept json.JSONDecodeError as e:\n\n\u00a0print(f\"Error parsing JSON: {e}\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u6548\u7ba1\u7406\u5f02\u5e38\u53ef\u786e\u4fdd\u60a8\u7684 Python \u7a0b\u5e8f\u4fdd\u6301\u5f39\u6027\u548c\u5065\u58ee\uff0c\u5373\u4f7f\u5728\u5904\u7406\u4e0d\u5b8c\u7f8e\u7684 JSON \u6570\u636e\u65f6\u4e5f\u662f\u5982\u6b64\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"parsing-json-files\"><strong>\u89e3\u6790 JSON \u6587\u4ef6<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u4e86\u4ece\u5b57\u7b26\u4e32\u89e3\u6790 JSON \u6570\u636e\u4e4b\u5916\uff0cPython \u8fd8\u63d0\u4f9b\u4e86\u4e00\u79cd\u4ece\u6587\u4ef6\u89e3\u6790 JSON \u7684\u65e0\u7f1d\u65b9\u6cd5\u3002\u5f53\u60a8\u9700\u8981\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u6216\u60f3\u8981\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6 JSON \u6570\u636e\u65f6\uff0c\u8fd9\u5c24\u5176\u6709\u7528\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"python-read-json-file\"><strong>Python \u8bfb\u53d6 JSON \u6587\u4ef6<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u5728Python\u4e2d\u8bfb\u53d6JSON\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528json\u6a21\u5757\u63d0\u4f9b\u7684json.load()\u65b9\u6cd5\u3002\u6b64\u65b9\u6cd5\u5141\u8bb8\u60a8\u4ece\u6587\u4ef6\u4e2d\u89e3\u6790 JSON \u5e76\u5c06\u5176\u76f4\u63a5\u52a0\u8f7d\u5230 Python \u5bf9\u8c61\u4e2d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u5982\u4f55\u4ece\u6587\u4ef6\u52a0\u8f7d JSON \u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">import json\n\n# Open the JSON file for reading\n\nwith open('data.json', 'r') as file:\n\n\u00a0parsed_data = json.load(file)\n\n# Now, parsed_data contains the JSON data from the file<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u4f7f\u7528\u6b64\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 Python \u9ad8\u6548\u5730\u4ece\u6587\u4ef6\u4e2d\u89e3\u6790 JSON\uff0c\u5e76\u5c06\u5176\u5185\u5bb9\u4f5c\u4e3a Python \u5bf9\u8c61\u8fdb\u884c\u8bbf\u95ee\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"parsing-json-arrays\"><strong>\u89e3\u6790 JSON \u6570\u7ec4<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JSON \u6570\u7ec4\u662f JSON \u6570\u636e\u7ed3\u6784\u7684\u4e3b\u8981\u7ec4\u6210\u90e8\u5206\uff0c\u662f\u503c\u7684\u6709\u5e8f\u805a\u5408\u3002\u5b83\u4eec\u5728 JSON \u6570\u636e\u4e2d\u7ecf\u5e38\u9047\u5230\uff0c\u9700\u8981\u4e00\u79cd\u72ec\u7279\u7684\u89e3\u6790\u65b9\u6cd5\u3002\u5728 Python \u4e2d\u89e3\u6790 JSON \u6570\u7ec4\u9700\u8981\u4ece\u8fd9\u4e9b\u6570\u7ec4\u4e2d\u63d0\u53d6\u6570\u636e\uff0c\u5e76\u5728\u5fc5\u8981\u65f6\u7cfb\u7edf\u5730\u904d\u5386\u5176\u5185\u5bb9\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"extracting-data-from-json-arrays\"><strong>\u4ece JSON \u6570\u7ec4\u4e2d\u63d0\u53d6\u6570\u636e<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u4ece JSON \u6570\u7ec4\u4e2d\u5254\u9664\u6570\u636e\uff0c\u60a8\u53ef\u4ee5\u4f9d\u9760\u7ecf\u8fc7\u9a8c\u8bc1\u7684\u7d22\u5f15\u5b9e\u8df5\u3002\u7d22\u5f15\u65b9\u6cd5\u53cd\u6620\u4e86 Python \u5217\u8868\u7684\u65b9\u6cd5\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u76f4\u89c2\u7684\u65b9\u6cd5\u6765\u8bbf\u95ee JSON \u6570\u7ec4\u4e2d\u7684\u5404\u4e2a\u5143\u7d20\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">import json\n\njson_array = '&#91;1, 2, 3, 4, 5]'\n\nparsed_array = json.loads(json_array)\n\nthird_element = parsed_array&#91;2]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"iterating-through-json-arrays\"><strong>\u904d\u5386 JSON \u6570\u7ec4<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5904\u7406 JSON \u6570\u636e\u65f6\uff0c\u8fed\u4ee3 JSON \u6570\u7ec4\u662f\u4e00\u9879\u5e38\u89c1\u4efb\u52a1\u3002\u4f7f\u7528\u5faa\u73af\uff0c\u60a8\u53ef\u4ee5\u65e0\u7f1d\u904d\u5386\u548c\u64cd\u4f5c JSON \u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">import json\n\njson_array = '&#91;{\"name\": \"Alice\"}, {\"name\": \"Bob\"}, {\"name\": \"Charlie\"}]'\n\nparsed_array = json.loads(json_array)\n\nfor item in parsed_array:\n\n\u00a0print(item&#91;\"name\"])<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fed\u4ee3\u7684\u827a\u672f\u4f7f\u60a8\u80fd\u591f\u5355\u72ec\u8bbf\u95ee\u548c\u5904\u7406 JSON \u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4ece\u800c\u4fc3\u8fdb\u6570\u636e\u7684\u9ad8\u6548\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"modifying-json-data\"><strong>\u4fee\u6539 JSON \u6570\u636e<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">JSON\uff08JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u50a8\u548c\u4ea4\u6362\u7ed3\u6784\u5316\u6570\u636e\u7684\u901a\u7528\u683c\u5f0f\u3002\u5728 Python \u4e2d\u5904\u7406 JSON \u6570\u636e\u65f6\uff0c\u4e0d\u4ec5\u4ec5\u662f\u89e3\u6790\u548c\u8bfb\u53d6\uff1b\u8fd8\u5305\u62ec\u89e3\u6790\u548c\u8bfb\u53d6\u3002\u5b83\u8fd8\u6d89\u53ca\u4fee\u6539 JSON \u6570\u636e\u4ee5\u6ee1\u8db3\u60a8\u7684\u7279\u5b9a\u9700\u6c42\u3002\u5728\u8fd9\u7bc7\u7efc\u5408\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u4f7f\u7528 Python \u4fee\u6539 JSON \u6570\u636e\u7684\u827a\u672f\uff0c\u5305\u62ec\u66f4\u65b0 JSON \u5bf9\u8c61\u4e2d\u7684\u5c5e\u6027\u4ee5\u53ca\u5bf9 JSON \u6570\u7ec4\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"updating-json-properties\"><strong>\u66f4\u65b0 JSON \u5c5e\u6027<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Python \u4e2d\u89e3\u6790 JSON \u65f6\u7684\u4e3b\u8981\u4efb\u52a1\u4e4b\u4e00\u662f\u66f4\u65b0 JSON \u5bf9\u8c61\u4e2d\u7684\u5c5e\u6027\u3002\u8fd9\u53ef\u80fd\u6d89\u53ca\u66f4\u6539\u5bf9\u8c61\u5185\u7684\u503c\u6216\u6dfb\u52a0\u5168\u65b0\u7684\u5c5e\u6027\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"modifying-values-in-a-json-object\"><strong>\u4fee\u6539 JSON \u5bf9\u8c61\u4e2d\u7684\u503c<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u4fee\u6539 Python \u4e2d JSON \u5bf9\u8c61\u4e2d\u7684\u503c\uff0c\u60a8\u53ea\u9700\u8bbf\u95ee\u8be5\u5c5e\u6027\u5e76\u4e3a\u5176\u5206\u914d\u65b0\u503c\u5373\u53ef\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b JSON \u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">{\n\n\u00a0\"name\": \"Alice\",\n\n\u00a0\"age\": 25,\n\n\u00a0\"city\": \"Boston\"\n\n}&lt;\/code>\n\nIf we want to update Alice's age, we can do the following:\n\n&lt;code>json_data = {\n\n\u00a0\"name\": \"Alice\",\n\n\u00a0\"age\": 25,\n\n\u00a0\"city\": \"Boston\"\n\n}\n\njson_data&#91;\"age\"] = 26 # Updating Alice's age<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"adding-new-properties-to-a-json-object\"><strong>\u5411 JSON \u5bf9\u8c61\u6dfb\u52a0\u65b0\u5c5e\u6027<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Python \u4e2d\u5411 JSON \u5bf9\u8c61\u6dfb\u52a0\u65b0\u5c5e\u6027\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ef\u4ee5\u7b80\u5355\u5730\u4e3a\u5c1a\u4e0d\u5b58\u5728\u7684\u65b0\u952e\u5206\u914d\u4e00\u4e2a\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">json_data = {\n\n\u00a0\"name\": \"Alice\",\n\n\u00a0\"age\": 25,\n\n\u00a0\"city\": \"Boston\"\n\n}\n\njson_data&#91;\"country\"] = \"USA\" # Adding a new property 'country'<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\uff0cJSON \u5bf9\u8c61\u5305\u542b\u201ccountry\u201d\u5c5e\u6027\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"modifying-json-arrays\"><strong>\u4fee\u6539 JSON \u6570\u7ec4<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JSON \u6570\u7ec4\u63d0\u4f9b\u4e86\u4e00\u79cd\u5b58\u50a8\u6709\u5e8f\u503c\u5217\u8868\u7684\u65b9\u6cd5\u3002\u4fee\u6539 JSON \u6570\u7ec4\u53ef\u80fd\u6d89\u53ca\u6dfb\u52a0\u3001\u66f4\u65b0\u6216\u5220\u9664\u5143\u7d20\uff0c\u4ee5\u53ca\u5bf9\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u91cd\u65b0\u6392\u5e8f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"adding-updating-and-deleting-elements-in-a-json-array\"><strong>\u6dfb\u52a0\u3001\u66f4\u65b0\u548c\u5220\u9664 JSON \u6570\u7ec4\u4e2d\u7684\u5143\u7d20<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u5728 Python \u4e2d\u6dfb\u52a0\u3001\u66f4\u65b0\u6216\u5220\u9664 JSON \u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u548c\u8d4b\u503c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8ba9\u6211\u4eec\u8003\u8651\u4e00\u4e2a JSON \u6570\u5b57\u6570\u7ec4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">&#91;1, 2, 3, 4, 5]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u6211\u4eec\u60f3\u8981\u6dfb\u52a0\u4e00\u4e2a\u65b0\u53f7\u7801\u3001\u66f4\u65b0\u4e00\u4e2a\u73b0\u6709\u53f7\u7801\u4ee5\u53ca\u5220\u9664\u53e6\u4e00\u4e2a\u53f7\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">json_array = &#91;1, 2, 3, 4, 5]\n\n# Adding a new number (6) at the end of the array\n\njson_array.append(6)\n\n# Updating the second number (index 1) to 10\n\njson_array&#91;1] = 10\n\n# Deleting the third number (index 2)\n\ndel json_array&#91;2]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"reordering-json-array-elements\"><strong>\u91cd\u65b0\u6392\u5e8f JSON \u6570\u7ec4\u5143\u7d20<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u4f7f\u7528 Python \u7684\u5217\u8868\u64cd\u4f5c\u65b9\u6cd5\u6765\u5b9e\u73b0\u5bf9 JSON \u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u91cd\u65b0\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u53cd\u8f6c\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528reverse()\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">json_array = &#91;1, 2, 3, 4, 5]\n\njson_array.reverse() # Reversing the order of elements<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6216\u8005\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 sort() \u65b9\u6cd5\u6309\u5347\u5e8f\u6216\u964d\u5e8f\u5bf9\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">json_array = &#91;3, 1, 5, 2, 4]\n\njson_array.sort() # Sorting in ascending order<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528 sort() \u65b9\u6cd5\u7684 key \u53c2\u6570\u81ea\u5b9a\u4e49\u6392\u5e8f\u6807\u51c6\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"error-handling-in-json-data-handling\"><strong>JSON \u6570\u636e\u5904\u7406\u4e2d\u7684\u9519\u8bef\u5904\u7406<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Python \u4e2d\u5904\u7406 JSON\uff08JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff09\u6570\u636e\u65f6\uff0c\u7cbe\u901a\u9519\u8bef\u5904\u7406\u81f3\u5173\u91cd\u8981\u3002 JSON \u6570\u636e\u53ef\u80fd\u5f88\u590d\u6742\uff0c\u5e76\u4e14\u5728\u89e3\u6790\u3001\u64cd\u4f5c\u6216\u5904\u7406\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u53d1\u751f\u9519\u8bef\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8 JSON \u6570\u636e\u5904\u7406\u4e2d\u9519\u8bef\u5904\u7406\u7684\u5404\u4e2a\u65b9\u9762\uff0c\u5305\u62ec\u5982\u4f55\u5904\u7406 JSON \u8bed\u6cd5\u9519\u8bef\u4ee5\u53ca\u786e\u4fdd Python \u4e2d\u9ad8\u6548\u4e14\u7a33\u5065\u7684 JSON \u6570\u636e\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"handling-json-syntax-errors\"><strong>\u5904\u7406 JSON \u8bed\u6cd5\u9519\u8bef<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JSON \u8bed\u6cd5\u9519\u8bef\u662f\u4f7f\u7528 JSON \u6570\u636e\u65f6\u9047\u5230\u7684\u6700\u5e38\u89c1\u95ee\u9898\u4e4b\u4e00\u3002\u8fd9\u4e9b\u9519\u8bef\u53ef\u80fd\u4f1a\u4ee4\u4eba\u6cae\u4e27\uff0c\u4f46\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u6b63\u786e\u7684\u65b9\u6cd5\u8f7b\u677e\u4fee\u590d\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"common-json-syntax-errors-and-how-to-fix-them\"><strong>\u5e38\u89c1\u7684 JSON \u8bed\u6cd5\u9519\u8bef\u4ee5\u53ca\u5982\u4f55\u4fee\u590d\u5b83\u4eec<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u62ec\u53f7\u7f3a\u5931\u6216\u4e0d\u5339\u914d\uff1aJSON \u5bf9\u8c61\u548c\u6570\u7ec4\u5fc5\u987b\u5177\u6709\u5339\u914d\u7684\u5de6\u62ec\u53f7\u548c\u53f3\u62ec\u53f7\u3002\u4e00\u4e2a\u5e38\u89c1\u7684\u9519\u8bef\u662f\u5fd8\u8bb0\u6b63\u786e\u5173\u95ed\u5bf9\u8c61\u6216\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">\/\/ Incorrect JSON\n\n{\n\n\u00a0\"name\": \"Alice\",\n\n\u00a0\"age\": 30<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8981\u4fee\u590d\u6b64\u9519\u8bef\uff0c\u8bf7\u6dfb\u52a0\u53f3\u5927\u62ec\u53f7 (})\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">\/\/ Corrected JSON\n\n{\n\n\u00a0\"name\": \"Alice\",\n\n\u00a0\"age\": 30\n\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7f3a\u5c11\u9017\u53f7\uff1a\u5bf9\u8c61\u6216\u6570\u7ec4\u4e2d\u7684 JSON \u5143\u7d20\u5e94\u4ee5\u9017\u53f7\u5206\u9694\u3002\u7701\u7565\u5143\u7d20\u4e4b\u95f4\u7684\u9017\u53f7\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8bed\u6cd5\u9519\u8bef\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">\/\/ Incorrect JSON\n\n{\n\n\u00a0\"name\": \"Alice\"\n\n\u00a0\"age\": 30<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8981\u89e3\u51b3\u6b64\u9519\u8bef\uff0c\u8bf7\u5728\u5143\u7d20\u4e4b\u95f4\u6dfb\u52a0\u9017\u53f7\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">\/\/ Corrected JSON\n\n{\n\n\u00a0\"name\": \"Alice\",\n\n\u00a0\"age\": 30\n\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b57\u7b26\u4e32\u5f15\u53f7\uff1aJSON \u8981\u6c42\u5c06\u5c5e\u6027\u540d\u79f0\u548c\u5b57\u7b26\u4e32\u503c\u62ec\u5728\u53cc\u5f15\u53f7\u4e2d\u3002\u5355\u5f15\u53f7\u5728 JSON \u4e2d\u65e0\u6548\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">\/\/ Incorrect JSON\n\n{\n\n\u00a0'name': 'Alice',\n\n\u00a0'age': 30<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8981\u4fee\u590d\u6b64\u9519\u8bef\uff0c\u8bf7\u4f7f\u7528\u53cc\u5f15\u53f7\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">\/\/ Corrected JSON\n\n{\n\n\u00a0\"name\": \"Alice\",\n\n\u00a0\"age\": 30\n\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"using-try-except-for-error-handling\"><strong>\u4f7f\u7528 try- except \u8fdb\u884c\u9519\u8bef\u5904\u7406<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u867d\u7136\u4fee\u590d\u8bed\u6cd5\u9519\u8bef\u5f88\u91cd\u8981\uff0c\u4f46\u5728 Python \u4ee3\u7801\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\u4e5f\u540c\u6837\u91cd\u8981\u3002\u5982\u679c\u8f93\u5165\u6570\u636e\u4e0d\u662f\u683c\u5f0f\u826f\u597d\u7684 JSON\uff0c\u5219 JSON \u89e3\u6790\u53ef\u80fd\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002\u4e3a\u4e86\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\uff0c\u53ef\u4ee5\u4f7f\u7528 try-except \u5757\u6765\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">import json\n\njson_string = '{\"name\": \"Alice\", \"age\": 30'\n\ntry:\n\n\u00a0parsed_data = json.loads(json_string)\n\nexcept json.JSONDecodeError as e:\n\n\u00a0print(f\"Error parsing JSON: {e}\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u4f7f\u7528 try-except\uff0c\u60a8\u53ef\u4ee5\u6355\u83b7 JSON \u8bed\u6cd5\u9519\u8bef\u5e76\u9002\u5f53\u5730\u54cd\u5e94\u5b83\u4eec\uff0c\u786e\u4fdd\u60a8\u7684 Python \u5e94\u7528\u7a0b\u5e8f\u4fdd\u6301\u5065\u58ee\u4e14\u7528\u6237\u53cb\u597d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"best-practices\"><strong>\u6700\u4f73\u5b9e\u8df5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u4ecb\u7ecd\u4e86\u9519\u8bef\u5904\u7406\uff0c\u8ba9\u6211\u4eec\u63a2\u7d22\u4e00\u4e9b\u5728 Python \u4e2d\u9ad8\u6548\u5904\u7406 JSON \u6570\u636e\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"tips-for-efficient-json-data-handling-in-python\"><strong>Python \u4e2d\u9ad8\u6548 JSON \u6570\u636e\u5904\u7406\u7684\u6280\u5de7<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 json \u6a21\u5757\uff1aPython \u5185\u7f6e\u7684 json \u6a21\u5757\u63d0\u4f9b\u4e86\u9ad8\u6548\u53ef\u9760\u7684 JSON \u89e3\u6790\u548c\u64cd\u4f5c\u5de5\u5177\u3002\u5b83\u662f\u5728 Python \u4e2d\u5904\u7406 JSON \u6570\u636e\u7684\u9996\u9009\u3002<\/li>\n\n\n\n<li>\u9a8c\u8bc1 JSON\uff1a\u5728\u89e3\u6790 JSON \u6570\u636e\u4e4b\u524d\uff0c\u8bf7\u8003\u8651\u4f7f\u7528 JSON \u9a8c\u8bc1\u5e93\u6765\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002\u9a8c\u8bc1 JSON \u53ef\u4ee5\u5e2e\u52a9\u60a8\u907f\u514d\u9519\u8bef\u548c\u5b89\u5168\u6f0f\u6d1e\u3002<\/li>\n\n\n\n<li>\u68c0\u67e5\u5bc6\u94a5\u662f\u5426\u5b58\u5728\uff1a\u8bbf\u95ee JSON \u5bf9\u8c61\u4e2d\u7684\u5c5e\u6027\u65f6\uff0c\u8bf7\u5728\u5c1d\u8bd5\u8bbf\u95ee\u4e4b\u524d\u68c0\u67e5\u5bc6\u94a5\u662f\u5426\u5b58\u5728\u3002\u8fd9\u53ef\u4ee5\u9632\u6b62 KeyError \u5f02\u5e38\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">if \"age\" in json_data:\n\n\u00a0age = json_data&#91;\"age\"]\n\nelse:\n\n\u00a0age = None<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5904\u7406\u5d4c\u5957 JSON\uff1a\u5982\u679c\u60a8\u7684 JSON \u6570\u636e\u662f\u6df1\u5ea6\u5d4c\u5957\u7684\uff0c\u8bf7\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6216 jsonpath-ng \u7b49\u5e93\u6765\u6709\u6548\u5730\u5bfc\u822a\u548c\u64cd\u4f5c\u5d4c\u5957\u7ed3\u6784\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"performance-considerations\"><strong>\u6027\u80fd\u8003\u8651\u56e0\u7d20<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5904\u7406\u5927\u578b JSON \u6570\u636e\u96c6\u6216\u5728\u9ad8\u541e\u5410\u91cf\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5904\u7406 JSON \u65f6\uff0c\u6548\u7387\u81f3\u5173\u91cd\u8981\u3002\u8bf7\u8003\u8651\u4ee5\u4e0b\u6027\u80fd\u63d0\u793a\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c06 json.loads() \u4e0e\u6587\u4ef6\u4e00\u8d77\u4f7f\u7528\uff1a\u4ece\u6587\u4ef6\u8bfb\u53d6 JSON \u6570\u636e\u65f6\uff0c\u5c06 json.load() \u6216 json.loads() \u4e0e\u6587\u4ef6\u5bf9\u8c61\u4e00\u8d77\u4f7f\u7528\uff0c\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u5185\u5b58\u6548\u7387\u3002\u5982\u679c JSON \u6587\u4ef6\u8fc7\u5927\uff0c\u8bf7\u907f\u514d\u5c06\u6574\u4e2a JSON \u6587\u4ef6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/li>\n\n\n\n<li>\u6d41\u5f0f\u89e3\u6790\uff1a\u5bf9\u4e8e\u975e\u5e38\u5927\u7684 JSON \u6570\u636e\uff0c\u8bf7\u8003\u8651\u4f7f\u7528 ijson \u7b49\u6d41\u5f0f\u89e3\u6790\u5668\u6765\u5206\u5757\u5904\u7406\u6570\u636e\uff0c\u6700\u5927\u9650\u5ea6\u5730\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002<\/li>\n\n\n\n<li>\u7f13\u5b58\u89e3\u6790\u540e\u7684\u6570\u636e\uff1a\u5982\u679c\u9700\u8981\u91cd\u590d\u8bbf\u95ee\u76f8\u540c\u7684JSON\u6570\u636e\uff0c\u5219\u89e3\u6790\u4e00\u6b21\u5e76\u7f13\u5b58\u89e3\u6790\u7ed3\u679c\uff0c\u907f\u514d\u591a\u4f59\u7684\u89e3\u6790\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"advanced-techniques-in-json-data-handling\"><strong>JSON \u6570\u636e\u5904\u7406\u7684\u9ad8\u7ea7\u6280\u672f<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/2FDI3RQJ3tePSv96cfg1OPzpmkfeXLBmVsESB4oZ-2x-1200x675.jpg\" alt=\"\" class=\"wp-image-922769\" title=\"\" srcset=\"https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/2FDI3RQJ3tePSv96cfg1OPzpmkfeXLBmVsESB4oZ-2x-1200x675.jpg 1200w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/2FDI3RQJ3tePSv96cfg1OPzpmkfeXLBmVsESB4oZ-2x-600x337.jpg 600w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/2FDI3RQJ3tePSv96cfg1OPzpmkfeXLBmVsESB4oZ-2x-150x84.jpg 150w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/2FDI3RQJ3tePSv96cfg1OPzpmkfeXLBmVsESB4oZ-2x-768x432.jpg 768w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/2FDI3RQJ3tePSv96cfg1OPzpmkfeXLBmVsESB4oZ-2x-1536x863.jpg 1536w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/2FDI3RQJ3tePSv96cfg1OPzpmkfeXLBmVsESB4oZ-2x.jpg 2048w, https:\/\/proxyelite.info\/wp-content\/uploads\/2024\/02\/2FDI3RQJ3tePSv96cfg1OPzpmkfeXLBmVsESB4oZ-2x-18x10.jpg 18w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6570\u636e\u64cd\u4f5c\u548c\u4ea4\u6362\u9886\u57df\uff0cJSON\uff08JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff09\u662f\u4e00\u79cd\u901a\u7528\u683c\u5f0f\uff0c\u5e7f\u6cdb\u7528\u4e8e\u5b58\u50a8\u548c\u4f20\u8f93\u7ed3\u6784\u5316\u6570\u636e\u3002\u867d\u7136\u8bb8\u591a\u4eba\u719f\u6089\u5982\u4f55\u5728 Python \u4e2d\u8bfb\u53d6 JSON \u6587\u4ef6\u3001\u89e3\u6790\u548c\u4fee\u6539 JSON \u6570\u636e\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u4f46\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8\u5728 Python \u4e2d\u638c\u63e1 JSON \u6570\u636e\u5904\u7406\u7684\u9ad8\u7ea7\u6280\u672f\u3002\u6211\u4eec\u5c06\u63a2\u7d22\u5728 Python \u4e2d\u9ad8\u6548\u52a0\u8f7d JSON\u3001\u5904\u7406\u5927\u578b JSON \u6587\u4ef6\u3001\u5408\u5e76\u548c\u7ec4\u5408 JSON \u6570\u636e\u4ee5\u53ca\u89e3\u51b3\u4e0e\u5185\u5b58\u7ba1\u7406\u3001\u91cd\u590d\u548c\u51b2\u7a81\u76f8\u5173\u7684\u95ee\u9898\u7684\u7b56\u7565\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"working-with-large-json-files\"><strong>\u5904\u7406\u5927\u578b JSON \u6587\u4ef6<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5927\u578b JSON \u6587\u4ef6\u5728\u9ad8\u6548\u5904\u7406\u548c\u5904\u7406\u65b9\u9762\u53ef\u80fd\u4f1a\u5e26\u6765\u72ec\u7279\u7684\u6311\u6218\u3002\u65e0\u8bba\u60a8\u662f\u5904\u7406\u5927\u91cf\u7684\u914d\u7f6e\u6587\u4ef6\u3001\u5e9e\u5927\u7684\u6570\u636e\u96c6\u8fd8\u662f\u590d\u6742\u7684 JSON \u7ed3\u6784\uff0c\u8fd9\u4e9b\u7b56\u7565\u90fd\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6709\u6548\u5730\u7ba1\u7406\u5927\u578b JSON \u6587\u4ef6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"strategies-for-handling-large-json-files-efficiently\"><strong>\u9ad8\u6548\u5904\u7406\u5927\u578b JSON \u6587\u4ef6\u7684\u7b56\u7565<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6d41\u5f0f\u89e3\u6790\uff1a\u5904\u7406\u5927\u578b JSON \u6587\u4ef6\u7684\u6700\u6709\u6548\u65b9\u6cd5\u4e4b\u4e00\u662f\u4f7f\u7528 ijson \u7b49\u6d41\u5f0f\u89e3\u6790\u5668\u3002\u8fd9\u4e9b\u89e3\u6790\u5668\u4ee5\u5757\u7684\u5f62\u5f0f\u8bfb\u53d6 JSON \u6570\u636e\uff0c\u5141\u8bb8\u60a8\u589e\u91cf\u5904\u7406\u6570\u636e\uff0c\u800c\u65e0\u9700\u5c06\u6574\u4e2a\u6587\u4ef6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">import ijson\n\nwith open('large_data.json', 'rb') as file:\n\n\u00a0for prefix, event, value in ijson.parse(file):\n\n\u00a0# Process JSON data incrementally<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5185\u5b58\u6620\u5c04\u6587\u4ef6\uff1aPython \u4e2d\u901a\u8fc7 mmap \u6a21\u5757\u63d0\u4f9b\u5185\u5b58\u6620\u5c04\u6587\u4ef6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06\u6587\u4ef6\u76f4\u63a5\u6620\u5c04\u5230\u5185\u5b58\u7684\u65b9\u6cd5\u3002\u6b64\u6280\u672f\u5bf9\u4e8e\u968f\u673a\u8bbf\u95ee\u5927\u578b JSON \u6587\u4ef6\u7684\u7279\u5b9a\u90e8\u5206\u975e\u5e38\u6709\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">import mmap\n\nwith open('large_data.json', 'rb') as file:\n\n\u00a0mmapped_file = mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ)\n\n# Now, you can access data in the mmapped_file as if it's in memory<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6279\u5904\u7406\uff1a\u5982\u679c\u60a8\u7684\u5927\u578b JSON \u6587\u4ef6\u53ef\u4ee5\u5206\u4e3a\u8f83\u5c0f\u7684\u5757\u6216\u6279\u6b21\uff0c\u8bf7\u8003\u8651\u5355\u72ec\u5904\u7406\u5b83\u4eec\u3002\u8fd9\u79cd\u65b9\u6cd5\u51cf\u5c11\u4e86\u5185\u5b58\u4f7f\u7528\u5e76\u5141\u8bb8\u5e76\u884c\u5904\u7406\u6570\u636e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">JSON Lines (JSONL) \u683c\u5f0f\uff1a\u8003\u8651\u4f7f\u7528 JSON Lines \u683c\u5f0f\uff08\u4e5f\u79f0\u4e3a JSONL\uff09\uff0c\u800c\u4e0d\u662f\u5c06\u6570\u636e\u5b58\u50a8\u4e3a\u5355\u4e2a\u5927\u578b JSON \u5bf9\u8c61\u3002\u5728\u8fd9\u79cd\u683c\u5f0f\u4e2d\uff0c\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\u4ee3\u8868\u4e00\u4e2a\u5355\u72ec\u7684 JSON \u5bf9\u8c61\uff0c\u4ece\u800c\u66f4\u5bb9\u6613\u589e\u91cf\u8bfb\u53d6\u548c\u5904\u7406\u6570\u636e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"combining-json-data\"><strong>\u7ec4\u5408 JSON \u6570\u636e<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u7ec4\u5408\u6765\u81ea\u591a\u4e2a\u6e90\u7684 JSON \u6570\u636e\u6216\u5408\u5e76 JSON \u5bf9\u8c61\u6216\u6570\u7ec4\u3002\u6709\u6548\u7ba1\u7406\u5408\u5e76\u8fc7\u7a0b\u5e76\u5904\u7406\u6f5c\u5728\u7684\u51b2\u7a81\u6216\u91cd\u590d\u81f3\u5173\u91cd\u8981\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"merging-multiple-json-objects-or-arrays\"><strong>\u5408\u5e76\u591a\u4e2a JSON \u5bf9\u8c61\u6216\u6570\u7ec4<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u63d0\u4f9b\u4e86\u591a\u79cd\u5408\u5e76 JSON \u6570\u636e\u7684\u65b9\u6cd5\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u60a8\u7684\u5177\u4f53\u8981\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 update() \u65b9\u6cd5\uff1a\u5982\u679c\u60a8\u6709\u591a\u4e2a JSON \u5bf9\u8c61\u5e76\u5e0c\u671b\u5c06\u5b83\u4eec\u5408\u5e76\u4e3a\u4e00\u4e2a\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u53ef\u7528\u4e8e\u5b57\u5178\u7684 update() \u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">json_data1 = {\"name\": \"Alice\", \"age\": 30}\n\njson_data2 = {\"city\": \"New York\"}\n\njson_data1.update(json_data2) # Merging json_data2 into json_data1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ec4\u5408 JSON \u6570\u7ec4\uff1a\u5904\u7406 JSON \u6570\u7ec4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 + \u8fd0\u7b97\u7b26\u6765\u8fde\u63a5\u5b83\u4eec\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">json_array1 = &#91;1, 2, 3]\n\njson_array2 = &#91;4, 5, 6]\n\ncombined_array = json_array1 + json_array2 # Concatenating arrays<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"handling-duplicates-and-conflicts\"><strong>\u5904\u7406\u91cd\u590d\u548c\u51b2\u7a81<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5408\u5e76 JSON \u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u51b2\u7a81\u6216\u91cd\u590d\u952e\u3002\u6709\u6548\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u5bf9\u4e8e\u7ef4\u62a4\u6570\u636e\u5b8c\u6574\u6027\u81f3\u5173\u91cd\u8981\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5904\u7406\u91cd\u590d\u952e\uff1a\u5982\u679c\u5408\u5e76\u5177\u6709\u91cd\u590d\u952e\u7684 JSON \u5bf9\u8c61\uff0c\u7b2c\u4e8c\u4e2a\u5bf9\u8c61\u4e2d\u7684\u503c\u5c06\u8986\u76d6\u7b2c\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u7684\u503c\u3002\u5408\u5e76\u6570\u636e\u65f6\u8bf7\u8003\u8651\u6b64\u884c\u4e3a\u4ee5\u907f\u514d\u610f\u5916\u7ed3\u679c\u3002<\/li>\n\n\n\n<li>\u51b2\u7a81\u89e3\u51b3\uff1a\u5982\u679c\u51fa\u73b0\u51b2\u7a81\uff0c\u60a8\u53ef\u4ee5\u5b9e\u65bd\u51b2\u7a81\u89e3\u51b3\u7b56\u7565\uff0c\u4f8b\u5982\u5c06\u4e00\u4e2a\u6e90\u4f18\u5148\u4e8e\u53e6\u4e00\u4e2a\u6e90\uff0c\u6216\u6839\u636e\u7279\u5b9a\u89c4\u5219\u5408\u5e76\u51b2\u7a81\u503c\u3002<\/li>\n\n\n\n<li>\u6570\u636e\u9a8c\u8bc1\uff1a\u5728\u5408\u5e76 JSON \u6570\u636e\u4e4b\u524d\uff0c\u9a8c\u8bc1\u548c\u6e05\u7406\u6570\u636e\u4ee5\u786e\u4fdd\u4e00\u81f4\u6027\u5e76\u6700\u5927\u7a0b\u5ea6\u5730\u51cf\u5c11\u51b2\u7a81\u3002\u6570\u636e\u9a8c\u8bc1\u53ef\u4ee5\u5e2e\u52a9\u63d0\u524d\u8bc6\u522b\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"use-cases-and-examples-of-json-parsing-in-python\"><strong>Python \u4e2d JSON \u89e3\u6790\u7684\u7528\u4f8b\u548c\u793a\u4f8b<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u4e2d\u7684 JSON \u89e3\u6790\u662f\u5f00\u53d1\u4eba\u5458\u7684\u4e00\u9879\u57fa\u672c\u6280\u80fd\uff0c\u56e0\u4e3a JSON\uff08JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff09\u5728\u6570\u636e\u4ea4\u6362\u548c\u5b58\u50a8\u9886\u57df\u53d1\u6325\u7740\u5173\u952e\u4f5c\u7528\u3002\u5b83\u7684\u7b80\u5355\u6027\u548c\u7075\u6d3b\u6027\u4f7f\u5176\u6210\u4e3a\u8868\u793a\u7ed3\u6784\u5316\u6570\u636e\u7684\u6700\u4f73\u9009\u62e9\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5728 Python \u9879\u76ee\u4e2d\u4f7f\u7528 JSON \u89e3\u6790\u7684\u5b9e\u9645\u7528\u4f8b\u548c\u793a\u4f8b\u3002\u6211\u4eec\u5c06\u63a2\u7d22\u5b9e\u9645\u5e94\u7528\u7a0b\u5e8f\uff0c\u63d0\u4f9b\u4ee3\u7801\u7247\u6bb5\uff0c\u5e76\u5c55\u793a\u5982\u4f55\u5229\u7528 JSON \u6765\u89e3\u51b3\u5404\u79cd\u4e0e\u6570\u636e\u76f8\u5173\u7684\u6311\u6218\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"real-world-examples-of-using-json-parsing-in-python-projects\"><strong>\u5728 Python \u9879\u76ee\u4e2d\u4f7f\u7528 JSON \u89e3\u6790\u7684\u771f\u5b9e\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"configuration-files\"><strong>\u914d\u7f6e\u6587\u4ef6<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u9879\u76ee\u4e2d JSON \u89e3\u6790\u6700\u5e38\u89c1\u7684\u7528\u4f8b\u4e4b\u4e00\u662f\u5b58\u50a8\u914d\u7f6e\u8bbe\u7f6e\u3002 JSON \u7684\u952e\u503c\u7ed3\u6784\u4f7f\u5176\u6210\u4e3a\u5b9a\u4e49\u53c2\u6570\u548c\u9009\u9879\u7684\u7406\u60f3\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u8003\u8651\u4e00\u4e2a\u5b58\u50a8\u6570\u636e\u5e93\u8fde\u63a5\u8be6\u7ec6\u4fe1\u606f\u7684 Web \u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">{\n\n\u00a0\"database\": {\n\n\u00a0\"host\": \"localhost\",\n\n\u00a0\"port\": 3306,\n\n\u00a0\"username\": \"myuser\",\n\n\u00a0\"password\": \"mypassword\",\n\n\u00a0\"database_name\": \"mydatabase\"\n\n\u00a0}\n\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u53ef\u4ee5\u8f7b\u677e\u89e3\u6790\u6b64 JSON \u914d\u7f6e\u4ee5\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">import json\n\nwith open('config.json') as config_file:\n\n\u00a0config_data = json.load(config_file)\n\n# Accessing database configuration\n\ndb_host = config_data&#91;'database']&#91;'host']\n\ndb_port = config_data&#91;'database']&#91;'port']\n\n# ...<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"web-ap-is-and-data-exchange\"><strong>Web API \u548c\u6570\u636e\u4ea4\u6362<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u4e30\u5bcc\u7684\u5e93\u4f7f\u5176\u6210\u4e3a\u4e0e Web API \u4ea4\u4e92\u7684\u6d41\u884c\u9009\u62e9\u3002\u8bb8\u591a API \u4f7f\u7528 JSON \u4f5c\u4e3a\u6570\u636e\u683c\u5f0f\u8fdb\u884c\u901a\u4fe1\u3002\u4f8b\u5982\uff0c\u4ece RESTful API \u68c0\u7d22\u5929\u6c14\u6570\u636e\u53ef\u80fd\u4f1a\u4ea7\u751f\u5982\u4e0b JSON \u54cd\u5e94\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">{\n\n\u00a0\"temperature\": 25.5,\n\n\u00a0\"humidity\": 60,\n\n\u00a0\"weather_condition\": \"Partly cloudy\"\n\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u53ef\u4ee5\u89e3\u6790\u8fd9\u4e9b JSON \u54cd\u5e94\uff0c\u63d0\u53d6\u76f8\u5173\u4fe1\u606f\uff0c\u5e76\u5c06\u5176\u5408\u5e76\u5230\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">import requests\n\nresponse = requests.get('https:\/\/api.weather.com\/forecast')\n\nweather_data = response.json()\n\ntemperature = weather_data&#91;'temperature']\n\nhumidity = weather_data&#91;'humidity']\n\nweather_condition = weather_data&#91;'weather_condition']<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"serialization-and-deserialization\"><strong>\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u5185\u7f6e\u7684 json \u6a21\u5757\u5141\u8bb8\u5e8f\u5217\u5316\uff08\u5c06 Python \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON\uff09\u548c\u53cd\u5e8f\u5217\u5316\uff08\u5c06 JSON \u89e3\u6790\u4e3a Python \u5bf9\u8c61\uff09\u3002\u5728\u4fdd\u5b58\u548c\u52a0\u8f7d\u6570\u636e\u65f6\uff0c\u6b64\u529f\u80fd\u975e\u5e38\u5b9d\u8d35\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code data-no-translation=\"\">import json\n\n# Serialization\n\ndata = {'name': 'Alice', 'age': 30}\n\njson_string = json.dumps(data) # Convert Python dict to JSON string\n\n# Deserialization\n\nparsed_data = json.loads(json_string) # Parse JSON string into Python dict<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"conclusion\"><strong>\u7ed3\u8bba<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Python \u9879\u76ee\u4e2d\u5bf9 JSON \u89e3\u6790\u7684\u63a2\u7d22\u4e2d\uff0c\u6211\u4eec\u4e86\u89e3\u4e86 JSON \u5982\u4f55\u4f5c\u4e3a\u6570\u636e\u8868\u793a\u548c\u4ea4\u6362\u7684\u591a\u529f\u80fd\u5de5\u5177\u3002\u5b83\u65e0\u7f1d\u96c6\u6210\u5230 Python \u4e2d\uff0c\u4e3a\u914d\u7f6e\u7ba1\u7406\u3001Web API \u4ea4\u4e92\u548c\u6570\u636e\u5e8f\u5217\u5316\u63d0\u4f9b\u5b9e\u7528\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u70b9\u56de\u987e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python \u4e2d\u7684 JSON \u89e3\u6790\u5bf9\u4e8e\u6709\u6548\u5904\u7406 JSON \u6570\u636e\u81f3\u5173\u91cd\u8981\u3002<\/li>\n\n\n\n<li>JSON \u89e3\u6790\u7528\u4e8e\u914d\u7f6e\u6587\u4ef6\u3001Web API \u4ea4\u4e92\u548c\u6570\u636e\u5e8f\u5217\u5316\u3002<\/li>\n\n\n\n<li>Python \u7684 json \u6a21\u5757\u63d0\u4f9b\u4e86\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684\u57fa\u672c\u5de5\u5177\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u60a8\u8fdb\u4e00\u6b65\u5c1d\u8bd5 Python \u5f00\u53d1\u65f6\uff0c\u8bf7\u8bb0\u4f4f\u63a2\u7d22\u53ef\u7528\u4e8e\u5904\u7406 JSON \u6570\u636e\u7684\u5404\u79cd\u5e93\u548c\u5de5\u5177\u3002 Python \u548c JSON \u4e4b\u95f4\u7684\u534f\u540c\u4f5c\u7528\u4f7f\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u81ea\u4fe1\u3001\u9ad8\u6548\u5730\u5e94\u5bf9\u5404\u79cd\u6570\u636e\u6311\u6218\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9f13\u52b1\u8fdb\u4e00\u6b65\u63a2\u7d22\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ee7\u7eed\u5728\u60a8\u7684 Python \u9879\u76ee\u4e2d\u5c1d\u8bd5 JSON \u89e3\u6790\u3002\u63a2\u7d22\u66f4\u590d\u6742\u7684 JSON \u7ed3\u6784\uff0c\u6df1\u5165\u7814\u7a76\u6570\u636e\u9a8c\u8bc1\uff0c\u5e76\u5c1d\u8bd5\u4f7f\u7528\u4e0d\u540c\u7684 Python \u5e93\u6765\u8fdb\u884c\u9ad8\u7ea7 JSON \u64cd\u4f5c\u3002\u901a\u8fc7\u78e8\u7ec3\u60a8\u7684 JSON \u89e3\u6790\u6280\u80fd\uff0c\u60a8\u5c06\u6210\u4e3a\u4e00\u540d\u66f4\u52a0\u591a\u624d\u591a\u827a\u548c\u9ad8\u6548\u7684\u5f00\u53d1\u4eba\u5458\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u4ed6\u8d44\u6e90\u548c\u53c2\u8003\u8d44\u6599\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python json \u6a21\u5757\u6587\u6863<\/li>\n\n\n\n<li><a href=\"https:\/\/www.json.org\/\" target=\"_blank\" rel=\"noopener\">JSON \u5b98\u65b9\u7f51\u7ad9<\/a><\/li>\n\n\n\n<li>\u5c06 JSON \u4e0e Python \u7ed3\u5408\u4f7f\u7528<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u4e86\u8fd9\u4e9b\u8d44\u6e90\u4ee5\u53ca\u60a8\u5728 Python \u4e2d\u65b0\u53d1\u73b0\u7684 JSON \u89e3\u6790\u77e5\u8bc6\uff0c\u60a8\u5c31\u53ef\u4ee5\u9a7e\u9a6d JSON \u7684\u52a8\u6001\u4e16\u754c\u5e76\u91ca\u653e\u5176\u9879\u76ee\u7684\u5168\u90e8\u6f5c\u529b\u3002\u5feb\u4e50\u7f16\u7801\uff01<\/p>","protected":false},"excerpt":{"rendered":"<p>Brief Explanation of JSON (JavaScript Object Notation) JSON, or JavaScript Object Notation, is a lightweight data interchange format that has become a cornerstone of modern data exchange on the web. It&#8217;s a text-based format that&#8217;s easy for humans to read and write, and it&#8217;s also easy for machines to parse and generate. JSON data is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":922766,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-922765","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-articles"],"acf":[],"_links":{"self":[{"href":"https:\/\/proxyelite.info\/cn\/wp-json\/wp\/v2\/posts\/922765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/proxyelite.info\/cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/proxyelite.info\/cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/proxyelite.info\/cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/proxyelite.info\/cn\/wp-json\/wp\/v2\/comments?post=922765"}],"version-history":[{"count":0,"href":"https:\/\/proxyelite.info\/cn\/wp-json\/wp\/v2\/posts\/922765\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/proxyelite.info\/cn\/wp-json\/wp\/v2\/media\/922766"}],"wp:attachment":[{"href":"https:\/\/proxyelite.info\/cn\/wp-json\/wp\/v2\/media?parent=922765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/proxyelite.info\/cn\/wp-json\/wp\/v2\/categories?post=922765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/proxyelite.info\/cn\/wp-json\/wp\/v2\/tags?post=922765"}],"curies":[{"name":"\u53ef\u6e7f\u6027\u7c89\u5242","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}