{"id":44928,"date":"2025-06-21T06:06:25","date_gmt":"2025-06-20T22:06:25","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/44928.html"},"modified":"2025-06-21T06:06:25","modified_gmt":"2025-06-20T22:06:25","slug":"%e5%9f%ba%e4%ba%8enlohmann-json-%e5%ae%9e%e7%8e%b0-%e4%bb%8ec%e5%af%b9%e8%b1%a1%e8%bd%ac%e6%8d%a2%e6%88%90json%e6%95%b0%e6%8d%ae%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/44928.html","title":{"rendered":"\u57fa\u4e8enlohmann\/json \u5b9e\u73b0 \u4eceC++\u5bf9\u8c61\u8f6c\u6362\u6210JSON\u6570\u636e\u683c\u5f0f"},"content":{"rendered":"<h2 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">C&#043;&#043;\u5bf9\u8c61\u7684JSON\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316<\/span><\/h2>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u57fa\u4e8eJsonCpp\u5e93\u5b9e\u73b0C&#043;&#043;\u5bf9\u8c61\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316<\/p>\n<h3 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">JSON \u4ecb\u7ecd<\/span><\/h3>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">JSON\u4f5c\u4e3a\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f&#xff0c;\u5728Web\u670d\u52a1\u548c\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">JSON&#xff08;JavaScript\u00a0Object Notation&#xff09;\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f&#xff0c;\u6613\u4e8e\u4eba\u9605\u8bfb\u548c\u7f16\u5199&#xff0c;\u540c\u65f6\u4e5f\u6613\u4e8e\u673a\u5668\u89e3\u6790\u548c\u751f\u6210\u3002\u5b83\u57fa\u4e8eJavaScript\u7684\u4e00\u4e2a\u5b50\u96c6&#xff0c;\u4f46\u662fJSON\u662f\u72ec\u7acb\u4e8e\u8bed\u8a00\u7684&#xff0c;\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u90fd\u652f\u6301JSON\u683c\u5f0f\u6570\u636e\u7684\u751f\u6210\u548c\u89e3\u6790\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<li style=\"text-align:justify\"><span style=\"color:#365f91\">JSON\u6570\u636e\u7ed3\u6784<\/span><\/li>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u4e24\u4e2a\u5173\u952e\u70b9: \u5bf9\u8c61\u548c\u6570\u7ec4<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5bf9\u8c61\u7531\u952e\u503c\u5bf9\u7ec4\u6210 \u952e -&gt; \u5b57\u7b26\u4e32 \u503c-&gt;\u5b57\u7b26\u4e32&#xff08;string&#xff09;&#xff0c;\u6570\u5b57&#xff08;number&#xff09;&#xff0c;\u5e03\u5c14\u503c&#xff08;boolean&#xff09;&#xff0c;\u5bf9\u8c61&#xff08;object&#xff09;&#xff08;\u5c31\u53ef\u4ee5\u7ec4\u6210\u5bf9\u8c61\u7684\u5bf9\u8c61&#xff0c;\u4e00\u76f4\u5d4c\u5957\u4e0b\u53bb&#xff09;\u6216null<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6570\u7ec4\u7531\u6709\u5e8f\u7684\u503c\u7684\u96c6\u5408\u7ec4\u6210<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">eg.<\/p>\n<p>{<br \/>\n    &#034;_comment&#034;: &#034;2025\u5e74\u914d\u7f6e\u6587\u4ef6\u7248\u672cv2.0 &#8211; \u6570\u636e\u6e90&#xff1a;\u4f01\u4e1aERP\u7cfb\u7edf&#034;,<br \/>\n    &#034;company&#034;: &#034;Tech Corp&#034;,<br \/>\n    &#034;departments&#034;: [<br \/>\n        {<br \/>\n            &#034;_comment&#034;: &#034;\u90e8\u95e8\u5bf9\u8c61\u6ce8\u91ca &#8211; name&#xff1a;\u90e8\u95e8\u540d\u79f0&#xff08;\u5fc5\u586b&#xff09;&#xff0c;employees&#xff1a;\u5458\u5de5\u5217\u8868&#xff08;\u81f3\u5c111\u4eba&#xff09;&#034;,<br \/>\n            &#034;name&#034;: &#034;Engineering&#034;,<br \/>\n            &#034;employees&#034;: [<br \/>\n                {<br \/>\n                    &#034;id&#034;: 101,<br \/>\n                    &#034;name&#034;: &#034;Alice&#034;,<br \/>\n                    &#034;skills&#034;: [&#034;Java&#034;, &#034;Python&#034;, &#034;Docker&#034;],<br \/>\n                    &#034;isManager&#034;: false,<br \/>\n                    &#034;contact&#034;: {<br \/>\n                        &#034;email&#034;: &#034;alice&#064;tech.com&#034;,<br \/>\n                        &#034;phone&#034;: [&#034;123-456-7890&#034;, &#034;987-654-3210&#034;]<br \/>\n                    }<br \/>\n                }<br \/>\n            ]<br \/>\n        }<br \/>\n    ]<br \/>\n}<\/p>\n<h4 style=\"background-color:transparent;margin-left:0px;margin-right:0px;text-align:justify\"><span style=\"color:#365f91\">2.JSON \u4e0e XML \u6bd4\u8f83<\/span><\/h4>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5728SQL Server 2014\u8fd9\u672c\u4e66\u4e0a&#xff0c;\u6211\u770b\u89c1\u770bXML\u7684\u8eab\u5f71:<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"280\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220623-6855db5fa212d.jpg\" width=\"439\" \/><\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<table cellspacing=\"0\" style=\"width:461.4pt\">\n<tr>\n<td style=\"background-color:#303030;height:13.45pt\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:white\">\u7ef4\u5ea6<\/span><\/p>\n<\/td>\n<td style=\"background-color:#303030;height:13.45pt\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:white\">JSON<\/span><span style=\"color:white\">\u4f18\u52bf\u573a\u666f<\/span><\/p>\n<\/td>\n<td style=\"background-color:#303030;height:13.45pt\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:white\">XML<\/span><span style=\"color:white\">\u4f18\u52bf\u573a\u666f<\/span><\/p>\n<\/td>\n<\/tr>\n<tbody>\n<tr>\n<td style=\"background-color:#212121;height:13.05pt\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:white\">\u200b\u200b<\/span><span style=\"color:white\">\u901f\u5ea6<\/span><span style=\"color:white\">\u200b<\/span><span style=\"color:white\">\u200b<\/span><\/p>\n<\/td>\n<td style=\"background-color:#212121;height:13.05pt\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:white\">\u9ad8\u9891\u6570\u636e\u4ea4\u6362&#xff08;\u5982\u5b9e\u65f6<\/span><span style=\"color:white\">API<\/span><span style=\"color:white\">&#xff09;<\/span><\/p>\n<\/td>\n<td style=\"background-color:#212121;height:13.05pt\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:white\">\u975e\u6027\u80fd\u654f\u611f\u573a\u666f&#xff08;\u5982\u6587\u6863\u5b58\u50a8&#xff09;<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"background-color:#212121;height:13.45pt\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:white\">\u200b\u200b<\/span><span style=\"color:white\">\u590d\u6742\u5ea6<\/span><span style=\"color:white\">\u200b<\/span><span style=\"color:white\">\u200b<\/span><\/p>\n<\/td>\n<td style=\"background-color:#212121;height:13.45pt\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:white\">\u7b80\u5355\u5230\u4e2d\u7b49\u6570\u636e\u7ed3\u6784<\/span><\/p>\n<\/td>\n<td style=\"background-color:#212121;height:13.45pt\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:white\">\u591a\u5c42\u6b21\u5d4c\u5957\u3001\u9700\u9a8c\u8bc1\u7684\u590d\u6742\u6570\u636e<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"background-color:#212121;height:13.05pt\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:white\">\u200b\u200b<\/span><span style=\"color:white\">\u751f\u6001<\/span><span style=\"color:white\">\u200b<\/span><span style=\"color:white\">\u200b<\/span><\/p>\n<\/td>\n<td style=\"background-color:#212121;height:13.05pt\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:white\">\u73b0\u4ee3<\/span><span style=\"color:white\">Web<\/span><span style=\"color:white\">\u5f00\u53d1\u3001<\/span><span style=\"color:white\">JavaScript<\/span><span style=\"color:white\">\u751f\u6001<\/span><\/p>\n<\/td>\n<td style=\"background-color:#212121;height:13.05pt\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:white\">\u4f20\u7edf\u4f01\u4e1a\u7cfb\u7edf\u3001\u8de8\u5e73\u53f0\u534f\u8bae&#xff08;\u5982<\/span><span style=\"color:white\">RSS<\/span><span style=\"color:white\">&#xff09;<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">JSON\u4e0eXML&#xff08;Extensible Markup Language&#xff09;\u90fd\u662f\u7528\u4e8e\u6570\u636e\u4ea4\u6362\u7684\u6709\u6548\u683c\u5f0f\u3002\u4e0eXML\u76f8\u6bd4&#xff0c;JSON\u66f4\u52a0\u8f7b\u91cf\u7ea7&#xff0c;\u7ed3\u6784\u7b80\u5355&#xff0c;\u89e3\u6790\u901f\u5ea6\u5feb&#xff0c;\u4e14\u6613\u4e8e\u5728JavaScript\u73af\u5883\u4e2d\u64cd\u4f5c\u3002\u7531\u4e8e\u8fd9\u4e9b\u4f18\u52bf&#xff0c;JSON\u5728Web API\u8bbe\u8ba1\u4e2d\u8d8a\u6765\u8d8a\u53d7\u6b22\u8fce&#xff0c;\u9010\u6e10\u53d6\u4ee3\u4e86XML\u6210\u4e3a\u4e3b\u6d41\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<h3 style=\"background-color:transparent;margin-left:0px;margin-right:0px;text-align:justify\"><span style=\"color:#365f91\">JSON \u5b57\u7b26\u4e32\u4e0eC&#043;&#043;\u5bf9\u8c61\u8f6c\u6362<\/span><\/h3>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5728c&#043;&#043;\u540e\u7aef\u5f00\u53d1\u4e2d&#xff0c;\u6211\u4eec\u4f1a\u6709\u9700\u6c42: \u4ecec&#043;&#043;\u5bf9\u8c61\u4e0eJSON\u5b57\u7b26\u4e32\u7684\u76f8\u4e92\u8f6c\u6362<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">JSON\u4f5c\u4e3a\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f&#xff0c;\u4e0eC&#043;&#043;\u7b49\u5f3a\u7c7b\u578b\u8bed\u8a00\u4e4b\u95f4\u7684\u8f6c\u6362&#xff0c;\u6d89\u53ca\u4e86\u6570\u636e\u6a21\u578b\u7684\u9002\u914d\u4e0e\u6620\u5c04\u95ee\u9898\u3002<\/p>\n<h4 style=\"text-align:justify\"><span style=\"color:#365f91\">JSON\u6570\u636e\u6a21\u578b\u4e0eC&#043;&#043;\u6570\u636e\u6a21\u578b\u5bf9\u6bd4<\/span><\/h4>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">JSON&#xff08;JavaScript Object Notation&#xff09;\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f&#xff0c;JSON\u6570\u636e\u6a21\u578b\u7684\u6838\u5fc3\u662f\u952e\u503c\u5bf9\u96c6\u5408&#xff0c;\u5373\u5bf9\u8c61\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">C&#043;&#043;\u662f\u4e00\u79cd\u9759\u6001\u7c7b\u578b\u3001\u7f16\u8bd1\u5f0f\u8bed\u8a00&#xff0c;\u5177\u5907\u590d\u6742\u7684\u7c7b\u578b\u7cfb\u7edf&#xff0c;\u5305\u542b\u7ed3\u6784\u4f53&#xff08;Structs&#xff09;\u3001\u7c7b&#xff08;Classes&#xff09;\u4ee5\u53ca\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bb9\u5668\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u663e\u800c\u6613\u89c1&#xff0c;\u4e24\u79cd\u6570\u636e\u6a21\u578b\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362&#xff0c;\u6211\u4eec\u9700\u8981\u5c06JSON\u5bf9\u8c61\u8f6c\u6362\u4e3aC&#043;&#043;\u7684\u7c7b\u6216\u7ed3\u6784\u4f53\u5b9e\u4f8b&#xff0c;\u53cd\u4e4b\u4ea6\u7136\u3002\u7531\u4e8eJSON\u662f\u52a8\u6001\u7684&#xff0c;\u901a\u5e38\u9700\u8981\u4e3a\u6bcf\u4e2aJSON\u5bf9\u8c61\u521b\u5efa\u4e00\u4e2a\u76f8\u5e94\u7684C&#043;&#043;\u7c7b&#xff0c;\u8fd9\u4e2a\u7c7b\u5b9a\u4e49\u4e86\u4e0eJSON\u5bf9\u8c61\u76f8\u5bf9\u5e94\u7684\u6210\u5458\u53d8\u91cf&#xff0c;\u5e76\u63d0\u4f9b\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\u7684\u63a5\u53e3\u3002<\/p>\n<h4 style=\"background-color:transparent;margin-left:0px;margin-right:0px;text-align:justify\"><span style=\"color:#365f91\">JSON \u5bf9\u8c61\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\u539f\u7406<\/span><\/h4>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5bf9\u8c61\u7684\u5e8f\u5217\u5316\u662f\u6307\u5c06\u5bf9\u8c61\u7684\u72b6\u6001\u4fe1\u606f\u8f6c\u6362\u4e3a\u53ef\u4ee5\u5b58\u50a8\u6216\u4f20\u8f93\u7684\u5f62\u5f0f\u7684\u8fc7\u7a0b<\/p>\n<h5 style=\"background-color:transparent;margin-left:0px;margin-right:0px;text-align:justify\"><span style=\"color:#365f91\">\u6570\u636e\u8f6c\u6362\u673a\u5236<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">JSON\u4e0eC&#043;&#043;\u7c7b\u578b\u7cfb\u7edf\u7684\u6620\u5c04\u662f\u5e8f\u5217\u5316\u7684\u57fa\u7840&#xff0c;\u4e3b\u8981\u4f9d\u8d56\u4ee5\u4e0b\u673a\u5236\u5b9e\u73b0&#xff1a;<\/p>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u57fa\u672c\u7c7b\u578b\u6620\u5c04\u200b\u200b<\/span><\/h6>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">JSON\u7684number\u5bf9\u5e94C&#043;&#043;\u7684int\/double&#xff0c;string\u5bf9\u5e94std::string&#xff0c;boolean\u5bf9\u5e94bool&#xff0c;null\u901a\u8fc7std::optional\u6216\u6307\u9488\u7c7b\u578b\u5904\u7406\u3002<\/p>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u5bb9\u5668\u7c7b\u578b\u6620\u5c04\u200b\u200b<\/span><\/h6>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">JSON\u6570\u7ec4\u53ef\u6620\u5c04\u5230std::vector\u3001std::list\u7b49\u7ebf\u6027\u5bb9\u5668&#xff0c;\u5bf9\u8c61\u6620\u5c04\u5230std::map\u6216\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u3002\u4f8b\u5982&#xff0c;{&#034;skills&#034;: [&#034;C&#043;&#043;&#034;, &#034;Python&#034;]}\u53ef\u81ea\u52a8\u8f6c\u6362\u4e3astd::vector&lt;std::string&gt;\u3002<\/p>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u81ea\u5b9a\u4e49\u5bf9\u8c61\u6620\u5c04\u200b\u200b<\/span><\/h6>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u200b\u200b\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u4fb5\u5165\u5f0f\u200b\u200b&#xff1a;\u901a\u8fc7\u7c7b\u7684to_json\/from_json\u65b9\u6cd5\u624b\u52a8\u5b9a\u4e49\u5b57\u6bb5\u6620\u5c04\u5173\u7cfb\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u200b\u200b\u975e\u4fb5\u5165\u5f0f\u200b\u200b&#xff1a;\u5229\u7528\u5b8f&#xff08;\u5982NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE&#xff09;\u81ea\u52a8\u751f\u6210\u6620\u5c04\u4ee3\u7801&#xff0c;\u901a\u8fc7\u53cd\u5c04\u83b7\u53d6\u6210\u5458\u53d8\u91cf\u4fe1\u606f\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u200b\u200b\u9759\u6001\u53cd\u5c04\u200b\u200b&#xff1a;\u501f\u52a9Boost.PFR\u6216\u7ed3\u6784\u5316\u7ed1\u5b9a&#xff0c;\u5728\u7f16\u8bd1\u671f\u904d\u5386\u7ed3\u6784\u4f53\u6210\u5458&#xff0c;\u5b9e\u73b0\u96f6\u4ee3\u7801\u5165\u4fb5\u7684\u5e8f\u5217\u5316\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5173\u952e\u6b65\u9aa4<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u904d\u5386\u5bf9\u8c61\u7684\u6210\u5458\u53d8\u91cf&#xff0c;\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3aJSON\u53ef\u8bc6\u522b\u7684\u952e\u503c\u5bf9\u683c\u5f0f\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5bf9\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b&#xff0c;\u76f4\u63a5\u8f6c\u6362\u5373\u53ef&#xff1b;\u5bf9\u4e8e\u590d\u6742\u7c7b\u578b&#xff0c;\u5219\u9700\u8981\u9012\u5f52\u8c03\u7528\u5e8f\u5217\u5316\u51fd\u6570\u3002\u53cd\u5e8f\u5217\u5316\u7684\u8fc7\u7a0b\u5219\u76f8\u53cd&#xff0c;\u89e3\u6790JSON\u5b57\u7b26\u4e32&#xff0c;\u5e76\u6839\u636e\u89e3\u6790\u7ed3\u679c\u521b\u5efaC&#043;&#043;\u5bf9\u8c61\u7684\u5b9e\u4f8b\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u770b\u4ee3\u7801<\/p>\n<h4 style=\"background-color:transparent;margin-left:0px;margin-right:0px;text-align:justify\"><span style=\"color:#365f91\">\u8f6c\u6362\u5b9e\u8df5\u6848\u4f8b(\u57fa\u4e8enlohmann\/json)<\/span><\/h4>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u9996\u5148&#xff0c;\u6211\u7684MinGW\u4e2d\u6ca1\u6709\u9ed8\u8ba4\u6709json\u5e93&#xff0c;\u672c\u4eba\u5728GitHub\u4e0a\u627e\u5230\u4e86\u8be5\u5e93,\u5c06\u5176\u8f7d\u5165\u6211\u7684cpp\u914d\u7f6e\u6587\u4ef6&#xff0c;\u66f4\u65b0\u6211\u7684\u914d\u7f6e\u6587\u6863<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5e93\u51fd\u6570\u4f4d\u7f6e:nlohmann\/json<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u800c\u540e\u66f4\u65b0\u6211\u7684<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"31\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220623-6855db5fb6db1.png\" width=\"323\" \/><\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"296\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220623-6855db5fc2817.jpg\" width=\"554\" \/><\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6211\u4eec\u6765\u770b\u4ee3\u7801:<\/p>\n<p>#include &lt;nlohmann\/json.hpp&gt;<br \/>\nusing json &#061; nlohmann::json;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u7ed3\u6784\u4f53\u5b9a\u4e49<br \/>\nstruct Person<br \/>\n{<br \/>\n    string name;<br \/>\n    int age;<br \/>\n    bool isStu;<br \/>\n};<\/p>\n<p>\/\/ \u5e8f\u5217\u5316\u9002\u914d\u5668\u7684\u5b9e\u73b0<br \/>\nvoid to_json(json&amp; j, const Person&amp; p) {<br \/>\n    j &#061; json{{&#034;name&#034;, p.name}, {&#034;age&#034;, p.age}, {&#034;isStu&#034;, p.isStu}};<br \/>\n}<\/p>\n<p>\/\/ \u53cd\u5e8f\u5217\u5316\u9002\u914d\u5668\u7684\u5b9e\u73b0<br \/>\nvoid fron_json(const json&amp; j, Person&amp; p) {<br \/>\n    j.at(&#034;name&#034;).get_to(p.name);<br \/>\n    j.at(&#034;age&#034;).get_to(p.age);<br \/>\n    j.at(&#034;isStu&#034;).get_to(p.isStu);<br \/>\n}<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5f53\u7136&#xff0c;\u5982\u679c&#043;&#061;stdc&#043;&#043;11\u53ca\u5176\u4ee5\u4e0a&#xff0c;\u6211\u4eec\u8fd8\u80fd\u4f7f\u7528\u5b8f\u6765\u505a<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u50cf\u8fd9\u6837&#xff1a;<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6211\u4eec\u8df3\u8f6c\u5230\u5e93\u51fd\u6570\u6765\u770b\u770b<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"187\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220623-6855db5fd984f.jpg\" width=\"554\" \/><\/p>\n<p>\/*!<br \/>\n&#064;brief macro<br \/>\n&#064;def NLOHMANN_DEFINE_TYPE_INTRUSIVE<br \/>\n&#064;since version 3.9.0<br \/>\n*\/<br \/>\n#define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, &#8230;)  \\\\<br \/>\n    friend void to_json(nlohmann::json&amp; nlohmann_json_j, const Type&amp; nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \\\\<br \/>\n    friend void from_json(const nlohmann::json&amp; nlohmann_json_j, Type&amp; nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6211\u4eec\u4f7f\u7528\u5b8f\u5c31\u53ef\u4ee5\u8fd9\u6837\u6765\u5199\u4ee3\u7801<\/p>\n<p>#include &lt;nlohmann\/json.hpp&gt;<br \/>\nusing json &#061; nlohmann::json;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u7ed3\u6784\u4f53\u5b9a\u4e49<br \/>\nstruct Person<br \/>\n{<br \/>\n    string name;<br \/>\n    int age;<br \/>\n    bool isStu;<br \/>\n    NLOHMANN_DEFINE_TYPE_INTRUSIVE(Person, name, age, isStu)<br \/>\n};<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u4e0a\u8ff0\u662f\u5728\u6211\u4eec\u5305\u542b\u4e86json\u5e93\u7684\u60c5\u51b5\u4e0b\u53ef\u4ee5\u90a3\u4e48\u53bb\u505a<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5f53\u7136&#xff0c;\u4e3a\u4e86\u4ee3\u7801\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b \u6211\u4eec\u53ef\u4ee5\u81ea\u5df1\u6765\u5b9e\u73b0\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u51fd\u6570<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u89c1\u4e0b:<\/p>\n<p>\/\/ \u624b\u52a8\u5b9e\u73b0c&#043;&#043;\u5bf9\u8c61\u4e0eJSON\u5b57\u7b26\u4e32\u7684\u8f6c\u6362  <\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\nusing namespace std;<\/p>\n<p>class Person<br \/>\n{<br \/>\nprivate:<br \/>\n    string name;<br \/>\n    int age;<br \/>\n    bool isStu;<\/p>\n<p>public:<br \/>\n    Person(const string&amp; name, int age, bool isStu) : name(name), age(age), isStu(isStu) {}<br \/>\n    Person() {}<br \/>\n    ~Person() {}<br \/>\n    \/\/ \u5e8f\u5217\u5316\u51fd\u6570<br \/>\n    string serialize() {<br \/>\n        string ans &#061; &#034;{\\\\&#034;name\\\\&#034;:\\\\&#034;&#034; &#043; name &#043; &#034;\\\\&#034;, \\\\&#034;age\\\\&#034;:&#034; &#043; std::to_string(age) &#043; &#034;, \\\\&#034;isStudent\\\\&#034;:&#034; &#043; std::to_string(isStu) &#043; &#034;}&#034;;<br \/>\n        return ans;<br \/>\n    }<br \/>\n    \/\/ \u53cd\u5e8f\u5217\u5316\u51fd\u6570<br \/>\n    static Person deserialize(const string&amp; json) {<br \/>\n        \/\/ \u89e3\u6790JSON\u5b57\u7b26\u4e32 \u586b\u5145Person\u5bf9\u8c61<br \/>\n        Person p;<br \/>\n        size_t nameStart &#061; json.find(&#034;\\\\&#034;name\\\\&#034;:\\\\&#034;&#034;) &#043; 8;<br \/>\n        size_t nameEnd &#061; json.find(&#034;\\\\&#034;&#034;, nameStart);<br \/>\n        p.name &#061; json.substr(nameStart, nameEnd &#8211; nameStart);<\/p>\n<p>        size_t ageStart &#061; json.find(&#034;\\\\&#034;age\\\\&#034;:&#034;) &#043; 6;<br \/>\n        size_t ageEnd &#061; json.find(&#034;,&#034;, ageStart);<br \/>\n        p.age &#061; stoi(json.substr(ageStart, ageEnd &#8211; ageStart));<\/p>\n<p>        size_t isStuStart &#061; json.find(&#034;\\\\&#034;isStu\\\\&#034;:&#034;) &#043; 8;<br \/>\n        p.isStu &#061; (json.substr(isStuStart, 4) &#061;&#061; &#034;true&#034;);<\/p>\n<p>        return p;<br \/>\n    }<br \/>\n};<\/p>\n<p>int main(int argc, char const *argv[])<br \/>\n{<br \/>\n    Person p &#061; {&#034;Kyrie Irving&#034;, 30, false};<br \/>\n    string serialized &#061; p.serialize();<br \/>\n    Person pDeserialized &#061; Person::deserialize(serialized);<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u5bf9\u6bd4\u5206\u6790 \u8f6c\u6362\u6548\u7387\u4e0e\u8d44\u6e90\u6d88\u8017\u5206\u6790<\/span><\/h4>\n<h5 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u624b\u52a8\u5b9e\u73b0<\/span><\/h5>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u8f6c\u6362\u6548\u7387<\/span><\/h6>\n<ul>\n<li style=\"text-align:justify\">\u4f18\u70b9&#xff1a;\u624b\u52a8\u5b9e\u73b0\u53ef\u4ee5\u9488\u5bf9\u7279\u5b9a\u7684\u5e94\u7528\u573a\u666f\u8fdb\u884c\u9ad8\u5ea6\u4f18\u5316&#xff0c;\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u51fd\u6570\u8c03\u7528\u548c\u6570\u636e\u590d\u5236\u3002\u5728\u5904\u7406\u7b80\u5355\u7684 JSON \u7ed3\u6784\u65f6&#xff0c;\u624b\u52a8\u5b9e\u73b0\u7684\u4ee3\u7801\u53ef\u4ee5\u975e\u5e38\u9ad8\u6548&#xff0c;\u56e0\u4e3a\u5b83\u76f4\u63a5\u64cd\u4f5c\u6570\u636e&#xff0c;\u6ca1\u6709\u989d\u5916\u7684\u5f00\u9500\u3002<\/li>\n<li style=\"text-align:justify\">\u7f3a\u70b9&#xff1a;\u624b\u52a8\u5b9e\u73b0\u7684\u4ee3\u7801\u901a\u5e38\u7f3a\u4e4f\u901a\u7528\u6027&#xff0c;\u5bf9\u4e8e\u590d\u6742\u7684 JSON \u7ed3\u6784&#xff0c;\u7f16\u5199\u548c\u7ef4\u62a4\u4ee3\u7801\u7684\u96be\u5ea6\u4f1a\u5927\u5927\u589e\u52a0\u3002\u800c\u4e14&#xff0c;\u624b\u52a8\u5b9e\u73b0\u5f88\u96be\u5904\u7406\u4e00\u4e9b\u590d\u6742\u7684\u60c5\u51b5&#xff0c;\u5982\u5d4c\u5957\u7684 JSON \u5bf9\u8c61\u548c\u6570\u7ec4&#xff0c;\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4ee3\u7801\u7684\u6548\u7387\u4e0b\u964d\u3002<\/li>\n<\/ul>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u8d44\u6e90\u6d88\u8017<\/span><\/h6>\n<ul>\n<li style=\"text-align:justify\">\u4f18\u70b9&#xff1a;\u624b\u52a8\u5b9e\u73b0\u53ef\u4ee5\u7cbe\u786e\u63a7\u5236\u5185\u5b58\u7684\u4f7f\u7528&#xff0c;\u907f\u514d\u4e86\u7b2c\u4e09\u65b9\u5e93\u53ef\u80fd\u5e26\u6765\u7684\u989d\u5916\u5185\u5b58\u5f00\u9500\u3002\u5728\u8d44\u6e90\u53d7\u9650\u7684\u73af\u5883\u4e2d&#xff0c;\u624b\u52a8\u5b9e\u73b0\u53ef\u80fd\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/li>\n<li style=\"text-align:justify\">\u7f3a\u70b9&#xff1a;\u624b\u52a8\u5b9e\u73b0\u9700\u8981\u5f00\u53d1\u8005\u81ea\u5df1\u5904\u7406\u5404\u79cd\u8fb9\u754c\u60c5\u51b5\u548c\u9519\u8bef\u5904\u7406&#xff0c;\u8fd9\u53ef\u80fd\u4f1a\u589e\u52a0\u4ee3\u7801\u7684\u590d\u6742\u5ea6\u548c\u5185\u5b58\u6cc4\u6f0f\u7684\u98ce\u9669\u3002\u800c\u4e14&#xff0c;\u624b\u52a8\u5b9e\u73b0\u7684\u4ee3\u7801\u901a\u5e38\u4e0d\u591f\u5065\u58ee&#xff0c;\u5bb9\u6613\u51fa\u73b0\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<h5 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u7b2c\u4e09\u65b9\u5e93<\/span><\/h5>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u8f6c\u6362\u6548\u7387<\/span><\/h6>\n<ul>\n<li style=\"text-align:justify\">\u4f18\u70b9&#xff1a;\u7b2c\u4e09\u65b9\u5e93\u901a\u5e38\u7ecf\u8fc7\u4e86\u9ad8\u5ea6\u4f18\u5316&#xff0c;\u5177\u6709\u8f83\u9ad8\u7684\u8f6c\u6362\u6548\u7387\u3002\u5b83\u4eec\u4f7f\u7528\u4e86\u5404\u79cd\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u6765\u63d0\u9ad8\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684\u901f\u5ea6&#xff0c;\u7279\u522b\u662f\u5728\u5904\u7406\u590d\u6742\u7684 JSON \u7ed3\u6784\u65f6&#xff0c;\u7b2c\u4e09\u65b9\u5e93\u7684\u4f18\u52bf\u66f4\u52a0\u660e\u663e\u3002<\/li>\n<li style=\"text-align:justify\">\u7f3a\u70b9&#xff1a;\u7b2c\u4e09\u65b9\u5e93\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u989d\u5916\u7684\u5f00\u9500&#xff0c;\u5982\u5185\u5b58\u5206\u914d\u548c\u51fd\u6570\u8c03\u7528\u3002\u5728\u5904\u7406\u7b80\u5355\u7684 JSON \u7ed3\u6784\u65f6&#xff0c;\u8fd9\u4e9b\u5f00\u9500\u53ef\u80fd\u4f1a\u663e\u5f97\u6bd4\u8f83\u660e\u663e\u3002<\/li>\n<\/ul>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u8d44\u6e90\u6d88\u8017<\/span><\/h6>\n<ul>\n<li style=\"text-align:justify\">\u4f18\u70b9&#xff1a;\u7b2c\u4e09\u65b9\u5e93\u901a\u5e38\u4f1a\u5904\u7406\u5404\u79cd\u8fb9\u754c\u60c5\u51b5\u548c\u9519\u8bef\u5904\u7406&#xff0c;\u4ee3\u7801\u66f4\u52a0\u5065\u58ee&#xff0c;\u51cf\u5c11\u4e86\u5185\u5b58\u6cc4\u6f0f\u7684\u98ce\u9669\u3002\u800c\u4e14&#xff0c;\u4e00\u4e9b\u5e93\u8fd8\u63d0\u4f9b\u4e86\u5185\u5b58\u6c60\u7b49\u673a\u5236\u6765\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u3002<\/li>\n<li style=\"text-align:justify\">\u7f3a\u70b9&#xff1a;\u7b2c\u4e09\u65b9\u5e93\u53ef\u80fd\u4f1a\u5f15\u5165\u989d\u5916\u7684\u4f9d\u8d56\u548c\u5f00\u9500&#xff0c;\u589e\u52a0\u4e86\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5927\u5c0f\u3002\u5728\u8d44\u6e90\u53d7\u9650\u7684\u73af\u5883\u4e2d&#xff0c;\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u95ee\u9898\u3002<\/li>\n<\/ul>\n<h5 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u8f6c\u6362\u6548\u7387\u4e0e\u8d44\u6e90\u6d88\u8017\u5206\u6790\u5de5\u5177<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u4f7f\u7528\u6027\u80fd\u5206\u6790\u5de5\u5177&#xff08;\u5982gprof\u3001Valgrind\u7b49&#xff09;\u6765\u76d1\u6d4b\u7a0b\u5e8f\u7684 CPU \u4f7f\u7528\u60c5\u51b5\u548c\u5185\u5b58\u5206\u914d\u60c5\u51b5\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u663e\u7136&#xff0c;\u6211\u4eec\u53d1\u73b0&#xff0c;\u624b\u52a8\u5b9e\u73b0&#xff0c;\u66f4\u9700\u8981\u6211\u4eec\u6ce8\u610f\u9519\u8bef\u5904\u7406\u673a\u5236<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<h3 style=\"margin-left:0px;margin-right:0px;text-align:justify\">\u9519\u8bef\u5904\u7406\u673a\u5236 \u00a0\u00a0\u00a0 <span style=\"color:#365f91\">\u00a0\u00a0<\/span><\/h3>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">JSON\u89e3\u6790\u9519\u8bef\u901a\u5e38\u53d1\u751f\u5728JSON\u683c\u5f0f\u4e0d\u6b63\u786e\u65f6&#xff0c;\u6bd4\u5982\u7f3a\u5c11\u95ed\u5408\u7684\u62ec\u53f7\u3001\u9017\u53f7\u4f7f\u7528\u9519\u8bef\u3001\u6216\u952e\u503c\u5bf9\u672a\u6b63\u786e\u914d\u5bf9\u7b49\u3002\u5728C&#043;&#043;\u4e2d\u89e3\u6790JSON\u5b57\u7b26\u4e32&#xff0c;\u5f53\u9047\u5230\u9519\u8bef\u65f6&#xff0c;\u9700\u8981\u5177\u4f53\u5206\u6790\u9519\u8bef\u7c7b\u578b&#xff0c;\u4ee5\u4fbf\u91c7\u53d6\u76f8\u5e94\u7684\u5904\u7406\u63aa\u65bd\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6211\u4eec\u6765\u770b\u770b\u00a0nlohmann\/json\u00a0\u8fd9\u6837\u7684\u5e93\u662f\u5982\u4f55\u89e3\u51b3\u7684&#xff0c;\u5b66\u4e60 \u4e00\u4e0b\u5176\u89e3\u6790\u9519\u8bef\u5904\u7406\u7684\u65b9\u5f0f<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6211\u4eec\u6765\u770b\u770b\u8fd9\u4e2a\u5e93\u4e2d\u7684\u00a0 <span style=\"background-color:yellow\">parse_error\u00a0<\/span><span style=\"background-color:yellow\">\u5f02\u5e38<\/span>\u00a0 \u7684\u5b9e\u73b0<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"210\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220623-6855db5fea72f.jpg\" width=\"554\" \/><\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6211\u4eec\u6765\u89e3\u6790\u4e00\u4e0b\u8be5\u6a21\u677f\u51fd\u6570<\/p>\n<h5 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u6a21\u677f\u53c2\u6570 Exception\u200b\u200b<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u8be5\u51fd\u6570\u662f\u4e00\u4e2a\u6a21\u677f\u51fd\u6570&#xff0c;\u5141\u8bb8\u63a5\u6536\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u5bf9\u8c61&#xff08;\u5982 parse_error \u6216 exception&#xff09;&#xff0c;\u4f53\u73b0\u4e86\u5e93\u7684\u7075\u6d3b\u6027\u548c\u5bf9\u591a\u79cd\u5f02\u5e38\u7c7b\u578b\u7684\u652f\u6301\u3002<\/p>\n<h5 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u53c2\u6570\u8bf4\u660e\u200b\u200b<\/span><\/h5>\n<li style=\"text-align:justify\">std::size_t&#xff1a;\u901a\u5e38\u8868\u793a\u89e3\u6790\u9519\u8bef\u7684\u4f4d\u7f6e&#xff08;\u5982\u884c\u53f7\u6216\u5217\u53f7&#xff09;&#xff0c;\u4f46\u6b64\u5904\u88ab\u6ce8\u91ca\u5ffd\u7565&#xff08;\u53ef\u80fd\u7531\u5176\u4ed6\u673a\u5236\u5904\u7406&#xff09;\u3002<\/li>\n<li style=\"text-align:justify\">const std::string&amp;&#xff1a;\u9519\u8bef\u63cf\u8ff0\u4fe1\u606f&#xff0c;\u540c\u6837\u88ab\u6ce8\u91ca\u5ffd\u7565&#xff0c;\u5b9e\u9645\u9519\u8bef\u4fe1\u606f\u901a\u8fc7\u5f02\u5e38\u5bf9\u8c61 ex \u4f20\u9012\u3002<\/li>\n<li style=\"text-align:justify\">const Exception&amp; ex&#xff1a;\u5177\u4f53\u7684\u5f02\u5e38\u5bf9\u8c61&#xff0c;\u5305\u542b\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f&#xff08;\u5982\u9519\u8bef\u7c7b\u578b\u3001\u4f4d\u7f6e\u7b49&#xff09;\u3002<\/li>\n<h5 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u6838\u5fc3\u903b\u8f91\u5206\u6790\u200b\u200b<\/span><\/h5>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u6807\u8bb0\u9519\u8bef\u72b6\u6001\u200b\u200b<\/span><\/h6>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">errored &#061; true;<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u8bbe\u7f6e\u5e03\u5c14\u53d8\u91cf errored \u4e3a true&#xff0c;\u7528\u4e8e\u8bb0\u5f55\u89e3\u6790\u8fc7\u7a0b\u4e2d\u53d1\u751f\u4e86\u9519\u8bef\u3002\u6b64\u53d8\u91cf\u53ef\u80fd\u5728\u540e\u7eed\u6d41\u7a0b\u4e2d\u7528\u4e8e\u5224\u65ad\u662f\u5426\u9700\u8981\u7ec8\u6b62\u89e3\u6790\u6216\u56de\u6eda\u64cd\u4f5c\u3002<\/p>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u5f02\u5e38\u5bf9\u8c61\u7684\u5904\u7406\u200b\u200b<\/span><\/h6>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">static_cast&lt;void&gt;(ex);<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6b64\u64cd\u4f5c\u4ec5\u4e3a\u907f\u514d\u7f16\u8bd1\u5668\u5bf9\u672a\u4f7f\u7528\u53d8\u91cf ex \u7684\u8b66\u544a&#xff0c;\u65e0\u5b9e\u9645\u529f\u80fd\u5f71\u54cd\u3002<\/p>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u5f02\u5e38\u629b\u51fa\u6761\u4ef6\u200b\u200b<\/span><\/h6>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">if (allow_exceptions)<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">{<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u00a0\u00a0\u00a0 JSON_THROW(ex);<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">}<\/p>\n<li style=\"text-align:justify\">allow_exceptions \u662f\u5e93\u7684\u5168\u5c40\u914d\u7f6e\u6807\u5fd7&#xff0c;\u63a7\u5236\u662f\u5426\u5141\u8bb8\u629b\u51fa\u5f02\u5e38\u3002\u82e5\u4e3a true&#xff0c;\u5219\u901a\u8fc7\u5b8f JSON_THROW \u629b\u51fa\u5f02\u5e38 ex\u3002<\/li>\n<li style=\"text-align:justify\">JSON_THROW \u662f\u5e93\u5b9a\u4e49\u7684\u5b8f&#xff0c;\u901a\u5e38\u5c01\u88c5\u4e86 throw \u8bed\u53e5&#xff0c;\u7528\u4e8e\u7edf\u4e00\u5f02\u5e38\u629b\u51fa\u884c\u4e3a\u3002<\/li>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u8fd4\u56de\u503c\u200b\u200b<\/span><\/h6>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">return false;<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u8fd4\u56de false \u8868\u793a\u89e3\u6790\u5931\u8d25&#xff0c;\u901a\u77e5\u4e0a\u5c42\u89e3\u6790\u5668\u7ec8\u6b62\u5f53\u524d\u89e3\u6790\u6d41\u7a0b\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u9519\u8bef\u5904\u7406\u5206\u5c42\u200b\u200b<\/span><\/h6>\n<li style=\"text-align:justify\">\u200b\u200b\u5f02\u5e38\u6a21\u5f0f\u200b\u200b&#xff1a;\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff08;allow_exceptions&#061;true&#xff09;&#xff0c;\u76f4\u63a5\u629b\u51fa\u5f02\u5e38&#xff0c;\u9002\u5408\u9700\u8981\u5373\u65f6\u7ec8\u6b62\u89e3\u6790\u7684\u573a\u666f&#xff08;\u5982\u914d\u7f6e\u89e3\u6790\u5931\u8d25\u9700\u7acb\u5373\u62a5\u9519&#xff09;\u3002<\/li>\n<li style=\"text-align:justify\">\u200b\u200b\u9759\u9ed8\u6a21\u5f0f\u200b\u200b&#xff1a;\u82e5\u5173\u95ed\u5f02\u5e38&#xff08;allow_exceptions&#061;false&#xff09;&#xff0c;\u4ec5\u8bb0\u5f55\u9519\u8bef\u72b6\u6001&#xff0c;\u9002\u5408\u9700\u8981\u5bb9\u9519\u5904\u7406\u7684\u573a\u666f&#xff08;\u5982\u65e5\u5fd7\u8bb0\u5f55\u6216\u6d41\u5f0f\u89e3\u6790&#xff09;\u3002<\/li>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u5f02\u5e38\u4fe1\u606f\u4f20\u9012\u200b\u200b<\/span><\/h6>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u901a\u8fc7\u5f02\u5e38\u5bf9\u8c61 ex \u4f20\u9012\u5177\u4f53\u9519\u8bef\u4fe1\u606f&#xff08;\u5982 parse_error::create(101, &#8230;)&#xff09;&#xff0c;\u5305\u542b\u9519\u8bef\u4ee3\u7801\u3001\u4f4d\u7f6e\u548c\u63cf\u8ff0&#xff0c;\u4fbf\u4e8e\u5f00\u53d1\u8005\u5b9a\u4f4d\u95ee\u9898\u3002\u4f8b\u5982&#xff0c;\u7528\u6237\u9047\u5230\u7684\u7a7a\u8f93\u5165\u9519\u8bef\u4f1a\u89e6\u53d1 [json.exception.parse_error.101]&#xff0c;\u660e\u786e\u63d0\u793a\u95ee\u9898\u539f\u56e0\u3002<\/p>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u4e0e\u89e3\u6790\u6d41\u7a0b\u7684\u96c6\u6210\u200b\u200b<\/span><\/h6>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6b64\u51fd\u6570\u901a\u5e38\u7531\u8bcd\u6cd5\u5206\u6790\u5668&#xff08;lexer&#xff09;\u6216\u8bed\u6cd5\u5206\u6790\u5668&#xff08;parser&#xff09;\u5728\u68c0\u6d4b\u5230\u975e\u6cd5 Token \u6216\u7ed3\u6784\u65f6\u8c03\u7528&#xff0c;\u6784\u6210\u5b8c\u6574\u7684\u9519\u8bef\u5904\u7406\u94fe\u8def\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u200b\u200b\u4e0e\u5176\u4ed6\u673a\u5236\u7684\u534f\u4f5c\u200b\u200b<\/p>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200bassert_invariant \u68c0\u67e5\u200b\u200b<\/span><\/h6>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5728\u89e3\u6790\u8fc7\u7a0b\u4e2d&#xff0c;\u5e93\u901a\u8fc7 assert_invariant \u51fd\u6570\u6821\u9a8c JSON \u5bf9\u8c61\u72b6\u6001&#xff08;\u5982\u6307\u9488\u975e\u7a7a&#xff09;&#xff0c;\u786e\u4fdd\u9519\u8bef\u4e0d\u4f1a\u8fdb\u4e00\u6b65\u4f20\u64ad\u5230\u4e0d\u4e00\u81f4\u7684\u72b6\u6001\u3002<\/p>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u9519\u8bef\u65e5\u5fd7\u8bb0\u5f55\u200b\u200b<\/span><\/h6>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u7528\u6237\u53ef\u901a\u8fc7\u81ea\u5b9a\u4e49\u56de\u8c03\u51fd\u6570\u6216\u6355\u83b7\u5f02\u5e38\u540e\u8bb0\u5f55\u65e5\u5fd7&#xff08;\u5982 log_error \u51fd\u6570&#xff09;&#xff0c;\u5b9e\u73b0\u9519\u8bef\u8ffd\u8e2a\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u4f9d\u6258\u4e8e\u00a0nlohmann\/json\u00a0\u5e93 \u6211\u4eec\u53ef\u4ee5\u5904\u7406\u591a\u79cd\u5f02\u5e38\u9519\u8bef<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<h4 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u9519\u8bef\u7c7b\u578b\u53ca\u68c0\u6d4b<\/span><\/h4>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;fstream&gt;<br \/>\n#include &lt;nlohmann\/json.hpp&gt;<\/p>\n<p>using json &#061; nlohmann::json;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u65e5\u5fd7\u8bb0\u5f55\u51fd\u6570<br \/>\nvoid log_error(const string&amp; message, int error_code &#061; 0) {<br \/>\n    cerr &lt;&lt; &#034;[ERROR] Code &#034; &lt;&lt; error_code &lt;&lt; &#034;: &#034; &lt;&lt; message &lt;&lt; endl;<br \/>\n    \/\/ \u53ef\u62d3\u5c55 \u5c06\u9519\u8bef\u5199\u5165\u6587\u4ef6<br \/>\n}<br \/>\nint main()<br \/>\n{<br \/>\n    try {<br \/>\n        \/\/ condition1: \u6587\u4ef6\u4e2d\u8bfb\u53d6JSON<br \/>\n        ifstream file(&#034;data.json&#034;);<br \/>\n        if (!file) {<br \/>\n            log_error(&#034;\u6253\u5f00\u6587\u4ef6\u5f02\u5e38&#034;, 1001);<br \/>\n            exit(1);<br \/>\n        }<br \/>\n        json j;<br \/>\n        file &gt;&gt; j;      \/\/ \u629b\u51faparse_error \u53ef\u80fd<\/p>\n<p>        \/\/ condition2: \u4ece\u5b57\u7b26\u4e32\u89e3\u6790JSON<br \/>\n        string json_str &#061; R&#034;({&#034;name&#034;: &#034;Kevin&#034;, &#034;age&#034;: &#034;invalid_value&#034;})&#034;;<br \/>\n        json j2 &#061; json::parse(json_str); \/\/ \u629b\u51fa type_error<\/p>\n<p>        \/\/ condition3: \u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u5b57\u6bb5<br \/>\n        cout &lt;&lt; j[&#034;nonexistent_key&#034;]; \/\/ \u53ef\u80fd\u629b\u51fa out_of_range<br \/>\n    } catch (const json::parse_error&amp; e) {<br \/>\n        \/\/ \u6355\u83b7 parse_error<br \/>\n        \/\/ \u89e3\u6790\u9519\u8bef&#xff08;\u5982\u8bed\u6cd5\u9519\u8bef\u3001\u7a7a\u8f93\u5165&#xff09;<br \/>\n        log_error(&#034;Parse error: &#034; &#043; string(e.what()), e.id);<br \/>\n        \/\/ \u9519\u8bef\u4f4d\u7f6e  \u6309\u5b57\u8282\u5b9a\u4f4d<br \/>\n        cerr &lt;&lt; &#034;Error position: byte &#034; &lt;&lt; e.byte &lt;&lt; endl;<br \/>\n    } catch (const json::type_error&amp; e) {<br \/>\n        \/\/ \u7c7b\u578b\u9519\u8bef<br \/>\n        log_error(&#034;Type error: &#034; &#043; string(e.what()), e.id);<br \/>\n    } catch (const json::out_of_range&amp; e) {<br \/>\n        \/\/ \u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u6216\u7d22\u5f15<br \/>\n        log_error(&#034;Key not found: &#034; &#043; string(e.what()), e.id);<br \/>\n    } catch (const std::exception&amp; e) {<br \/>\n        \/\/ \u901a\u7528\u9519\u8bef<br \/>\n        log_error(&#034;Unexpected error: &#034; &#043; string(e.what()));<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:left\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<h5 style=\"background-color:transparent;margin-left:0px;margin-right:0px;text-align:justify\"><span style=\"color:#365f91\">1.\u00a0 JSON\u89e3\u6790\u9519\u8bef<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5728\u5904\u7406JSON\u6570\u636e\u65f6&#xff0c;\u89e3\u6790\u9519\u8bef\u662f\u6700\u5e38\u89c1\u4e5f\u662f\u6700\u76f4\u63a5\u7684\u95ee\u9898\u3002JSON\u89e3\u6790\u9519\u8bef\u901a\u5e38\u53d1\u751f\u5728JSON\u683c\u5f0f\u4e0d\u6b63\u786e\u65f6&#xff0c;\u6bd4\u5982\u7f3a\u5c11\u95ed\u5408\u7684\u62ec\u53f7\u3001\u9017\u53f7\u4f7f\u7528\u9519\u8bef\u3001\u6216\u952e\u503c\u5bf9\u672a\u6b63\u786e\u914d\u5bf9\u7b49\u3002\u5728C&#043;&#043;\u4e2d\u89e3\u6790JSON\u5b57\u7b26\u4e32&#xff0c;\u5f53\u9047\u5230\u9519\u8bef\u65f6&#xff0c;\u9700\u8981\u5177\u4f53\u5206\u6790\u9519\u8bef\u7c7b\u578b&#xff0c;\u4ee5\u4fbf\u91c7\u53d6\u76f8\u5e94\u7684\u5904\u7406\u63aa\u65bd\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u4e0a\u8ff0\u4ee3\u7801\u6bb5\u4e2d<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"224\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220624-6855db60092b9.jpg\" width=\"484\" \/><\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"139\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220624-6855db601a98c.jpg\" width=\"554\" \/><\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u8fd9\u4e24\u79cd\u60c5\u51b5\u5c31\u662f\u6700\u5e38\u9047\u89c1\u7684\u5f02\u5e38<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<h5 style=\"text-align:justify\"><span style=\"color:#365f91\">2. \u6570\u636e\u7c7b\u578b\u8f6c\u6362\u9519\u8bef<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">JSON\u6570\u636e\u5305\u62ec\u516d\u79cd\u57fa\u672c\u7c7b\u578b&#xff1a;\u5bf9\u8c61\u3001\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u548cnull\u3002\u5728\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aC&#043;&#043;\u5bf9\u8c61\u65f6&#xff0c;\u53ef\u80fd\u4f1a\u9047\u5230\u6570\u636e\u7c7b\u578b\u4e0d\u5339\u914d\u7684\u95ee\u9898\u3002\u6bd4\u5982&#xff0c;\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u6570\u5b57\u7c7b\u578b\u7684JSON\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578bC&#043;&#043;\u5bf9\u8c61&#xff0c;\u6216\u8005\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u578b\u7684JSON\u503c\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\u5bf9\u8c61\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6765\u770b\u4e00\u6bb5\u4ee3\u7801:<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;nlohmann\/json.hpp&gt;<\/p>\n<p>using json &#061; nlohmann::json;<\/p>\n<p>int main() {<br \/>\n    std::string json_str &#061; R&#034;({&#034;number&#034;: &#034;123&#034;, &#034;string&#034;: &#034;hello&#034;, &#034;bool&#034;: true})&#034;;<br \/>\n    json j;<\/p>\n<p>    try {<br \/>\n        j &#061; json::parse(json_str);<\/p>\n<p>        \/\/ \u68c0\u67e5\u5b57\u6bb5\u5b58\u5728\u6027&#xff08;\u907f\u514d out_of_range&#xff09;<br \/>\n        if (j.contains(&#034;number&#034;) &amp;&amp; j[&#034;number&#034;].is_number()) {<br \/>\n            int number &#061; j[&#034;number&#034;];<br \/>\n        } else {<br \/>\n            std::cerr &lt;&lt; &#034;number \u5b57\u6bb5\u7f3a\u5931\u6216\u7c7b\u578b\u9519\u8bef&#034; &lt;&lt; std::endl;<br \/>\n        }<\/p>\n<p>        std::string str &#061; j.value(&#034;string&#034;, &#034;default_str&#034;);<br \/>\n        bool bool_val &#061; j[&#034;bool&#034;].get&lt;bool&gt;();<\/p>\n<p>    } catch (const json::type_error&amp; e) {<br \/>\n        std::cerr &lt;&lt; &#034;\u7c7b\u578b\u9519\u8bef: &#034; &lt;&lt; e.what() &lt;&lt; std::endl;<br \/>\n    } catch (const json::parse_error&amp; e) {<br \/>\n        std::cerr &lt;&lt; &#034;JSON\u89e3\u6790\u5931\u8d25: &#034; &lt;&lt; e.what() &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u7ed3\u5408\u8be5\u4ee3\u7801\u4e0e\u4e0a\u6587&#xff0c;\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0 \u6355\u83b7\u5230 json::type_error \u4e2d\u7684\u5f02\u5e38<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5f53\u7136&#xff0c;nlohmann\/json\u00a0\u5e93&#xff0c;\u5176\u63d0\u4f9b\u4e86\u00a0get&lt;T&gt;()\u00a0\u65b9\u6cd5\u6765\u5c1d\u8bd5\u8fdb\u884c\u5b89\u5168\u7c7b\u578b\u8f6c\u6362&#xff0c;\u5e76\u901a\u8fc7\u5f02\u5e38\u6765\u62a5\u544a\u7c7b\u578b\u8f6c\u6362\u9519\u8bef&#xff1a;<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6211\u4eec\u5728\u5e93\u51fd\u6570\u4e2d\u4e5f\u80fd\u770b\u89c1\u5176\u5b9a\u4e49:<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"315\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220624-6855db602bca7.jpg\" width=\"550\" \/><\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"192\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220624-6855db603efa5.jpg\" width=\"553\" \/><\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6211\u5c06\u5176\u6252\u4e0b\u6765\u5177\u4f53\u770b\u770b&#xff1a;<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">nlohmann\/json.hpp<\/p>\n<p>\/*<br \/>\n    &#064;brief get next character from the input<\/p>\n<p>    This function provides the interface to the used input adapter. It does<br \/>\n    not throw in case the input reached EOF, but returns a<br \/>\n    &#096;char_traits&lt;char&gt;::eof()&#096; in that case.  Stores the scanned characters<br \/>\n    for use in error messages.<\/p>\n<p>    &#064;return character read from the input<br \/>\n    *\/<br \/>\n    char_int_type get()<br \/>\n    {<br \/>\n        &#043;&#043;position.chars_read_total;<br \/>\n        &#043;&#043;position.chars_read_current_line;<\/p>\n<p>        if (next_unget)<br \/>\n        {<br \/>\n            \/\/ just reset the next_unget variable and work with current<br \/>\n            next_unget &#061; false;<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            current &#061; ia.get_character();<br \/>\n        }<\/p>\n<p>        if (JSON_HEDLEY_LIKELY(current !&#061; char_traits&lt;char_type&gt;::eof()))<br \/>\n        {<br \/>\n            token_string.push_back(char_traits&lt;char_type&gt;::to_char_type(current));<br \/>\n        }<\/p>\n<p>        if (current &#061;&#061; &#039;\\\\n&#039;)<br \/>\n        {<br \/>\n            &#043;&#043;position.lines_read;<br \/>\n            position.chars_read_current_line &#061; 0;<br \/>\n        }<\/p>\n<p>        return current;<br \/>\n    }<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6211\u4eec\u6765\u7ffb\u8bd1\u4e00\u4e0b\u8be5\u51fd\u6570\u7684&#064;brief\u6ce8\u91ca<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\/ *<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u4ece\u8f93\u5165\u4e2d\u83b7\u53d6\u4e0b\u4e00\u4e2a\u5b57\u7b26<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u8fd9\u4e2a\u51fd\u6570\u4e3a\u4f7f\u7528\u7684\u8f93\u5165\u9002\u914d\u5668\u63d0\u4f9b\u63a5\u53e3\u3002\u5b83<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5982\u679c\u8f93\u5165\u8fbe\u5230EOF&#xff0c;\u5219\u4e0d\u629b\u51fa&#xff0c;\u800c\u662f\u8fd4\u56dea<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u2018 char_traits&lt;char&gt;::eof() \u2019\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u3002\u5b58\u50a8\u626b\u63cf\u7684\u5b57\u7b26<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u7528\u4e8e\u9519\u8bef\u6d88\u606f\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">&#064;\u8fd4\u56de\u4ece\u8f93\u5165\u4e2d\u8bfb\u53d6\u7684\u5b57\u7b26<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">* \/<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">get&lt;T&gt;()\u00a0\u7684\u5b9e\u73b0\u7ed3\u5408\u4e86\u7c7b\u578b\u5b89\u5168\u3001\u5f02\u5e38\u5b89\u5168\u548c\u6a21\u677f\u5143\u7f16\u7a0b\u6280\u672f&#xff0c;\u5176\u6838\u5fc3\u5728\u4e8e&#xff1a;<\/p>\n<li style=\"text-align:justify\">\u200b\u200b\u4e25\u683c\u7684\u7c7b\u578b\u68c0\u67e5\u200b\u200b&#xff1a;\u786e\u4fdd\u8f6c\u6362\u7684\u5b89\u5168\u6027\u3002<\/li>\n<li style=\"text-align:justify\">\u200b\u200b\u7075\u6d3b\u7684\u6a21\u677f\u7279\u5316\u200b\u200b&#xff1a;\u652f\u6301\u4ece\u57fa\u7840\u7c7b\u578b\u5230\u590d\u6742\u5bf9\u8c61\u7684\u8f6c\u6362\u3002<\/li>\n<li style=\"text-align:justify\">\u200b\u200b\u4e0e\u53cd\u5e8f\u5217\u5316\u903b\u8f91\u7684\u6df1\u5ea6\u96c6\u6210\u200b\u200b&#xff1a;\u901a\u8fc7\u5b8f\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u6269\u5c55\u7528\u6237\u7c7b\u578b\u652f\u6301\u3002<\/li>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p>#include &lt;nlohmann\/json.hpp&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\nusing json &#061; nlohmann::json;<br \/>\nusing namespace std;<\/p>\n<p>template&lt;typename T&gt;<br \/>\nbool SafeGet(const json&amp; j, const string&amp; key, T&amp; val, const T&amp; default_val &#061; T{}) {<br \/>\n    if (!j.contains(key))<br \/>\n        return false;<\/p>\n<p>    try {<br \/>\n        val &#061; j.at(key).get&lt;T&gt;();<br \/>\n        return true;<br \/>\n    } catch(const std::exception&amp; e) {<br \/>\n        val &#061; default_val;<br \/>\n        return false;<br \/>\n    }<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    json j &#061; json::parse(R&#034;({&#034;number&#034;: &#034;123&#034;, &#034;bool&#034;: true})&#034;);<br \/>\n    int num &#061; 0;<br \/>\n    if (!SafeGet(j, &#034;number&#034;, num)) {<br \/>\n        cerr &lt;&lt; &#034;num \u5b57\u6bb5\u65e0\u6548&#xff0c;\u4f7f\u7528\u9ed8\u8ba4\u503c: &#034; &lt;&lt; num &lt;&lt; endl;<br \/>\n    }<br \/>\n    bool flag &#061; false;<br \/>\n    SafeGet(j, &#034;bool&#034;, flag, false);        \/\/ \u5e26\u9ed8\u8ba4\u503c\u7684\u91cd\u8f7d<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:left\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u8fd9\u5c31\u662f\u53ef\u4ee5\u5b89\u5168\u4f7f\u7528\u7684get&lt;T&gt;()\u7684\u793a\u4f8b<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<h5 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">3. \u00a0\u7f16\u7801\/\u89e3\u7801\u9519\u8bef<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">JSON\u6570\u636e\u7684\u7f16\u7801\u548c\u89e3\u7801\u9519\u8bef\u901a\u5e38\u6d89\u53ca\u5230\u5b57\u7b26\u7f16\u7801\u95ee\u9898&#xff0c;\u6bd4\u5982\u5728\u4f7f\u7528\u975eUTF-8\u7f16\u7801\u7684JSON\u6570\u636e\u65f6&#xff0c;\u5982\u679c\u6ca1\u6709\u6b63\u786e\u5904\u7406&#xff0c;\u5c31\u4f1a\u51fa\u73b0\u4e71\u7801\u6216\u89e3\u6790\u5931\u8d25\u7684\u95ee\u9898\u3002\u5728C&#043;&#043;\u4e2d\u5904\u7406JSON\u6570\u636e\u65f6&#xff0c;\u9700\u8981\u786e\u4fddJSON\u5b57\u7b26\u4e32\u548cC&#043;&#043;\u7a0b\u5e8f\u4f7f\u7528\u76f8\u540c\u7684\u5b57\u7b26\u7f16\u7801\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5728\u6211\u4eec\u7684nlohmann\/json\u5e93\u4e2d&#xff0c;\u5185\u90e8\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\u8fdb\u884c\u5904\u7406\u3002\u5982\u679c\u9047\u5230\u975eUTF-8\u7f16\u7801\u7684\u5b57\u7b26\u4e32&#xff0c;\u9700\u8981\u5728\u89e3\u6790\u4e4b\u524d\u8fdb\u884c\u8f6c\u7801\u3002\u8fd9\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u4e13\u95e8\u7684\u7f16\u7801\u8f6c\u6362\u5e93\u6765\u5b8c\u6210\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6211\u4eec\u8fd8\u662f\u6765\u770b\u4ee3\u7801<\/p>\n<p>#include &lt;iconv.h&gt;<br \/>\n#include &lt;stdexcept&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;nlohmann\/json.hpp&gt;<br \/>\nusing json &#061; nlohmann::json;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u8c03\u7528iconv.h\u5e93 \u8f6c\u6362\u7f16\u7801\u683c\u5f0f<br \/>\n\/**<br \/>\n * &#064;brief \u5c06\u8f93\u5165\u5b57\u7b26\u4e32\u4ece\u4e00\u79cd\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u5b57\u7b26\u7f16\u7801\u3002<br \/>\n *<br \/>\n * &#064;param input \u8f93\u5165\u7684\u5b57\u7b26\u4e32&#xff0c;\u5f85\u8f6c\u6362\u7684\u6587\u672c\u5185\u5bb9\u3002<br \/>\n * &#064;param from_encoding \u6e90\u5b57\u7b26\u7f16\u7801\u683c\u5f0f&#xff0c;\u4f8b\u5982 &#034;GBK&#034;\u3002<br \/>\n * &#064;param to_encoding \u76ee\u6807\u5b57\u7b26\u7f16\u7801\u683c\u5f0f&#xff0c;\u9ed8\u8ba4\u4e3a &#034;UTF-8&#034;\u3002<br \/>\n * &#064;return string \u8f6c\u6362\u540e\u7684\u5b57\u7b26\u4e32&#xff0c;\u7f16\u7801\u683c\u5f0f\u4e3a\u76ee\u6807\u7f16\u7801\u3002<br \/>\n *<br \/>\n * &#064;throws runtime_error \u5982\u679c\u521d\u59cb\u5316 iconv \u6216\u7f16\u7801\u8f6c\u6362\u5931\u8d25&#xff0c;\u5c06\u629b\u51fa\u5f02\u5e38\u3002<br \/>\n *<br \/>\n * &#064;note \u4f7f\u7528 iconv \u5e93\u8fdb\u884c\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\u3002iconv_open \u7528\u4e8e\u521d\u59cb\u5316\u8f6c\u6362\u63cf\u8ff0\u7b26&#xff0c;<br \/>\n *       iconv \u6267\u884c\u5b9e\u9645\u7684\u7f16\u7801\u8f6c\u6362&#xff0c;iconv_close \u91ca\u653e\u8d44\u6e90\u3002<br \/>\n *<br \/>\n * &#064;details<br \/>\n * &#8211; iconv_open: \u521d\u59cb\u5316\u4e00\u4e2a\u7528\u4e8e\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\u7684\u8f6c\u6362\u63cf\u8ff0\u7b26\u3002<br \/>\n * &#8211; iconv: \u6267\u884c\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\u3002<br \/>\n * &#8211; iconv_close: \u91ca\u653e\u8f6c\u6362\u63cf\u8ff0\u7b26\u8d44\u6e90\u3002<br \/>\n *<br \/>\n * &#064;example<br \/>\n * string input &#061; &#034;\u4f60\u597d&#034;;<br \/>\n * string output &#061; convert_encoding(input, &#034;GBK&#034;, &#034;UTF-8&#034;);<br \/>\n * std::cout &lt;&lt; output &lt;&lt; std::endl;<br \/>\n *\/<\/p>\n<p>string convert_encoding(const string&amp; input, const char* from_encoding, const char* to_encoding &#061; &#034;UTF-8&#034;) {<br \/>\n    \/**<br \/>\n     * iconv_t iconv_open (const char* tocode, const char* fromcode)<br \/>\n     * \u529f\u80fd: \u521d\u59cb\u5316\u4e00\u4e2a\u7528\u4e8e\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\u7684\u8f6c\u6362\u63cf\u8ff0\u7b26<br \/>\n     * \u53c2\u6570:<br \/>\n     *   const char* tocode: \u76ee\u6807\u7f16\u7801\u683c\u5f0f&#xff0c;\u4f8b\u5982 &#034;UTF-8&#034;<br \/>\n     *   const char* fromcode: \u6e90\u7f16\u7801\u683c\u5f0f&#xff0c;\u4f8b\u5982 &#034;GBK&#034;<br \/>\n     * \u8fd4\u56de\u503c:<br \/>\n     *   iconv_t: \u8f6c\u6362\u63cf\u8ff0\u7b26&#xff0c;\u7c7b\u578b\u4e3a void*&#xff0c;\u7528\u4e8e\u540e\u7eed\u7684\u7f16\u7801\u8f6c\u6362\u64cd\u4f5c<br \/>\n     *\/<br \/>\n    iconv_t cd &#061; iconv_open(to_encoding, from_encoding);<br \/>\n    if (cd &#061;&#061; (iconv_t)-1) {<br \/>\n        throw runtime_error(&#034;iconv \u521d\u59cb\u5316\u5931\u8d25&#034;);<br \/>\n    }<\/p>\n<p>    char* in_buf &#061; const_cast&lt;char*&gt;(input.data());<br \/>\n    size_t in_len &#061; input.size();<br \/>\n    string output(input.size() * 4, &#039;\\\\0&#039;);<br \/>\n    char* out_buf &#061; output.data();<br \/>\n    size_t out_len &#061; output.size();<\/p>\n<p>    \/**<br \/>\n     * size_t iconv (iconv_t cd, char** inbuf, size_t *inbytesleft, char** outbuf, size_t outbytesleft);<br \/>\n     * \u529f\u80fd: \u6267\u884c\u5b57\u7b26\u7f16\u7801\u8f6c\u6362&#xff0c;\u5c06\u8f93\u5165\u7f13\u51b2\u533a\u4e2d\u7684\u6570\u636e\u4ece\u6e90\u7f16\u7801\u8f6c\u6362\u4e3a\u76ee\u6807\u7f16\u7801\u3002<br \/>\n     * \u53c2\u6570:<br \/>\n     *   iconv_t cd: \u8f6c\u6362\u63cf\u8ff0\u7b26&#xff0c;\u7531 iconv_open \u521d\u59cb\u5316\u3002<br \/>\n     *   char** inbuf: \u8f93\u5165\u7f13\u51b2\u533a\u7684\u6307\u9488&#xff0c;\u6307\u5411\u5f85\u8f6c\u6362\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n     *   size_t *inbytesleft: \u8f93\u5165\u7f13\u51b2\u533a\u5269\u4f59\u7684\u5b57\u8282\u6570\u3002<br \/>\n     *   char** outbuf: \u8f93\u51fa\u7f13\u51b2\u533a\u7684\u6307\u9488&#xff0c;\u5b58\u50a8\u8f6c\u6362\u540e\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n     *   size_t *outbytesleft: \u8f93\u51fa\u7f13\u51b2\u533a\u5269\u4f59\u7684\u5b57\u8282\u6570\u3002<br \/>\n     * \u8fd4\u56de\u503c:<br \/>\n     *   size_t: \u6210\u529f\u8f6c\u6362\u7684\u5b57\u8282\u6570\u3002\u5982\u679c\u53d1\u751f\u9519\u8bef&#xff0c;\u8fd4\u56de (size_t)-1\u3002<br \/>\n     * \u6ce8\u610f:<br \/>\n     *   &#8211; \u8f6c\u6362\u8fc7\u7a0b\u4e2d&#xff0c;inbuf \u548c outbuf \u7684\u6307\u9488\u4f1a\u88ab\u66f4\u65b0&#xff0c;\u6307\u5411\u672a\u5904\u7406\u7684\u90e8\u5206\u3002<br \/>\n     *   &#8211; inbytesleft \u548c outbytesleft \u4f1a\u88ab\u66f4\u65b0&#xff0c;\u8868\u793a\u5269\u4f59\u7684\u5b57\u8282\u6570\u3002<br \/>\n     *\/<br \/>\n    if (iconv(cd, &amp;in_buf, &amp;in_len,&amp;out_buf, &amp;out_len) &#061;&#061; (size_t)-1) {<br \/>\n        iconv_close(cd);<br \/>\n        throw runtime_error(&#034;\u7f16\u7801\u8f6c\u6362\u5931\u8d25&#034;);<br \/>\n    }<br \/>\n    iconv_close(cd);<br \/>\n    output.resize(output.size() &#8211; out_len);<\/p>\n<p>    return output;<br \/>\n}<\/p>\n<p>struct Product {<br \/>\n    string name_gbk;    \/\/ gbk\u7f16\u7801\u5b57\u7b26\u4e32<br \/>\n    double price;<br \/>\n};<\/p>\n<p>\/\/ \u5e26\u7f16\u7801\u8f6c\u6362\u7684to_json<br \/>\nvoid to_json(json&amp; j, const Product&amp; p) {<br \/>\n    \/\/ GBK -&gt; UTF-8<br \/>\n    string utf8_name &#061; convert_encoding(p.name_gbk, &#034;GBK&#034;);<br \/>\n    j &#061; json{{&#034;name&#034;, utf8_name}, {&#034;price&#034;, p.price}};<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    Product product{&#034;IPhone&#034;, 4999};    \/\/ GBK\u5b58\u50a8<br \/>\n    try {<br \/>\n        json j &#061; product;<br \/>\n        cout &lt;&lt; &#034;JSON\u6570\u636e:\\\\n&#034; &lt;&lt; j.dump(4) &lt;&lt; endl;<br \/>\n    } catch (const exception&amp; e) {<br \/>\n        cerr &lt;&lt; &#034;\u9519\u8bef: &#034; &lt;&lt; e.what() &lt;&lt; endl;<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:left\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<h4 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u9519\u8bef\u5904\u7406\u7b56\u7565<\/span><\/h4>\n<h5 style=\"text-align:justify\"><span style=\"color:#365f91\">1. \u5f02\u5e38\u5904\u7406<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5f02\u5e38\u5904\u7406\u662fC&#043;&#043;\u4e2d\u5904\u7406\u9519\u8bef\u7684\u4e00\u79cd\u5e38\u89c1\u65b9\u5f0f\u3002\u5728\u89e3\u6790JSON\u548c\u6267\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u65f6&#xff0c;\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528try-catch\u5757\u6765\u6355\u83b7\u5e76\u5904\u7406\u5404\u79cd\u5f02\u5e38\u3002\u5f02\u5e38\u5904\u7406\u5e94\u8be5\u6070\u5f53\u5730\u4f7f\u7528&#xff0c;\u907f\u514d\u8fc7\u5ea6\u6355\u83b7\u5f02\u5e38&#xff0c;\u56e0\u4e3a\u8fd9\u53ef\u80fd\u4f1a\u9690\u85cf\u7a0b\u5e8f\u7684\u5176\u4ed6\u9519\u8bef\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5f02\u5e38\u5904\u7406\u7b56\u7565\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\u5e94\u8be5\u5305\u62ec\u5f02\u5e38\u7c7b\u578b\u7684\u9009\u62e9&#xff0c;\u5982\u4f55\u629b\u51fa\u5f02\u5e38&#xff0c;\u4ee5\u53ca\u5982\u4f55\u6070\u5f53\u5730\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u8fd8\u662f\u6765\u770b\u4ee3\u7801<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;nlohmann\/json.hpp&gt;<\/p>\n<p>using json &#061; nlohmann::json;<\/p>\n<p>\/\/ JSON\u6570\u636e\u5904\u7406\u51fd\u6570&#xff08;\u652f\u6301\u5d4c\u5957\u7ed3\u6784\u89e3\u6790&#xff09;<br \/>\nvoid process_json(const json&amp; j) {<br \/>\n    \/\/ \u9632\u5fa1\u6027\u68c0\u67e5&#xff1a;\u9a8c\u8bc1\u5fc5\u9700\u5b57\u6bb5\u5b58\u5728\u6027<br \/>\n    if (!j.contains(&#034;user&#034;) || !j[&#034;user&#034;].is_object()) {<br \/>\n        throw std::runtime_error(&#034;\u7f3a\u5931user\u5bf9\u8c61\u5b57\u6bb5&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ \u5d4c\u5957\u5bf9\u8c61\u89e3\u6790<br \/>\n    const auto&amp; user &#061; j[&#034;user&#034;];<br \/>\n    std::string username &#061; user.value(&#034;name&#034;, &#034;unknown&#034;); \/\/ \u5b89\u5168\u83b7\u53d6\u5b57\u6bb5<\/p>\n<p>    \/\/ \u7c7b\u578b\u4e25\u683c\u9a8c\u8bc1[3](&#064;ref)<br \/>\n    if (user[&#034;age&#034;].is_number()) {<br \/>\n        int age &#061; user[&#034;age&#034;];<br \/>\n        std::cout &lt;&lt; &#034;\u7528\u6237\u5e74\u9f84: &#034; &lt;&lt; age &lt;&lt; std::endl;<br \/>\n    } else {<br \/>\n        throw json::type_error::create(302, &#034;age\u5b57\u6bb5\u7c7b\u578b\u65e0\u6548&#034;, &amp;user);<br \/>\n    }<\/p>\n<p>    \/\/ \u6570\u7ec4\u5904\u7406[8](&#064;ref)<br \/>\n    if (j.contains(&#034;tags&#034;) &amp;&amp; j[&#034;tags&#034;].is_array()) {<br \/>\n        std::cout &lt;&lt; &#034;\u5173\u8054\u6807\u7b7e: &#034;;<br \/>\n        for (const auto&amp; tag : j[&#034;tags&#034;]) {<br \/>\n            std::cout &lt;&lt; tag.get&lt;std::string&gt;() &lt;&lt; &#034; &#034;;<br \/>\n        }<br \/>\n        std::cout &lt;&lt; std::endl;<br \/>\n    }<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ \u542b\u5d4c\u5957\u7ed3\u6784\u7684JSON\u6d4b\u8bd5\u6570\u636e<br \/>\n    std::string json_str &#061; R&#034;(<br \/>\n        {<br \/>\n            &#034;user&#034;: {<br \/>\n                &#034;name&#034;: &#034;John&#034;,<br \/>\n                &#034;age&#034;: &#034;thirty&#034;,  \/\/ \u6545\u610f\u8bbe\u7f6e\u9519\u8bef\u7c7b\u578b<br \/>\n                &#034;email&#034;: &#034;john&#064;example.com&#034;<br \/>\n            },<br \/>\n            &#034;tags&#034;: [&#034;tech&#034;, &#034;c&#043;&#043;&#034;, &#034;json&#034;]<br \/>\n        }<br \/>\n    )&#034;;<\/p>\n<p>    try {<br \/>\n        json j &#061; json::parse(json_str);  \/\/ \u53ef\u80fd\u629b\u51faparse_error<br \/>\n        process_json(j);<br \/>\n    }<br \/>\n    \/\/ \u5206\u5c42\u6355\u83b7\u5f02\u5e38\u7c7b\u578b<br \/>\n    catch (const json::parse_error&amp; e) {<br \/>\n        std::cerr &lt;&lt; &#034;[\u89e3\u6790\u9519\u8bef] \u5b57\u8282\u4f4d\u7f6e &#034; &lt;&lt; e.byte &lt;&lt; &#034; \u9519\u8bef\u8be6\u60c5: &#034; &lt;&lt; e.what() &lt;&lt; std::endl;<br \/>\n    } catch (const json::type_error&amp; e) {<br \/>\n        std::cerr &lt;&lt; &#034;[\u7c7b\u578b\u9519\u8bef] ID:&#034; &lt;&lt; e.id &lt;&lt; &#034; \u9519\u8bef\u4e0a\u4e0b\u6587: &#034; &lt;&lt; e.what() &lt;&lt; std::endl;<br \/>\n    } catch (const json::out_of_range&amp; e) {<br \/>\n        std::cerr &lt;&lt; &#034;[\u5b57\u6bb5\u7f3a\u5931] &#034; &lt;&lt; e.what() &lt;&lt; std::endl;<br \/>\n    } catch (const json::exception&amp; e) {<br \/>\n        std::cerr &lt;&lt; &#034;[JSON\u5f02\u5e38] \u9519\u8bef\u7801 &#034; &lt;&lt; e.id &lt;&lt; &#034; : &#034; &lt;&lt; e.what() &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<h5 style=\"text-align:justify\"><span style=\"color:#365f91\">2. \u56de\u8c03\u51fd\u6570<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u9664\u4e86\u8fd0\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236&#xff0c;\u8fd8\u53ef\u501f\u52a9<span style=\"background-color:yellow\"><span style=\"color:red\">\u9519\u8bef\u56de\u8c03\u51fd\u6570<\/span><\/span>\u6765\u5e94\u5bf9\u9519\u8bef\u60c5\u51b5\u3002\u6b64\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u81ea\u884c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570&#xff0c;\u4e00\u65e6\u51fa\u73b0\u9519\u8bef&#xff0c;\u8be5\u51fd\u6570\u4fbf\u4f1a\u6267\u884c\u4e00\u7cfb\u5217\u9884\u5148\u8bbe\u5b9a\u7684\u64cd\u4f5c&#xff0c;\u800c\u65e0\u9700\u4f9d\u8d56\u5f02\u5e38\u673a\u5236\u3002\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b&#xff0c;\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u80fd\u63d0\u5347\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387&#xff0c;\u8fd8\u80fd\u591f\u5904\u7406\u4e00\u4e9b\u5f02\u5e38\u5904\u7406\u96be\u4ee5\u5e94\u5bf9\u7684\u72b6\u51b5&#xff0c;\u6bd4\u5982\u5185\u5b58\u4e0d\u8db3\u95ee\u9898\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5728\u91c7\u7528\u9519\u8bef\u56de\u8c03\u51fd\u6570\u65f6&#xff0c;\u9996\u5148\u8981\u5b9a\u4e49\u4e13\u95e8\u7684\u9519\u8bef\u5904\u7406\u51fd\u6570&#xff0c;\u7136\u540e\u5728\u9519\u8bef\u53d1\u751f\u7684\u5730\u65b9\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f&#xff0c;\u8fd9\u4e2a\u56de\u8c03\u51fd\u6570\u5e94\u5177\u5907\u9ad8\u5ea6\u7684\u7075\u6d3b\u6027&#xff0c;\u80fd\u591f\u4f9d\u636e\u4e0d\u540c\u7c7b\u578b\u7684\u9519\u8bef&#xff0c;\u6267\u884c\u76f8\u5e94\u7684\u5dee\u5f02\u5316\u903b\u8f91\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6765\u770b\u4ee3\u7801:<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &lt;nlohmann\/json.hpp&gt;<br \/>\nusing json &#061; nlohmann::json;<br \/>\nusing namespace std;<br \/>\n\/\/ \u9519\u8bef\u7c7b\u578b\u679a\u4e3e<br \/>\nenum class ErrorType {<br \/>\n    ParseError,      \/\/ JSON\u89e3\u6790\u5931\u8d25<br \/>\n    FieldMissing,    \/\/ \u5b57\u6bb5\u7f3a\u5931<br \/>\n    TypeMismatch,    \/\/ \u7c7b\u578b\u4e0d\u5339\u914d<br \/>\n    InvalidValue     \/\/ \u503c\u975e\u6cd5<br \/>\n};<br \/>\n\/\/ \u9519\u8bef\u56de\u8c03\u51fd\u6570\u7c7b\u578b<br \/>\nusing ErrorBack &#061; function&lt;void(ErrorType, const string&amp;)&gt;;<\/p>\n<p>\/\/ \u9519\u8bef\u5904\u7406\u51fd\u6570(\u5168\u5c40)<br \/>\nvoid defaultErrorHandler(ErrorType type, const string&amp; msg) {<br \/>\n    const char* typeStr &#061; &#034;&#034;;<br \/>\n    switch (type) {<br \/>\n        case ErrorType::ParseError:     typeStr &#061; &#034;[\u89e3\u6790\u9519\u8bef]&#034;;     break;<br \/>\n        case ErrorType::FieldMissing:   typeStr &#061; &#034;[\u5b57\u6bb5\u7f3a\u5931]&#034;;     break;<br \/>\n        case ErrorType::TypeMismatch:   typeStr &#061; &#034;[\u7c7b\u578b\u9519\u8bef]&#034;;     break;<br \/>\n        case ErrorType::InvalidValue:   typeStr &#061; &#034;[\u975e\u6cd5\u503c]&#034;;      break;<br \/>\n    }<br \/>\n    cerr &lt;&lt; typeStr &lt;&lt; &#034; &#034; &lt;&lt; msg &lt;&lt; endl;<br \/>\n}<\/p>\n<p>\/\/ \u5e26\u9519\u8bef\u56de\u8c03\u7684JSON\u5904\u7406\u51fd\u6570<br \/>\nbool process_json(const string&amp; json_str, const ErrorBack&amp; cb &#061; defaultErrorHandler) {<br \/>\n    \/\/ \u89e3\u6790JSON \u7981\u7528\u5f02\u5e38<br \/>\n    auto ans &#061; json::parse(json_str, nullptr, false);<\/p>\n<p>    if (ans.is_discarded()) {<br \/>\n        cb(ErrorType::ParseError, &#034;JSON\u8bed\u6cd5\u9519\u8bef&#034;);<br \/>\n        return false;<br \/>\n    }<br \/>\n    const json&amp; j &#061; ans;<\/p>\n<p>    \/\/ \u5b57\u6bb5\u9a8c\u8bc1<br \/>\n    if (!j.contains(&#034;user&#034;) || !j[&#034;user&#034;].is_object()) {<br \/>\n        cb(ErrorType::FieldMissing, &#034;\u7f3a\u5931user\u5bf9\u8c61&#034;);<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    const auto&amp; user &#061; j[&#034;user&#034;];<br \/>\n    if (!user.contains(&#034;age&#034;) || !user[&#034;age&#034;].is_number()) {<br \/>\n        cb(ErrorType::TypeMismatch, &#034;user.age\u9700\u8981\u6570\u503c\u7c7b\u578b&#034;);<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    int age &#061; user[&#034;age&#034;];<br \/>\n    if (age &lt; 0 || age &gt; 150) {<br \/>\n        cb(ErrorType::InvalidValue, &#034;\u5e74\u9f84\u503c\u8d85\u51fa\u5408\u7406\u8303\u56f4: &#034; &#043; std::to_string(age));<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    \/\/ \u6b63\u5e38\u5904\u7406\u903b\u8f91&#8230;<\/p>\n<p>    cout &lt;&lt; &#034;\u9a8c\u8bc1\u901a\u8fc7&#xff0c;\u7528\u6237\u5e74\u9f84: &#034; &lt;&lt; age &lt;&lt; endl;<br \/>\n    return true;<br \/>\n}<\/p>\n<p>int main(int argc, char const *argv[])<br \/>\n{<br \/>\n     \/\/ \u6d4b\u8bd5\u7528\u4f8b1&#xff1a;\u5408\u6cd5\u6570\u636e<br \/>\n     std::string valid_json &#061; R&#034;({<br \/>\n        &#034;user&#034;: {&#034;name&#034;: &#034;John&#034;, &#034;age&#034;: 30}<br \/>\n    })&#034;;<br \/>\n    process_json(valid_json);<\/p>\n<p>    \/\/ \u6d4b\u8bd5\u7528\u4f8b2&#xff1a;\u975e\u6cd5\u5e74\u9f84\u503c<br \/>\n    std::string invalid_age &#061; R&#034;({<br \/>\n        &#034;user&#034;: {&#034;name&#034;: &#034;Alice&#034;, &#034;age&#034;: -5}<br \/>\n    })&#034;;<br \/>\n    process_json(invalid_age);<\/p>\n<p>    \/\/ \u6d4b\u8bd5\u7528\u4f8b3&#xff1a;\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406<br \/>\n    std::string missing_field &#061; R&#034;({&#034;profile&#034;: {}})&#034;;<br \/>\n    process_json(missing_field, [](ErrorType t, const std::string&amp; msg) {<br \/>\n        std::cerr &lt;&lt; &#034;\u81ea\u5b9a\u4e49\u5904\u7406 &#061;&gt; &#034; &lt;&lt; msg &lt;&lt; std::endl;<br \/>\n    });<br \/>\n    return 0;<br \/>\n}<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:left\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5728\u8fd9\u91cc&#xff0c;\u6211\u7684\u4ee3\u7801\u662f\u901a\u8fc7GBK\u6765\u4fdd\u5b58\u7684<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"97\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220624-6855db60501ac.png\" width=\"482\" \/><\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u8fd0\u884c\u6548\u679c\u5982\u4e0b:<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"79\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220624-6855db605c542.jpg\" width=\"554\" \/><\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5982\u679c\u5f00\u59cb\u4fdd\u5b58\u65b9\u5f0f\u4e0d\u5bf9&#xff0c;\u5219\u4f1a\u662f:<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"78\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220624-6855db606b666.jpg\" width=\"554\" \/><\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<h5 style=\"background-color:transparent;margin-left:0px;margin-right:0px;text-align:justify\"><span style=\"color:#365f91\">3. \u65e5\u5fd7\u8bb0\u5f55\u4e0e\u9519\u8bef\u62a5\u544a<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u65e5\u5fd7\u8bb0\u5f55\u5728\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u8d77\u7740\u5173\u952e\u4f5c\u7528&#xff0c;\u5b83\u5982\u540c\u4e3a\u5f00\u53d1\u8005\u914d\u5907\u7684 \u201c\u900f\u89c6\u955c\u201d&#xff0c;\u80fd\u591f\u7cbe\u51c6\u8ffd\u8e2a\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001&#xff0c;\u52a9\u529b\u9ad8\u6548\u6392\u67e5\u95ee\u9898\u3002\u5728\u9519\u8bef\u5904\u7406\u673a\u5236\u91cc&#xff0c;\u5c06\u9519\u8bef\u4fe1\u606f\u8bb0\u5f55\u81f3\u65e5\u5fd7\u6587\u4ef6&#xff0c;\u6216\u8005\u76f4\u63a5\u8f93\u51fa\u5230\u63a7\u5236\u53f0&#xff0c;\u662f\u4e00\u9879\u4e0d\u53ef\u6216\u7f3a\u7684\u57fa\u672c\u8981\u6c42\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u4e00\u4efd\u5b8c\u6574\u7684\u9519\u8bef\u62a5\u544a&#xff0c;\u901a\u5e38\u6db5\u76d6\u591a\u4e2a\u5173\u952e\u8981\u7d20\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u9996\u5148\u662f\u9519\u8bef\u53d1\u751f\u7684\u7cbe\u786e\u65f6\u95f4&#xff0c;\u5b83\u80fd\u4e3a\u95ee\u9898\u8ffd\u6eaf\u63d0\u4f9b\u65f6\u95f4\u8f74\u7ebf\u7d22&#xff1b;<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u9519\u8bef\u7c7b\u578b\u660e\u786e\u6307\u51fa\u95ee\u9898\u7684\u6027\u8d28&#xff0c;\u65b9\u4fbf\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u65b9\u5411&#xff1b;<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u9519\u8bef\u63cf\u8ff0\u5219\u8be6\u7ec6\u9610\u8ff0\u95ee\u9898\u7ec6\u8282&#xff0c;\u5e2e\u52a9\u5f00\u53d1\u8005\u6df1\u5165\u7406\u89e3\u95ee\u9898\u6240\u5728&#xff1b;<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u8c03\u7528\u5806\u6808\u4fe1\u606f\u5c31\u50cf\u4e00\u5f20 \u201c\u5bfc\u822a\u56fe\u201d&#xff0c;\u6e05\u6670\u5c55\u793a\u7a0b\u5e8f\u6267\u884c\u8def\u5f84&#xff0c;\u8f85\u52a9\u5206\u6790\u9519\u8bef\u4ea7\u751f\u7684\u8fc7\u7a0b&#xff1b;<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5f71\u54cd\u8303\u56f4\u754c\u5b9a\u4e86\u9519\u8bef\u6ce2\u53ca\u7684\u533a\u57df&#xff0c;\u6709\u52a9\u4e8e\u8bc4\u4f30\u95ee\u9898\u7684\u4e25\u91cd\u7a0b\u5ea6\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u65e5\u5fd7\u7ea7\u522b\u662f\u5bf9\u65e5\u5fd7\u4fe1\u606f\u8fdb\u884c\u5206\u7c7b\u7ba1\u7406\u7684\u91cd\u8981\u65b9\u5f0f&#xff0c;\u5e38\u89c1\u7684\u5305\u62ec ERROR&#xff08;\u9519\u8bef&#xff09;\u3001WARN&#xff08;\u8b66\u544a&#xff09;\u3001INFO&#xff08;\u4fe1\u606f&#xff09;\u3001DEBUG&#xff08;\u8c03\u8bd5&#xff09;\u7b49\u3002\u4f9d\u636e\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b&#xff0c;\u7cfb\u7edf\u4f1a\u6709\u9488\u5bf9\u6027\u5730\u8bb0\u5f55\u76f8\u5e94\u7ea7\u522b\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u4ee5\u5904\u7406 JSON \u6570\u636e\u4e3a\u4f8b&#xff0c;\u4e00\u65e6\u51fa\u73b0\u9519\u8bef&#xff0c;\u9664\u4e86\u8bb0\u5f55\u5e38\u89c1\u7684\u9519\u8bef\u4fe1\u606f\u5916&#xff0c;\u8fd8\u5e94\u7279\u522b\u5173\u6ce8\u9519\u8bef\u53d1\u751f\u7684\u5177\u4f53\u4f4d\u7f6e&#xff0c;\u8fd9\u5982\u540c\u5728\u7a0b\u5e8f\u4ee3\u7801 \u201c\u5730\u56fe\u201d \u4e0a\u7cbe\u51c6\u6807\u6ce8\u51fa\u95ee\u9898\u70b9&#xff1b;\u8be6\u7ec6\u7684\u9519\u8bef\u5177\u4f53\u63cf\u8ff0&#xff0c;\u786e\u4fdd\u5bf9\u9519\u8bef\u6709\u5168\u9762\u6e05\u6670\u7684\u8ba4\u8bc6&#xff1b;\u4ee5\u53ca\u9519\u8bef\u4e0a\u4e0b\u6587\u4fe1\u606f&#xff0c;\u5b83\u4e3a\u7406\u89e3\u9519\u8bef\u5728\u7279\u5b9a\u73af\u5883\u4e0b\u4ea7\u751f\u7684\u539f\u56e0\u63d0\u4f9b\u4e86\u5173\u952e\u7ebf\u7d22\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u8fd8\u662f\u6765\u770b\u4ee3\u7801:<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;fstream&gt;<br \/>\n#include &lt;ctime&gt;<br \/>\n#include &lt;iomanip&gt;<br \/>\n#include &lt;nlohmann\/json.hpp&gt;<br \/>\n#include &lt;mutex&gt;<br \/>\n#include &lt;sstream&gt;        \/\/ \u5b57\u7b26\u4e32\u6d41\u5904\u7406<br \/>\n#include &lt;cstring&gt;        \/\/ \u7528\u4e8estrerror\u51fd\u6570<\/p>\n<p>using json &#061; nlohmann::json;<\/p>\n<p>using namespace std;<\/p>\n<p>\/\/ \u65e5\u5fd7\u679a\u4e3e<br \/>\nenum LogLevel {<br \/>\n    DEBUG,<br \/>\n    INFO,<br \/>\n    WARN,<br \/>\n    ERROR<br \/>\n};<br \/>\n\/\/ DEBUG: \u8c03\u8bd5\u4fe1\u606f&#xff0c;INFO: \u5e38\u89c4\u4fe1\u606f&#xff0c;WARN: \u8b66\u544a&#xff0c;ERROR: \u9519\u8bef<\/p>\n<p>\/\/ \u5355\u4f8b\u65e5\u5fd7<br \/>\nclass Logger {<br \/>\nprivate:<br \/>\n    Logger(\/* args *\/) &#061; default;   \/\/ \u7981\u6b62\u5916\u90e8\u6784\u9020<br \/>\n    ~Logger() {<br \/>\n        if (logFile_.is_open())     logFile_.close();<br \/>\n    }<br \/>\n    Logger(const Logger&amp;) &#061; delete;<br \/>\n    Logger&amp; operator&#061;(const Logger&amp;) &#061; delete;<\/p>\n<p>    \/\/ \u8f6c\u6362\u65e5\u5fd7\u7ea7\u522b\u4e3a\u5b57\u7b26\u4e32<br \/>\n    string levelToString(LogLevel level) {<br \/>\n        switch (level) {<br \/>\n            case DEBUG:     return &#034;DEBUG&#034;;<br \/>\n            case INFO:      return &#034;INFO&#034;;<br \/>\n            case WARN:      return &#034;WARN&#034;;<br \/>\n            case ERROR:     return &#034;ERROR&#034;;<br \/>\n            default:        return &#034;UNKNOWN&#034;;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/* \u5b57\u6bb5 *\/<br \/>\n    ofstream logFile_;  \/\/ \u65e5\u5fd7\u6587\u4ef6\u6d41<br \/>\n    LogLevel curLevel_; \/\/ \u5f53\u524d\u7684\u65e5\u5fd7\u7ea7\u522b<br \/>\n    mutex mutex_;       \/\/ \u4e92\u65a5\u9501\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168<br \/>\npublic:<br \/>\n    \/\/ \u5355\u4f8b\u5b9e\u4f8b(\u7ebf\u7a0b\u5b89\u5168)<br \/>\n    static Logger&amp; getInstance() {<br \/>\n        \/\/ c&#043;&#043; 11 \u4fdd\u8bc1\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u521d\u59cb\u5316 \u7ebf\u7a0b\u5b89\u5168<br \/>\n        static Logger instance;<br \/>\n        return instance;<br \/>\n    }<\/p>\n<p>    \/\/ \u521d\u59cb\u5316\u65e5\u5fd7\u7cfb\u7edf<br \/>\n    \/**<br \/>\n     * &#064;brief \u521d\u59cb\u5316\u65e5\u5fd7\u7cfb\u7edf&#xff0c;\u8bbe\u7f6e\u65e5\u5fd7\u6587\u4ef6\u548c\u65e5\u5fd7\u7ea7\u522b\u3002<br \/>\n     *<br \/>\n     * \u6b64\u51fd\u6570\u7528\u4e8e\u6253\u5f00\u6307\u5b9a\u7684\u65e5\u5fd7\u6587\u4ef6&#xff0c;\u5e76\u8bbe\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u7684\u6700\u4f4e\u7ea7\u522b\u3002<br \/>\n     * \u5982\u679c\u65e5\u5fd7\u6587\u4ef6\u65e0\u6cd5\u6253\u5f00&#xff0c;\u4f1a\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u5230\u6807\u51c6\u9519\u8bef\u6d41\u3002<br \/>\n     *  enum _Ios_Openmode<br \/>\n    {<br \/>\n      _S_app        &#061; 1L &lt;&lt; 0,<br \/>\n      _S_ate        &#061; 1L &lt;&lt; 1,<br \/>\n      _S_bin        &#061; 1L &lt;&lt; 2,<br \/>\n      _S_in         &#061; 1L &lt;&lt; 3,<br \/>\n      _S_out        &#061; 1L &lt;&lt; 4,<br \/>\n      _S_trunc      &#061; 1L &lt;&lt; 5,<br \/>\n      _S_noreplace  &#061; 1L &lt;&lt; 6,<br \/>\n      _S_ios_openmode_end &#061; 1L &lt;&lt; 16,<br \/>\n      _S_ios_openmode_max &#061; __INT_MAX__,<br \/>\n      _S_ios_openmode_min &#061; ~__INT_MAX__<br \/>\n    };<br \/>\n     *<br \/>\n     * &#064;param filename \u65e5\u5fd7\u6587\u4ef6\u540d&#xff0c;\u9ed8\u8ba4\u4e3a &#034;Log.log&#034;\u3002<br \/>\n     * &#064;param level \u65e5\u5fd7\u7ea7\u522b&#xff0c;\u9ed8\u8ba4\u4e3a INFO\u3002<br \/>\n     *\/<br \/>\n    void init(const string&amp; filename &#061; &#034;Log.log&#034;, LogLevel level &#061; INFO) {<br \/>\n        lock_guard&lt;mutex&gt; lock(mutex_); \/\/ \u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168<br \/>\n        \/\/ \u6253\u5f00\u65e5\u5fd7\u6587\u4ef6&#xff0c;\u6a21\u5f0f\u4e3a ios::out&#xff08;\u8f93\u51fa\u6a21\u5f0f&#xff09;\u548c ios::app&#xff08;\u8ffd\u52a0\u6a21\u5f0f&#xff09;<br \/>\n        logFile_.open(filename, ios::out | ios::app);<br \/>\n        if (!logFile_.is_open()) {<br \/>\n            \/\/ \u5982\u679c\u65e5\u5fd7\u6587\u4ef6\u6253\u5f00\u5931\u8d25&#xff0c;\u8f93\u51fa\u9519\u8bef\u4fe1\u606f<br \/>\n            cerr &lt;&lt; &#034;\u65e5\u5fd7\u6587\u4ef6\u6253\u5f00\u5931\u8d25: &#034; &lt;&lt; filename &lt;&lt; &#034; \u9519\u8bef: &#034; &lt;&lt; strerror(errno) &lt;&lt; endl;<br \/>\n        }<br \/>\n        \/\/ \u8bbe\u7f6e\u5f53\u524d\u65e5\u5fd7\u7ea7\u522b<br \/>\n        curLevel_ &#061; level;<br \/>\n    }<\/p>\n<p>    \/\/ \u8bb0\u5f55\u65e5\u5fd7 \u6838\u5fc3<br \/>\n    \/**<br \/>\n     * &#064;brief \u8bb0\u5f55\u65e5\u5fd7\u4fe1\u606f\u5230\u63a7\u5236\u53f0\u548c\u6587\u4ef6\u3002<br \/>\n     *<br \/>\n     * \u6b64\u51fd\u6570\u6839\u636e\u65e5\u5fd7\u7ea7\u522b\u8fc7\u6ee4\u65e5\u5fd7\u4fe1\u606f&#xff0c;\u5e76\u5c06\u65e5\u5fd7\u4fe1\u606f\u683c\u5f0f\u5316\u540e\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u548c\u65e5\u5fd7\u6587\u4ef6\u3002<br \/>\n     * \u65e5\u5fd7\u683c\u5f0f\u4e3a&#xff1a;[\u65f6\u95f4\u6233][\u65e5\u5fd7\u7ea7\u522b] \u6587\u4ef6\u540d:\u884c\u53f7 &#8211; \u65e5\u5fd7\u6d88\u606f\u3002<br \/>\n     *<br \/>\n     * &#064;param level \u65e5\u5fd7\u7ea7\u522b&#xff08;DEBUG, INFO, WARN, ERROR&#xff09;\u3002<br \/>\n     * &#064;param message \u65e5\u5fd7\u6d88\u606f\u5185\u5bb9\u3002<br \/>\n     * &#064;param file \u8bb0\u5f55\u65e5\u5fd7\u7684\u6e90\u6587\u4ef6\u540d&#xff08;\u9ed8\u8ba4\u4f7f\u7528\u9884\u5b9a\u4e49\u5b8f __FILE__&#xff09;\u3002<br \/>\n     * &#064;param line \u8bb0\u5f55\u65e5\u5fd7\u7684\u884c\u53f7&#xff08;\u9ed8\u8ba4\u4f7f\u7528\u9884\u5b9a\u4e49\u5b8f __LINE__&#xff09;\u3002<br \/>\n     *\/<br \/>\n    void log(LogLevel level, const string&amp; message, const char* file &#061; __FILE__, int line &#061; __LINE__) {<br \/>\n        \/\/ \u5982\u679c\u65e5\u5fd7\u7ea7\u522b\u4f4e\u4e8e\u5f53\u524d\u8bbe\u7f6e\u7684\u65e5\u5fd7\u7ea7\u522b&#xff0c;\u5219\u76f4\u63a5\u8fd4\u56de<br \/>\n        if (level &lt; curLevel_)  return;     <\/p>\n<p>        \/\/ \u4f7f\u7528\u4e92\u65a5\u9501\u4fdd\u8bc1\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u7ebf\u7a0b\u5b89\u5168<br \/>\n        lock_guard&lt;mutex&gt; lock(mutex_);     <\/p>\n<p>        \/\/ \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233<br \/>\n        auto now &#061; chrono::system_clock::now();<br \/>\n        time_t now_time &#061; chrono::system_clock::to_time_t(now);<br \/>\n        tm now_tm &#061; *localtime(&amp;now_time);<\/p>\n<p>        \/\/ \u683c\u5f0f\u5316\u65e5\u5fd7\u4fe1\u606f<br \/>\n        ostringstream oss;<br \/>\n        oss &lt;&lt; &#034;[&#034; &lt;&lt; put_time(&amp;now_tm, &#034;%Y-%m-%d %H:%M:%S&#034;) &lt;&lt; &#034;]&#034; \/\/ \u65f6\u95f4\u6233<br \/>\n            &lt;&lt; &#034;[&#034; &lt;&lt; levelToString(level) &lt;&lt; &#034;] &#034;                  \/\/ \u65e5\u5fd7\u7ea7\u522b<br \/>\n            &lt;&lt; file &lt;&lt; &#034;:&#034; &lt;&lt; line &lt;&lt; &#034; &#8211; &#034;                        \/\/ \u6587\u4ef6\u540d\u548c\u884c\u53f7<br \/>\n            &lt;&lt; message;                                            \/\/ \u65e5\u5fd7\u6d88\u606f<br \/>\n        const string logMsg &#061; oss.str();<\/p>\n<p>        \/\/ \u8f93\u51fa\u5230\u63a7\u5236\u53f0&#xff08;DEBUG \u7ea7\u522b\u4f7f\u7528\u9752\u8272\u663e\u793a&#xff09;<br \/>\n        if (level &#061;&#061; DEBUG)     cout &lt;&lt; &#034;\\\\033[36m&#034;;     \/\/ \u9752\u8272<br \/>\n        cout &lt;&lt; logMsg &lt;&lt; &#034;\\\\033[0m&#034; &lt;&lt; endl;    <\/p>\n<p>        \/\/ \u5982\u679c\u65e5\u5fd7\u6587\u4ef6\u5df2\u6253\u5f00&#xff0c;\u5c06\u65e5\u5fd7\u5199\u5165\u6587\u4ef6<br \/>\n        if (logFile_.is_open()) {<br \/>\n            logFile_ &lt;&lt; logMsg &lt;&lt; endl;<br \/>\n            \/\/ \u7acb\u5373\u5237\u65b0\u7f13\u51b2\u533a&#xff0c;\u786e\u4fdd\u65e5\u5fd7\u6570\u636e\u4e0d\u4f1a\u4e22\u5931<br \/>\n            logFile_.flush();<br \/>\n        }<br \/>\n    }<br \/>\n};<\/p>\n<p>\/\/ \u5b8f<br \/>\n#define LOG_DEBUG(msg) Logger::getInstance().log(DEBUG, msg, __FILE__, __LINE__)<br \/>\n#define LOG_INFO(msg) Logger::getInstance().log(INFO, msg, __FILE__, __LINE__)<br \/>\n#define LOG_WARN(msg) Logger::getInstance().log(WARN, msg, __FILE__, __LINE__)<br \/>\n#define LOG_ERROR(msg) Logger::getInstance().log(ERROR, msg, __FILE__, __LINE__)<\/p>\n<p>\/\/ \u83b7\u53d6JSON\u7684\u9519\u8bef\u4e0a\u4e0b\u6587<br \/>\n\/**<br \/>\n * &#064;brief \u83b7\u53d6 JSON \u5b57\u7b26\u4e32\u4e2d\u9519\u8bef\u4f4d\u7f6e\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002<br \/>\n *<br \/>\n * \u6b64\u51fd\u6570\u7528\u4e8e\u4ece\u7ed9\u5b9a\u7684 JSON \u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u9519\u8bef\u4f4d\u7f6e\u9644\u8fd1\u7684\u4e00\u6bb5\u4e0a\u4e0b\u6587\u4fe1\u606f&#xff0c;<br \/>\n * \u4ee5\u4fbf\u66f4\u597d\u5730\u7406\u89e3\u9519\u8bef\u53d1\u751f\u7684\u5177\u4f53\u4f4d\u7f6e\u548c\u5185\u5bb9\u3002<br \/>\n *<br \/>\n * &#064;param json_str \u8f93\u5165\u7684 JSON \u5b57\u7b26\u4e32\u3002<br \/>\n * &#064;param error_pos \u9519\u8bef\u53d1\u751f\u7684\u4f4d\u7f6e&#xff08;\u7d22\u5f15&#xff09;\u3002<br \/>\n * &#064;return string \u8fd4\u56de\u5305\u542b\u9519\u8bef\u4f4d\u7f6e\u4e0a\u4e0b\u6587\u7684\u5b57\u7b26\u4e32&#xff0c;\u683c\u5f0f\u4e3a &#034;&#8230;\u4e0a\u4e0b\u6587\u5185\u5bb9&#8230;&#034;\u3002<br \/>\n *         \u5982\u679c\u9519\u8bef\u4f4d\u7f6e\u5728\u5b57\u7b26\u4e32\u7684\u5f00\u5934\u6216\u7ed3\u5c3e&#xff0c;\u53ef\u80fd\u4f1a\u7701\u7565\u90e8\u5206\u4e0a\u4e0b\u6587\u3002<br \/>\n *\/<br \/>\nstring get_json_context(const string&amp; json_str, size_t error_pos) {<br \/>\n    const size_t context_size &#061; 0;<br \/>\n    size_t start &#061; (error_pos &gt; context_size) ? error_pos &#8211; context_size : 0;   \/\/ \u672a\u8d8a\u754c\u5c31\u662f\u76f4\u63a5\u76f8\u51cf<br \/>\n    size_t end &#061; min(error_pos &#043; context_size, json_str.size());<br \/>\n    return &#034;&#8230;&#034; &#043; json_str.substr(start, end &#8211; start) &#043; &#034;&#8230;&#034;;<br \/>\n}<\/p>\n<p>int main(int argc, char const *argv[])<br \/>\n{<br \/>\n    \/\/ \u521d\u59cb\u5316<br \/>\n    Logger::getInstance().init(&#034;runtime.log&#034;, DEBUG);<br \/>\n\/\/ (const char [101])&#034;{\\\\n        \\\\&#034;name\\\\&#034;: \\\\&#034;Alice\\\\&#034;,\\\\n        \\\\&#034;age\\\\&#034;: \\\\&#034;thirty\\\\&#034;,  \\\\n        \\\\&#034;address\\\\&#034;: { \\\\&#034;city\\\\&#034;: \\\\&#034;Beijing\\\\&#034; }\\\\n    }&#034;<br \/>\n    string json_str &#061; R&#034;({<br \/>\n        &#034;name&#034;: &#034;Alice&#034;,<br \/>\n        &#034;age&#034;: &#034;thirty&#034;,<br \/>\n        &#034;address&#034;: { &#034;city&#034;: &#034;Beijing&#034; }<br \/>\n    })&#034;;<br \/>\n    try {<br \/>\n        json j &#061; json::parse(json_str);<br \/>\n        \/\/ \u6570\u636e\u6821\u9a8c<br \/>\n        if (!j[&#034;age&#034;].is_number()) {<br \/>\n            LOG_WARN(&#034;\u7c7b\u578b\u5f02\u5e38: age\u5b57\u6bb5\u5e94\u4e3a\u6570\u503c\u7c7b\u578b&#xff0c;\u5f53\u524d\u503c: &#034; &#043; j[&#034;age&#034;].dump());<br \/>\n            j[&#034;age&#034;] &#061; 30;  \/\/ \u81ea\u52a8\u4fee\u6b63\u793a\u4f8b<br \/>\n        }<\/p>\n<p>        LOG_INFO(&#034;JSON\u89e3\u6790\u6210\u529f&#xff0c;\u7528\u6237: &#034; &#043; j[&#034;name&#034;].get&lt;string&gt;());<\/p>\n<p>    } catch (const json::parse_error&amp; e) {<br \/>\n        \/\/ \u8be6\u7ec6\u9519\u8bef\u65e5\u5fd7<br \/>\n        ostringstream errorMsg;<br \/>\n        errorMsg &lt;&lt; &#034;JSON\u89e3\u6790\u5931\u8d25: &#034; &lt;&lt; e.what() &lt;&lt; &#034;\\\\n&#034;<br \/>\n                 &lt;&lt; &#034;\u9519\u8bef\u4f4d\u7f6e: \u5b57\u8282\u504f\u79fb &#034; &lt;&lt; e.byte &lt;&lt; &#034;\\\\n&#034;<br \/>\n                 &lt;&lt; &#034;\u9519\u8bef\u4e0a\u4e0b\u6587: &#034; &lt;&lt; get_json_context(json_str, e.byte);<br \/>\n        LOG_ERROR(errorMsg.str());<br \/>\n    } catch (const exception&amp; e) {<br \/>\n        LOG_ERROR(&#034;\u672a\u6355\u83b7\u5f02\u5e38: &#034; &#043; string(e.what()));<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:left\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u8ffd\u52a0\u6a21\u5f0f\u5199\u5165\u4fdd\u8bc1\u4e0d\u8986\u76d6\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u4e86\u89e3\u9519\u8bef\u53d1\u751f\u7684\u4e0a\u4e0b\u6587&#xff0c;\u4fbf\u4e8e\u540e\u7eed\u95ee\u9898\u5206\u6790\u548c\u89e3\u51b3\u3002<\/p>\n<h3 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">JSON Schema\u9a8c\u8bc1\u548cC&#043;&#043; 11 \u7279\u6027\u5e94\u7528<\/span><\/h3>\n<h4 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">JSON Schema\u9a8c\u8bc1\u673a\u5236<\/span><\/h4>\n<h5 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">JSON Schema\u6982\u5ff5<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">JSON Schema \u662f\u7528\u4e8e\u63cf\u8ff0 JSON \u6570\u636e\u7ed3\u6784\u7684\u5143\u6570\u636e\u6807\u51c6&#xff0c;\u6838\u5fc3\u529f\u80fd\u662f\u9a8c\u8bc1 JSON \u6570\u636e\u662f\u5426\u7b26\u5408\u9884\u8bbe\u6a21\u5f0f&#xff0c;\u9632\u6b62\u6570\u636e\u89e3\u6790\u9519\u8bef\u3002Schema \u672c\u8eab\u4ee5 JSON \u5bf9\u8c61\u5f62\u5f0f\u5b58\u5728&#xff0c;\u53ef\u5b9a\u4e49\u6570\u636e\u7ed3\u6784\u3001\u7c7b\u578b\u3001\u53d6\u503c\u8303\u56f4\u3001\u9ed8\u8ba4\u503c\u7b49\u89c4\u5219&#xff0c;\u786e\u4fdd\u6570\u636e\u683c\u5f0f\u6b63\u786e\u4e0e\u4e00\u81f4\u6027\u3002<\/p>\n<h5 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">C&#043;&#043; \u5b9e\u73b0JSON Schema\u9a8c\u8bc1<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5728 C&#043;&#043; \u4e2d\u5b9e\u73b0 JSON Schema \u9a8c\u8bc1&#xff0c;\u53ef\u501f\u52a9\u7b2c\u4e09\u65b9\u5e93&#xff0c;\u8fd8\u662fnlohmann\/json\u3002nlohmann\/json\u7528\u4e8e\u5c06JSON Schema\u89e3\u6790\u4e3a\u4e00\u4e2a\u9a8c\u8bc1\u5668&#xff0c;\u7136\u540e\u4f7f\u7528\u9a8c\u8bc1\u5668\u6765\u6821\u9a8c\u5177\u4f53\u7684JSON\u6570\u636e<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u4e5f\u6709\u4e24\u79cd\u65b9\u5f0f \u624b\u52a8\u57fa\u7840\u6821\u9a8c\u548c \u62d3\u5c55\u5e93\u5b9e\u73b0\u81ea\u52a8\u6821\u9a8c<\/p>\n<li style=\"text-align:justify\">\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 nlohmann\/json \u89e3\u6790 JSON \u6570\u636e\u540e&#xff0c;\u6839\u636e Schema \u89c4\u5219\u9010\u9879\u68c0\u67e5\u5b57\u6bb5\u7c7b\u578b\u3001\u5fc5\u586b\u9879\u7b49\u7ea6\u675f\u3002<\/li>\n<li style=\"text-align:justify\">\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u00a0json\u00a0\u7c7b\u6dfb\u52a0\u6821\u9a8c\u65b9\u6cd5&#xff0c;\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u94fe\u5f0f\u8c03\u7528\u3002<\/li>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6765\u770b\u4ee3\u7801<\/p>\n<p>#include &lt;nlohmann\/json.hpp&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;stdexcept&gt;<\/p>\n<p>using namespace std;<br \/>\nusing json &#061; nlohmann::json;<\/p>\n<p>\/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u65b9\u6cd51&#xff1a;\u624b\u52a8\u6821\u9a8c\u51fd\u6570 &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n\/\/ \u624b\u52a8\u6821\u9a8c\u51fd\u6570       \u6839\u636e Schema \u89c4\u5219\u9010\u9879\u68c0\u67e5\u5b57\u6bb5\u7c7b\u578b\u3001\u5fc5\u586b\u9879\u7b49\u7ea6\u675f \u5b9e\u73b0\u57fa\u7840\u6821\u9a8c<br \/>\nbool validateSchema(const json&amp; data, const json&amp; schema, string &amp;error) {<br \/>\n    \/\/ \u68c0\u67e5\u9876\u5c42\u5bf9\u8c61\u7684\u7c7b\u578b\u662f\u5426\u5339\u914d<br \/>\n    if (schema.contains(&#034;type&#034;) &amp;&amp; schema[&#034;type&#034;] !&#061; data.type_name()) {<br \/>\n        error &#061; &#034;\u7c7b\u578b\u4e0d\u5339\u914d&#xff0c;\u671f\u671b\u7c7b\u578b: &#034; &#043; schema[&#034;type&#034;].get&lt;string&gt;();<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    \/\/ \u68c0\u67e5\u5fc5\u586b\u5b57\u6bb5\u662f\u5426\u5b58\u5728<br \/>\n    if (schema.contains(&#034;required&#034;)) {<br \/>\n        for (const auto&amp; field : schema[&#034;required&#034;]) {<br \/>\n            \/\/ \u5982\u679c\u5fc5\u586b\u5b57\u6bb5\u5728\u6570\u636e\u4e2d\u4e0d\u5b58\u5728&#xff0c;\u5219\u8fd4\u56de\u9519\u8bef<br \/>\n            if (!data.contains(field)) {<br \/>\n                error &#061; &#034;\u7f3a\u5c11\u5fc5\u586b\u5b57\u6bb5: &#034; &#043; field.get&lt;std::string&gt;();<br \/>\n                return false;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u68c0\u67e5\u5bf9\u8c61\u7684\u5c5e\u6027\u662f\u5426\u7b26\u5408 Schema \u5b9a\u4e49<br \/>\n    if (schema.contains(&#034;properties&#034;)) {<br \/>\n        for (const auto&amp; [key, propSchema] : schema[&#034;properties&#034;].items()) {<br \/>\n            \/\/ \u5982\u679c\u5b57\u6bb5\u662f\u53ef\u9009\u7684\u4e14\u6570\u636e\u4e2d\u4e0d\u5b58\u5728&#xff0c;\u5219\u8df3\u8fc7\u68c0\u67e5<br \/>\n            if (!data.contains(key))<br \/>\n                continue;<\/p>\n<p>            \/\/ \u68c0\u67e5\u5b57\u6bb5\u7684\u7c7b\u578b\u662f\u5426\u5339\u914d<br \/>\n            if (propSchema.contains(&#034;type&#034;)) {<br \/>\n                \/\/ \u5982\u679c\u5b57\u6bb5\u7c7b\u578b\u4e0e Schema \u5b9a\u4e49\u7684\u7c7b\u578b\u4e0d\u5339\u914d&#xff0c;\u5219\u8fd4\u56de\u9519\u8bef<br \/>\n                if (data[key].type_name() !&#061; propSchema[&#034;type&#034;])    {<br \/>\n                    error &#061; &#034;\u5b57\u6bb5 &#039;&#034; &#043; key &#043; &#034;&#039; \u7c7b\u578b\u9519\u8bef&#xff0c;\u671f\u671b\u7c7b\u578b: &#034; &#043; propSchema[&#034;type&#034;].get&lt;string&gt;();<br \/>\n                    return false;<br \/>\n                }<br \/>\n            }<\/p>\n<p>            \/\/ \u68c0\u67e5\u6570\u503c\u5b57\u6bb5\u7684\u6700\u5c0f\u503c\u7ea6\u675f<br \/>\n            if (propSchema.contains(&#034;minimum&#034;) &amp;&amp; data[key].is_number()) {<br \/>\n                \/\/ \u5982\u679c\u5b57\u6bb5\u503c\u5c0f\u4e8e Schema \u5b9a\u4e49\u7684\u6700\u5c0f\u503c&#xff0c;\u5219\u8fd4\u56de\u9519\u8bef<br \/>\n                if (data[key] &lt; propSchema[&#034;minimum&#034;]) {<br \/>\n                    error &#061; &#034;\u5b57\u6bb5 &#039;&#034; &#043; key &#043; &#034;&#039; \u7684\u503c\u5fc5\u987b &gt;&#061; &#034; &#043; propSchema[&#034;minimum&#034;].dump();<br \/>\n                    return false;<br \/>\n                }<br \/>\n            }<\/p>\n<p>            \/\/ \u68c0\u67e5\u5b57\u6bb5\u503c\u662f\u5426\u5728\u679a\u4e3e\u8303\u56f4\u5185<br \/>\n            if (propSchema.contains(&#034;enum&#034;)) {<br \/>\n                bool isValid &#061; false;<br \/>\n                \/\/ \u904d\u5386\u679a\u4e3e\u503c&#xff0c;\u68c0\u67e5\u5b57\u6bb5\u503c\u662f\u5426\u5339\u914d\u5176\u4e2d\u4e4b\u4e00<br \/>\n                for (const auto&amp; val : propSchema[&#034;enum&#034;]) {<br \/>\n                    if (data[key] &#061;&#061; val)<br \/>\n                        isValid &#061; true;<br \/>\n                }<br \/>\n                \/\/ \u5982\u679c\u5b57\u6bb5\u503c\u4e0d\u5728\u679a\u4e3e\u8303\u56f4\u5185&#xff0c;\u5219\u8fd4\u56de\u9519\u8bef<br \/>\n                if (!isValid) {<br \/>\n                    error &#061; &#034;\u5b57\u6bb5 &#039;&#034; &#043; key &#043; &#034;&#039; \u7684\u503c\u4e0d\u5728\u679a\u4e3e\u8303\u56f4\u5185&#034;;<br \/>\n                    return false;<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u5982\u679c\u6240\u6709\u68c0\u67e5\u90fd\u901a\u8fc7&#xff0c;\u5219\u8fd4\u56de true<br \/>\n    return true;<br \/>\n}<\/p>\n<p>\/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u65b9\u6cd52&#xff1a;\u6269\u5c55\u7c7b\u6821\u9a8c &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n\/**<br \/>\n * \u7ee7\u627f json \u7c7b\u6dfb\u52a0\u6821\u9a8c\u65b9\u6cd5&#xff0c;\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u94fe\u5f0f\u8c03\u7528<br \/>\n *\/<\/p>\n<p>class SchemaValidator : public json {<br \/>\nprivate:<br \/>\n    \/* \u5bf9\u5185\u6ca1\u6709\u5b57\u6bb5 *\/<br \/>\npublic:<br \/>\n    \/\/ \u91cd\u8f7d operator[] \u4ee5\u8fd4\u56de SchemaValidator \u5f15\u7528<br \/>\n    \/\/ \u91cd\u8f7d[]<br \/>\n    SchemaValidator&amp; operator[](const char* key) {<br \/>\n        return static_cast&lt;SchemaValidator&amp;&gt;(json::operator[](key));<br \/>\n    }<br \/>\n    SchemaValidator&amp; operator[](const string&amp; key) {<br \/>\n        return static_cast&lt;SchemaValidator&amp;&gt;(json::operator[](key));<br \/>\n    }<br \/>\n    \/\/ \u6784\u9020\u51fd\u6570&#xff0c;\u63a5\u53d7\u4e00\u4e2a json \u5bf9\u8c61<br \/>\n    SchemaValidator(const json&amp; j) : json(j) {}<br \/>\n    \/\/ \u94fe\u5f0f\u8c03\u7528 \u7c7b\u578b\u68c0\u67e5<br \/>\n    SchemaValidator&amp; checkType(const string&amp; type) {<br \/>\n        if (this-&gt;type_name() !&#061; type) {<br \/>\n            \/\/ \u76f4\u63a5\u629b\u51fa<br \/>\n            \/\/ \u5982\u679c\u7c7b\u578b\u4e0d\u5339\u914d&#xff0c;\u629b\u51fa\u5f02\u5e38\u5e76\u63d0\u793a\u671f\u671b\u7684\u7c7b\u578b<br \/>\n            throw runtime_error(&#034;\u7c7b\u578b\u9519\u8bef: \u671f\u671b\u7c7b\u578b\u4e3a &#034; &#043; type);<br \/>\n        }<br \/>\n        return *this;<br \/>\n    }<\/p>\n<p>    \/\/ \u94fe\u5f0f\u8c03\u7528 \u6570\u503c\u8303\u56f4\u68c0\u67e5<br \/>\n    SchemaValidator&amp; checkMin(int minNum) {<br \/>\n        \/\/ \u5982\u679c\u5f53\u524d\u503c\u662f\u6570\u5b57\u4e14\u5c0f\u4e8e\u6700\u5c0f\u503c&#xff0c;\u629b\u51fa\u5f02\u5e38\u5e76\u63d0\u793a\u9519\u8bef\u4fe1\u606f<br \/>\n        if (this-&gt;is_number() &amp;&amp; *this &lt; minNum) {<br \/>\n            throw runtime_error(&#034;\u6570\u503c\u8fc7\u5c0f: \u5fc5\u987b &gt;&#061; &#034; &#043; to_string(minNum));<br \/>\n        }<br \/>\n        return *this;<br \/>\n    }<br \/>\n    SchemaValidator&amp; checkEnum(const json&amp; allowedVal) {<br \/>\n        \/\/ \u94fe\u5f0f\u8c03\u7528 \u679a\u4e3e\u503c\u68c0\u67e5<br \/>\n        \/\/ \u904d\u5386\u5141\u8bb8\u7684\u679a\u4e3e\u503c&#xff0c;\u68c0\u67e5\u5f53\u524d\u503c\u662f\u5426\u5339\u914d\u5176\u4e2d\u4e4b\u4e00<br \/>\n        bool valid &#061; false;<br \/>\n        for (const auto&amp; val : allowedVal) {<br \/>\n            if (*this &#061;&#061; val)<br \/>\n                valid &#061; true;<br \/>\n        }<br \/>\n        \/\/ \u5982\u679c\u5f53\u524d\u503c\u4e0d\u5728\u679a\u4e3e\u8303\u56f4\u5185&#xff0c;\u629b\u51fa\u5f02\u5e38\u5e76\u63d0\u793a\u9519\u8bef\u4fe1\u606f<br \/>\n        if (!valid) {<br \/>\n            throw runtime_error(&#034;\u65e0\u6548\u7684\u679a\u4e3e\u503c&#034;);<br \/>\n        }<br \/>\n        return *this;<br \/>\n    }<br \/>\n};<\/p>\n<p>\/**<br \/>\n * &#064;brief \u4e3b\u51fd\u6570\u6f14\u793a JSON Schema \u6821\u9a8c\u548c\u6570\u636e\u8f6c\u6362\u3002<br \/>\n *<br \/>\n * \u672c\u7a0b\u5e8f\u5b9e\u73b0\u4e86\u4e24\u79cd JSON \u6821\u9a8c\u65b9\u5f0f&#xff1a;<br \/>\n * 1. \u4f7f\u7528 &#096;validateSchema&#096; \u51fd\u6570\u8fdb\u884c\u624b\u52a8\u6821\u9a8c\u3002<br \/>\n * 2. \u4f7f\u7528\u81ea\u5b9a\u4e49\u7684 &#096;SchemaValidator&#096; \u7c7b&#xff0c;\u901a\u8fc7\u94fe\u5f0f\u8c03\u7528\u8fdb\u884c\u6821\u9a8c\u3002<br \/>\n *<br \/>\n * \u4e3b\u8981\u6b65\u9aa4&#xff1a;<br \/>\n * &#8211; \u5b9a\u4e49\u7528\u4e8e\u6821\u9a8c\u7684 JSON Schema\u3002<br \/>\n * &#8211; \u6839\u636e Schema \u6821\u9a8c\u8f93\u5165\u7684 JSON \u6570\u636e\u3002<br \/>\n * &#8211; \u5c06\u6821\u9a8c\u901a\u8fc7\u7684 JSON \u6570\u636e\u8f6c\u6362\u4e3a &#096;SchemaValidator&#096; \u5bf9\u8c61&#xff0c;\u5e76\u5bf9\u5b57\u6bb5\u8fdb\u884c\u8fdb\u4e00\u6b65\u6821\u9a8c\u3002<br \/>\n *<br \/>\n * &#8211; \u4fee\u6b63\u4e86 try \u5757\u4e2d &#096;SchemaValidator validatedData&#096; \u7684\u5197\u4f59\u58f0\u660e\u3002<br \/>\n *   \u5c06 &#096;SchemaValidator validatedData(inputData);&#096; \u66ff\u6362\u4e3a\u6b63\u786e\u7684\u521d\u59cb\u5316&#xff0c;<br \/>\n *   \u907f\u514d\u91cd\u590d\u58f0\u660e\u5e76\u786e\u4fdd &#096;SchemaValidator&#096; \u5bf9\u8c61\u7684\u6b63\u786e\u4f7f\u7528\u3002<br \/>\n *<br \/>\n * &#064;param argc \u547d\u4ee4\u884c\u53c2\u6570\u7684\u6570\u91cf\u3002<br \/>\n * &#064;param argv \u547d\u4ee4\u884c\u53c2\u6570\u7684\u6570\u7ec4\u3002<br \/>\n * &#064;return int \u7a0b\u5e8f\u7684\u9000\u51fa\u72b6\u6001\u3002<br \/>\n *\/<br \/>\nint main(int argc, char const *argv[])<br \/>\n{<br \/>\n    \/\/ \u5b9a\u4e49Schema(\u53ef\u4ee5\u52a8\u6001\u52a0\u8f7d)<br \/>\n    json schema &#061; R&#034;({<br \/>\n        &#034;type&#034;: &#034;object&#034;,<br \/>\n        &#034;properties&#034;: {<br \/>\n            &#034;name&#034;: {&#034;type&#034;: &#034;string&#034;},<br \/>\n            &#034;age&#034;: {&#034;type&#034;: &#034;number&#034;, &#034;minimum&#034;: 0},<br \/>\n            &#034;status&#034;: {&#034;enum&#034;: [&#034;active&#034;, &#034;inactive&#034;]}<br \/>\n        },<br \/>\n        &#034;required&#034;: [&#034;name&#034;, &#034;status&#034;]<br \/>\n    })&#034;_json;<\/p>\n<p>    \/\/ \u9700\u8981\u9a8c\u8bc1\u7684\u6570\u636e<br \/>\n    json testData &#061; R&#034;({<br \/>\n        &#034;name&#034;: &#034;Stephen Curry&#034;,<br \/>\n        &#034;age&#034;: 37,<br \/>\n        &#034;status&#034;: &#034;active&#034;<br \/>\n    })&#034;_json;<\/p>\n<p>    \/\/ \u624b\u52a8\u6821\u9a8c \u4ee3\u7801\u6bb5<br \/>\n    string errorMsg;<br \/>\n    if(!validateSchema(testData, schema, errorMsg)) {<br \/>\n        cerr &lt;&lt; &#034;[\u624b\u52a8\u6821\u9a8c] \u5931\u8d25:&#034; &lt;&lt; errorMsg &lt;&lt; endl;<br \/>\n    } else {<br \/>\n        cout &lt;&lt; &#034;[\u624b\u52a8\u6821\u9a8c] \u901a\u8fc7!&#034; &lt;&lt; endl;<br \/>\n    }<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;&#8212;&#8211; \u65b9\u6cd52&#xff1a;\u6269\u5c55\u7c7b\u6821\u9a8c &#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    \/\/ SchemaValidator validatedData(inputData); \/\/ \u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u6269\u5c55\u7c7b<br \/>\n    try {<br \/>\n        SchemaValidator data &#061; testData; \/\/ \u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u6269\u5c55\u7c7b<\/p>\n<p>        \/\/ \u9010\u5b57\u6bb5\u94fe\u5f0f\u6821\u9a8c<\/p>\n<p>        \/**<br \/>\n         * \u4e0d\u660e\u767d\u7684\u70b9\u662f: data \u7684\u7c7b\u578b\u662f SchemaValidator<br \/>\n         * \u4f46\u662f\u8fd8\u9700\u8981\u5c06 data[&#034;name&#034;] \u7684\u7c7b\u578b \u8f6c\u6362\u6210 SchemaValidator \u624d\u80fd\u8c03\u7528check\u65b9\u6cd5<br \/>\n         * SchemaValidator \u7ee7\u627f\u81eanlohmann::json,\u4f46\u5f53\u8bbf\u95eedata[&#034;name&#034;]\u65f6&#xff0c;\u8fd4\u56de\u7684\u662f nlohmann::json \u7684\u5f15\u7528&#xff0c;\u800c\u975eSchemaValidator\u7684\u5f15\u7528&#xff0c;\u56e0\u6b64\u9700\u8981\u663e\u5f0f\u8f6c\u6362\u624d\u80fd\u8c03\u7528\u81ea\u5b9a\u4e49\u7684\u6821\u9a8c\u65b9\u6cd5\u3002<br \/>\n         * \u4e24\u79cd\u89e3\u51b3\u65b9\u6cd5:<br \/>\n         *  1. \u91cd\u8f7d[]<br \/>\n         *  2. \u663e\u793a\u8f6c\u6362<br \/>\n         *\/<br \/>\n        \/\/ 2. \u663e\u5f0f\u8f6c\u6362<br \/>\n        \/\/ static_cast&lt;SchemaValidator&amp;&gt;(data[&#034;name&#034;]).checkType(&#034;string&#034;);<br \/>\n        \/\/ static_cast&lt;SchemaValidator&amp;&gt;(data[&#034;age&#034;]).checkType(&#034;number&#034;).checkMin(0);<br \/>\n        \/\/ static_cast&lt;SchemaValidator&amp;&gt;(data[&#034;status&#034;]).checkEnum({&#034;active&#034;, &#034;inactive&#034;});<\/p>\n<p>        \/\/ 1. \u91cd\u8f7d<br \/>\n        \/\/ \u94fe\u5f0f\u8c03\u7528&#xff08;\u65e0\u9700\u663e\u5f0f\u8f6c\u6362&#xff09;<br \/>\n        \/\/ \u6b64\u5904\u7684\u90fd\u662f (const char [5]) \u7c7b\u578b \u6240\u4ee5\u91cd\u8f7d\u4f20\u53c2\u8981\u4f20 const char* key<br \/>\n        \/\/ SchemaValidator&amp; operator[](const char* key)<br \/>\n        data[&#034;name&#034;].checkType(&#034;string&#034;);<br \/>\n        data[&#034;age&#034;].checkType(&#034;number&#034;).checkMin(0);<br \/>\n        data[&#034;status&#034;].checkEnum({&#034;active&#034;, &#034;inactive&#034;});<\/p>\n<p>        cout &lt;&lt; &#034;[\u6269\u5c55\u7c7b\u6821\u9a8c] \u6210\u529f!&#034; &lt;&lt; endl;<br \/>\n    } catch (const exception &amp;exception) {<br \/>\n        cerr &lt;&lt; &#034;[\u6269\u5c55\u7c7b\u6821\u9a8c] \u5931\u8d25: &#034; &lt;&lt; exception.what() &lt;&lt; endl;<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6211\u4eec\u5c06main\u4e2d\u7684\u5e74\u9f84\u6539\u6210\u8d1f\u6570<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u4f1a\u5f97\u5230:<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"77\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220624-6855db6079b87.jpg\" width=\"554\" \/><\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6539\u6210\u6b63\u5e38\u6570\u5b57&#xff0c;\u81ea\u7136\u5c31\u662f:<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"81\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220624-6855db6087fe2.jpg\" width=\"555\" \/><\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u8981\u9a8c\u8bc1\u7684JSON\u6570\u636e\u548c\u4e00\u4e2aJSON Schema\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u901a\u8fc7\u624b\u52a8\u5b9e\u73b0\u548c\u7ee7\u627fjson\u6dfb\u52a0\u81ea\u5b9a\u4e49\u65b9\u6cd5\u00a0\u9a8c\u8bc1\u5668\u6765\u5bf9\u6570\u636e\u8fdb\u884c\u9a8c\u8bc1&#xff0c;\u5e76\u6355\u83b7\u53ef\u80fd\u7684\u9a8c\u8bc1\u5f02\u5e38\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<h5 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u9a8c\u8bc1\u7684\u6548\u7387\u4e0e\u51c6\u786e\u6027<\/span><\/h5>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u4e00\u3001\u6548\u7387\u5f71\u54cd\u56e0\u7d20\u200b\u200b<\/span><\/h6>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200bSchema \u7684\u590d\u6742\u5ea6\u200b\u200b<\/span><\/h6>\n<li style=\"text-align:justify\">\u200b\u200b\u89c4\u5219\u7c7b\u578b\u200b\u200b&#xff1a;\u82e5 Schema \u5305\u542b\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u591a\u6761\u4ef6\u903b\u8f91&#xff08;\u5982\u00a0if-then-else&#xff09;\u6216\u5d4c\u5957\u7ed3\u6784&#xff0c;\u9a8c\u8bc1\u65f6\u95f4\u4f1a\u663e\u8457\u589e\u52a0\u3002<\/li>\n<li style=\"text-align:justify\">\u200b\u200b\u9a8c\u8bc1\u6df1\u5ea6\u200b\u200b&#xff1a;\u9012\u5f52\u9a8c\u8bc1\u591a\u5c42\u5d4c\u5957\u5bf9\u8c61\u6216\u6570\u7ec4&#xff08;\u5982\u6df1\u5ea6\u8d85\u8fc7 5 \u5c42\u7684\u6811\u72b6\u7ed3\u6784&#xff09;\u4f1a\u6d88\u8017\u66f4\u591a\u8ba1\u7b97\u8d44\u6e90\u3002<\/li>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u6570\u636e\u89c4\u6a21\u200b\u200b<\/span><\/h6>\n<li style=\"text-align:justify\">\u200b\u200b\u6570\u636e\u91cf\u7ea7\u200b\u200b&#xff1a;\u5904\u7406\u5927\u89c4\u6a21 JSON \u6570\u636e&#xff08;\u5982\u8d85\u8fc7 1MB \u7684\u6587\u4ef6&#xff09;\u65f6&#xff0c;\u5185\u5b58\u5360\u7528\u548c\u89e3\u6790\u65f6\u95f4\u4f1a\u4e0a\u5347\u3002<\/li>\n<li style=\"text-align:justify\">\u200b\u200b\u5b57\u6bb5\u6570\u91cf\u200b\u200b&#xff1a;\u5bf9\u8c61\u5c5e\u6027\u6570\u91cf\u8d8a\u591a&#xff08;\u5982\u5305\u542b 100 \u4e2a\u5b57\u6bb5\u7684 API \u54cd\u5e94&#xff09;&#xff0c;\u9010\u4e2a\u5339\u914d Schema \u89c4\u5219\u7684\u65f6\u95f4\u6210\u672c\u8d8a\u9ad8<\/li>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u4e8c\u3001\u51c6\u786e\u6027\u5f71\u54cd\u56e0\u7d20\u200b\u200b<\/span><\/h6>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200bSchema \u7684\u5b8c\u5584\u6027\u200b\u200b<\/span><\/h6>\n<li style=\"text-align:justify\">\u200b\u200b\u7ea6\u675f\u8986\u76d6\u5ea6\u200b\u200b&#xff1a;\u82e5 Schema \u672a\u6db5\u76d6\u6240\u6709\u53ef\u80fd\u7684\u5b57\u6bb5\u6216\u8fb9\u754c\u6761\u4ef6&#xff08;\u5982\u672a\u5b9a\u4e49\u00a0minimum\u00a0\u5bfc\u81f4\u8d1f\u6570\u901a\u8fc7\u6821\u9a8c&#xff09;&#xff0c;\u51c6\u786e\u6027\u4f1a\u4e0b\u964d\u3002<\/li>\n<li style=\"text-align:justify\">\u200b\u200b\u7248\u672c\u517c\u5bb9\u6027\u200b\u200b&#xff1a;\u4e0d\u540c\u7248\u672c\u7684 JSON Schema \u8349\u6848&#xff08;\u5982 Draft-4 \u4e0e Draft-7&#xff09;\u652f\u6301\u7684\u7279\u6027\u4e0d\u540c&#xff0c;\u7248\u672c\u4e0d\u5339\u914d\u53ef\u80fd\u5bfc\u81f4\u89c4\u5219\u5931\u6548\u3002<\/li>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u6570\u636e\u89c4\u8303\u6027\u200b\u200b<\/span><\/h6>\n<li style=\"text-align:justify\">\u200b\u200b\u6570\u636e\u683c\u5f0f\u9519\u8bef\u200b\u200b&#xff1a;\u4f8b\u5982\u5b57\u6bb5\u7c7b\u578b\u4e0d\u7b26&#xff08;\u5b57\u7b26\u4e32\u8bef\u5199\u4e3a\u6570\u5b57&#xff09;\u3001\u679a\u4e3e\u503c\u8d8a\u754c\u7b49\u3002<\/li>\n<li style=\"text-align:justify\">\u200b\u200b\u52a8\u6001\u6570\u636e\u53d8\u5316\u200b\u200b&#xff1a;\u82e5\u4e1a\u52a1\u903b\u8f91\u8c03\u6574\u5bfc\u81f4\u6570\u636e\u7ed3\u6784\u53d8\u66f4&#xff08;\u5982\u65b0\u589e\u53ef\u9009\u5b57\u6bb5&#xff09;&#xff0c;Schema \u672a\u540c\u6b65\u66f4\u65b0\u4f1a\u5f15\u53d1\u9a8c\u8bc1\u9057\u6f0f\u3002<\/li>\n<h6 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u6821\u9a8c\u7b56\u7565\u200b\u200b<\/span><\/h6>\n<li style=\"text-align:justify\">\u200b\u200b\u5206\u5c42\u9a8c\u8bc1\u200b\u200b&#xff1a;\u5148\u6821\u9a8c\u6574\u4f53\u7ed3\u6784&#xff08;\u5982\u00a0type: object&#xff09;&#xff0c;\u518d\u9010\u5c42\u6df1\u5165\u6821\u9a8c\u5d4c\u5957\u5b57\u6bb5&#xff0c;\u907f\u514d\u65e0\u6548\u7684\u5168\u91cf\u68c0\u67e5\u3002<\/li>\n<li style=\"text-align:justify\">\u200b\u200b\u9519\u8bef\u5904\u7406\u673a\u5236\u200b\u200b&#xff1a;\u90e8\u5206\u5de5\u5177\u4ec5\u8fd4\u56de\u9996\u4e2a\u9519\u8bef&#xff0c;\u800c\u652f\u6301\u5168\u91cf\u9519\u8bef\u6536\u96c6\u7684\u5e93&#xff08;\u5982\u00a0jsonschema&#xff09;\u80fd\u63d0\u4f9b\u66f4\u5168\u9762\u7684\u8bca\u65ad\u3002<\/li>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<h4 style=\"background-color:transparent;margin-left:0px;margin-right:0px;text-align:justify\"><span style=\"color:#365f91\">C&#043;&#043;11\u53ca\u4ee5\u4e0a\u7279\u6027\u5e94\u7528<\/span><\/h4>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">C&#043;&#043;11 \u5f15\u5165\u591a\u4e2a\u7279\u6027&#xff0c;\u66f4\u52a0\u65b9\u4fbf\u6613\u4e8e\u5904\u7406JSON\u5bf9\u8c61<\/p>\n<h5 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u81ea\u52a8\u7c7b\u578b\u63a8\u5bfc<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">C&#043;&#043;11 \u5f15\u5165\u4e86\u00a0auto\u00a0\u5173\u952e\u5b57&#xff0c;\u5141\u8bb8\u7f16\u8bd1\u5668\u81ea\u52a8\u63a8\u65ad\u53d8\u91cf\u7684\u7c7b\u578b\u3002JSON\u7684\u7ed3\u6784\u5f80\u5f80\u662f\u52a8\u6001\u4e14\u590d\u6742\u7684&#xff0c;\u4f7f\u7528\u00a0auto\u00a0\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<h5 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">Lambda\u4e0eJSON\u5904\u7406<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">Lambda\u8868\u8fbe\u5f0f\u662fC&#043;&#043;11\u7684\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u7279\u6027\u3002\u5b83\u5141\u8bb8\u5b9a\u4e49\u533f\u540d\u51fd\u6570&#xff0c;\u975e\u5e38\u9002\u5408\u4e8eJSON\u6570\u636e\u5904\u7406\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u8fd8\u662f\u6765\u770b\u4ee3\u7801:<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p>#include &lt;vector&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;nlohmann\/json.hpp&gt;<br \/>\nusing json &#061; nlohmann::json;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53<br \/>\nstruct User<br \/>\n{<br \/>\n    string name;<br \/>\n    int age;<br \/>\n    vector&lt;string&gt; addr;<br \/>\n};<\/p>\n<p>int main(int argc, char const *argv[])<br \/>\n{<br \/>\n    User user{&#034;Lzj&#034;, 20, {&#034;Yibin&#034;, &#034;Chengdu&#034;}};<br \/>\n    \/\/ lambda \u6765\u5b9e\u73b0\u5e8f\u5217\u5316<br \/>\n    \/**<br \/>\n     * \u62a5\u9519\u65e5\u5fd7<br \/>\n     * \u6ca1\u6709\u4e0e\u53c2\u6570\u5217\u8868\u5339\u914d\u7684 \u91cd\u8f7d\u51fd\u6570 &#034;nlohmann::json_abi_v3_11_3::basic_json&lt;ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass&gt;::update [\u5176\u4e2d ObjectType&#061;std::map, ArrayType&#061;std::vector, StringType&#061;std::string, BooleanType&#061;bool, NumberIntegerType&#061;int64_t, NumberUnsignedType&#061;uint64_t, NumberFloatType&#061;double, AllocatorType&#061;std::allocator, JSONSerializer&#061;nlohmann::json_abi_v3_11_3::adl_serializer, BinaryType&#061;std::vector&lt;uint8_t, std::allocator&lt;uint8_t&gt;&gt;, CustomBaseClass&#061;void]&#034; \u5b9e\u4f8bC\/C&#043;&#043;(304)<br \/>\n     *\/<br \/>\n    \/\/ auto serializeUser &#061; [](const User&amp; u) -&gt; json {<br \/>\n    \/\/  return json{<br \/>\n    \/\/      {&#034;name&#034;, u.name},<br \/>\n    \/\/      {&#034;age&#034;, u.age},<br \/>\n    \/\/      {&#034;addr&#034;, json::array()} \/\/ \u521d\u59cb\u5316\u7684\u7a7a\u6570\u7ec4<br \/>\n    \/\/  }.update([&amp;u](json&amp; j) {<br \/>\n    \/\/      \/\/ lambda \u6765\u52a8\u6001\u586b\u5145\u521d\u59cb\u5316\u7684\u7a7a\u6570\u7ec4 \u4e5f\u5c31\u662faddr<br \/>\n    \/\/      for (const auto&amp; address : u.addr)<br \/>\n    \/\/          j[&#034;addr&#034;].push_back(address);<br \/>\n    \/\/  });<br \/>\n    \/\/ };<br \/>\n    auto serializeUser &#061; [](const User&amp; u) -&gt; json {<br \/>\n        return json{<br \/>\n            {&#034;name&#034;, u.name},<br \/>\n            {&#034;age&#034;, u.age},<br \/>\n            {&#034;addr&#034;, u.addr}  \/\/ \u76f4\u63a5\u8d4b\u503c vector&lt;string&gt; \u2192 JSON \u6570\u7ec4<br \/>\n        };<br \/>\n    };<\/p>\n<p>    \/\/ \u5e8f\u5217\u5316\u4e4b\u540e\u8f93\u51fa<br \/>\n    json userJson &#061; serializeUser(user);<br \/>\n    cout &lt;&lt; &#034;\u5e8f\u5217\u5316\u7ed3\u679c: \\\\n&#034; &lt;&lt; userJson.dump(2) &lt;&lt; endl;<br \/>\n    return 0;<br \/>\n}<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u8fd0\u884c\u51fa\u6765\u5c31\u662f:<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"182\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220624-6855db60967c8.jpg\" width=\"554\" \/><\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u4f18\u52bf\u4e5f\u5341\u5206\u660e\u663e:<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u200b\u6d88\u9664\u4e2d\u95f4\u53d8\u91cf\u200b\u200b&#xff1a;\u4e0d\u9700\u8981\u901a\u8fc7\u51fd\u6570\u53c2\u6570\u4f20\u9012 json \u5bf9\u8c61<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u200b\u200b\u907f\u514d\u6570\u636e\u62f7\u8d1d\u200b\u200b&#xff1a;\u6309\u5f15\u7528\u6355\u83b7\u7ed3\u6784\u4f53\u6210\u5458&#xff08;\u9690\u5f0f\u4f7f\u7528 const User&amp;&#xff09;,\u6027\u80fd\u4e5f\u5f97\u5230\u63d0\u5347<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<h5 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u5e76\u53d1\u4e0eJSON\u6570\u636e\u5904\u7406<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6211\u4eec\u5728\u591a\u7ebf\u7a0b\u4e0b&#xff0c;\u5b9e\u73b0JSON\u6570\u636e\u5904\u7406<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u501f\u52a9\u4e8e\u7ebf\u7a0b&#xff0c;\u539f\u5b50\u64cd\u4f5c&#xff0c;\u52a0\u9501\u7b49\u5e76\u53d1\u63a7\u5236\u673a\u5236\u7ed3\u5408JSON\u5904\u7406\u5e93<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u6765\u770b\u4ee3\u7801&#xff1a;<\/p>\n<p>#include &lt;nlohmann\/json.hpp&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;thread&gt;<br \/>\n#include &lt;mutex&gt;<br \/>\n#include &lt;shared_mutex&gt;<br \/>\n#include &lt;atomic&gt;<br \/>\n#include &lt;memory&gt;<\/p>\n<p>using namespace std;<br \/>\nusing json &#061; nlohmann::json;<\/p>\n<p>\/**<br \/>\n * \u539f\u5b50\u64cd\u4f5c\u53ef\u4ee5\u7528\u6765\u5b89\u5168\u5730\u66f4\u65b0\u8ba1\u6570\u5668&#xff0c;\u8fd9\u6837\u907f\u514d\u4e86\u4e92\u65a5\u9501\u7684\u5f00\u9500&#xff0c;\u9002\u5408\u7b80\u5355\u7684\u8ba1\u6570\u573a\u666f\u3002<br \/>\n *\/<br \/>\n\/\/ \u539f\u5b50\u64cd\u4f5c\u8ba1\u6570\u5668&#xff08;\u65e0\u9501\u8bbe\u8ba1&#xff09;<br \/>\natomic&lt;int&gt; processed_count{0};<br \/>\natomic&lt;int&gt; error_count{0};<\/p>\n<p>\/\/ \u667a\u80fd\u6307\u9488\u7ba1\u7406\u5171\u4eab\u6570\u636e \u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168<br \/>\nstruct SharedData {<br \/>\n    shared_ptr&lt;json&gt; dataSets;  \/\/ \u5171\u4eabJSON\u6570\u636e\u96c6\u5408<br \/>\n    shared_mutex rw_mutex;      \/\/ \u8bfb\u5199\u9501\u63a7\u5236<br \/>\n};<\/p>\n<p>using SharedDataPtr &#061; shared_ptr&lt;SharedData&gt;;<\/p>\n<p>\/\/ \u6a21\u677f\u7ed3\u6784\u4f53\u7684typedef<\/p>\n<p>\/\/ \u5904\u7406\u7ebf\u7a0b (\u5305\u542b\u667a\u80fd\u6307\u9488\u4e0e\u9501\u673a\u5236)<br \/>\nvoid process_chunk(SharedDataPtr data, size_t start, size_t end) {<br \/>\n    try {<br \/>\n        \/\/ \u8bfb\u9501 \u5171\u4eab\u8bbf\u95ee&#xff0c;\u786e\u4fdd\u5728\u8bfb\u53d6\u6570\u636e\u65f6\u4e0d\u4f1a\u88ab\u5176\u4ed6\u7ebf\u7a0b\u4fee\u6539<br \/>\n        {<br \/>\n            \/\/ \u8bfb\u9501\u7684\u751f\u547d\u5468\u671f\u5f00\u59cb<br \/>\n            shared_lock lock(data-&gt;rw_mutex);<br \/>\n            if (!data-&gt;dataSets || data-&gt;dataSets-&gt;empty())<br \/>\n                throw runtime_error(&#034;JSON\u6570\u636e\u96c6\u5408\u4e3a\u7a7a&#034;); \/\/ \u5982\u679c\u6570\u636e\u4e3a\u7a7a&#xff0c;\u629b\u51fa\u5f02\u5e38<br \/>\n            \/\/ \u8bfb\u9501\u7684\u751f\u547d\u5468\u671f\u7ed3\u675f<br \/>\n        }<\/p>\n<p>        \/\/ \u521b\u5efa\u7ebf\u7a0b\u5c40\u90e8\u7684\u5904\u7406\u526f\u672c&#xff0c;\u4f7f\u7528 unique_ptr \u786e\u4fdd\u72ec\u5360\u8d44\u6e90<br \/>\n        auto local_json &#061; make_unique&lt;json&gt;();<br \/>\n        {<br \/>\n            \/\/ \u8bfb\u9501\u7684\u751f\u547d\u5468\u671f\u5f00\u59cb<br \/>\n            shared_lock lock(data-&gt;rw_mutex);<br \/>\n            \/\/ \u6df1\u62f7\u8d1d\u5171\u4eab\u6570\u636e\u5230\u7ebf\u7a0b\u5c40\u90e8\u526f\u672c&#xff0c;\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168<br \/>\n            \/\/ \u8fd9\u91cc\u4f7f\u7528\u6df1\u62f7\u8d1d\u662f\u4e3a\u4e86\u907f\u514d\u7ebf\u7a0b\u4e4b\u95f4\u5171\u4eab\u540c\u4e00\u4efd\u6570\u636e&#xff0c;<br \/>\n            \/\/ \u4ece\u800c\u9632\u6b62\u5728\u5904\u7406\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u6570\u636e\u7ade\u4e89\u6216\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002<br \/>\n            \/\/ \u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u62e5\u6709\u81ea\u5df1\u7684\u72ec\u7acb\u526f\u672c&#xff0c;\u53ef\u4ee5\u5b89\u5168\u5730\u8fdb\u884c\u4fee\u6539\u6216\u8bfb\u53d6\u3002<br \/>\n            *local_json &#061; *data-&gt;dataSets;<br \/>\n            \/\/ \u8bfb\u9501\u7684\u751f\u547d\u5468\u671f\u7ed3\u675f<br \/>\n        }<\/p>\n<p>        \/\/ \u904d\u5386\u6307\u5b9a\u8303\u56f4\u5185\u7684 JSON \u6570\u636e<br \/>\n        for (size_t i &#061; start; i &lt; end; i&#043;&#043;) {<br \/>\n            \/\/ \u4fee\u6539\u6570\u636e\u65f6\u9700\u8981\u83b7\u53d6\u5199\u9501&#xff0c;\u786e\u4fdd\u4e92\u65a5\u8bbf\u95ee<br \/>\n            {<br \/>\n                \/\/ \u5199\u9501\u7684\u751f\u547d\u5468\u671f\u5f00\u59cb<br \/>\n                unique_lock lock(data-&gt;rw_mutex);<br \/>\n                auto&amp; entry &#061; (*data-&gt;dataSets)[i];<br \/>\n                entry[&#034;processed&#034;] &#061; true; \/\/ \u6807\u8bb0\u6570\u636e\u4e3a\u5df2\u5904\u7406<br \/>\n                \/\/ \u5199\u9501\u7684\u751f\u547d\u5468\u671f\u7ed3\u675f<br \/>\n            }<br \/>\n            \/\/ \u4f7f\u7528\u539f\u5b50\u64cd\u4f5c\u66f4\u65b0\u5904\u7406\u8ba1\u6570\u5668&#xff0c;\u907f\u514d\u7ade\u4e89\u6761\u4ef6<br \/>\n            processed_count.fetch_add(1, memory_order_relaxed);<br \/>\n        }<br \/>\n    } catch (const exception&amp; e) {<br \/>\n        \/\/ \u6355\u83b7\u5f02\u5e38\u5e76\u4f7f\u7528\u539f\u5b50\u64cd\u4f5c\u66f4\u65b0\u9519\u8bef\u8ba1\u6570\u5668<br \/>\n        error_count.fetch_add(1);<br \/>\n    }<br \/>\n}<\/p>\n<p>int main(int argc, char const *argv[])<br \/>\n{<br \/>\n    \/\/ \u521d\u59cb\u5316\u5171\u4eab\u6570\u636e \u4f7f\u7528\u667a\u80fd\u6307\u9488<br \/>\n    SharedDataPtr shared_data &#061; make_shared&lt;SharedData&gt;();<br \/>\n    shared_data-&gt;dataSets &#061; make_shared&lt;json&gt;(json::parse(R&#034;([{&#034;id&#034;: 1}, {&#034;id&#034;: 2}, {&#034;id&#034;: 3}, {&#034;id&#034;: 4}])&#034;));<\/p>\n<p>    \/\/ \u7ebf\u7a0b\u6c60 \u542f\u52a8<br \/>\n    \/**<br \/>\n     * inline unsigned int thread::hardware_concurrency() noexcept { return 0; }<br \/>\n     *\/<br \/>\n    const auto thread_num &#061; thread::hardware_concurrency();<br \/>\n    vector&lt;thread&gt; workers;<br \/>\n    const size_t chunk_size &#061; shared_data-&gt;dataSets-&gt;size() \/ thread_num;<\/p>\n<p>    for (size_t i &#061; 0; i &lt; thread_num; i&#043;&#043;)<br \/>\n    {<br \/>\n        const size_t start &#061; i * chunk_size;<br \/>\n        const size_t end &#061; (i &#061;&#061; thread_num &#8211; 1) ? shared_data-&gt;dataSets-&gt;size() : start &#043; chunk_size;<br \/>\n        workers.emplace_back(process_chunk, shared_data, start, end);<br \/>\n    }<\/p>\n<p>    \/\/ \u7b49\u5f85\u7ebf\u7a0b\u5168\u90e8\u5b8c\u6210<br \/>\n    for (auto&amp; tmp : workers)<br \/>\n        tmp.join();<br \/>\n    \/\/ \u6253\u5370\u5230\u63a7\u5236\u53f0<br \/>\n    cout &lt;&lt; &#034;\u5df2\u5904\u7406: &#034; &lt;&lt; processed_count<br \/>\n         &lt;&lt; &#034; \u9879, \u9519\u8bef: &#034; &lt;&lt; error_count &lt;&lt; &#034; \u9879&#034; &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;\u6700\u7ec8\u7684 JSON \u6570\u636e:\\\\n&#034; &lt;&lt; shared_data-&gt;dataSets-&gt;dump(2) &lt;&lt; endl;<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:left\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u8fd0\u884c\u6548\u679c\u5982\u56fe:<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"279\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220624-6855db60a6ea2.jpg\" width=\"553\" \/><\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u8fd9\u6bb5\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u591a\u7ebf\u7a0b\u5e76\u884c\u5904\u7406JSON\u6570\u636e\u7684\u7a0b\u5e8f&#xff0c;\u7ed3\u5408\u4e86\u73b0\u4ee3C&#043;&#043;\u7684\u5e76\u53d1\u63a7\u5236\u673a\u5236\u548cJSON\u5e93\u7684\u9ad8\u6548\u5904\u7406\u80fd\u529b\u3002\u8fd9\u4e2a\u5b9e\u73b0\u5178\u578b\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u541e\u5410\u91cf\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u7684\u573a\u666f&#xff08;\u5982\u65e5\u5fd7\u5206\u6790\u3001\u6279\u91cf\u6570\u636e\u8f6c\u6362\u7b49&#xff09;&#xff0c;\u901a\u8fc7\u5e76\u53d1\u5904\u7406\u673a\u5236\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u5904\u7406\u6548\u7387\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<h3 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">SUMMARY<\/span><\/h3>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u901a\u8fc7\u6574\u5408JSON Schema\u7684\u5f3a\u7c7b\u578b\u9a8c\u8bc1\u4f53\u7cfb\u4e0eC&#043;&#043;11\u7684\u73b0\u4ee3\u5316\u8bed\u8a00\u7279\u6027&#xff0c;\u80fd\u591f\u663e\u8457\u63d0\u5347JSON\u6570\u636e\u5904\u7406\u7684\u53ef\u9760\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<h5 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u7ed3\u6784\u5316\u6570\u636e\u9a8c\u8bc1\u673a\u5236\u200b\u200b<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">JSON Schema\u901a\u8fc7\u5b9a\u4e49\u4e25\u683c\u7684\u5b57\u6bb5\u7c7b\u578b\u3001\u683c\u5f0f\u7ea6\u675f\u548c\u4e1a\u52a1\u89c4\u5219&#xff0c;\u7ed3\u5408C&#043;&#043;\u7684\u7f16\u8bd1\u65f6\u7c7b\u578b\u68c0\u67e5&#xff0c;\u5f62\u6210\u53cc\u91cd\u6570\u636e\u6821\u9a8c\u5c4f\u969c\u3002\u901a\u8fc7JSON Schema\u53ef\u81ea\u52a8\u62e6\u622a\u7f3a\u5931\u5fc5\u586b\u5b57\u6bb5\u6216\u7c7b\u578b\u4e0d\u7b26\u7684\u8bf7\u6c42&#xff0c;\u800cC&#043;&#043;11\u7684static_assert\u7b49\u7279\u6027\u80fd\u5728\u7f16\u8bd1\u9636\u6bb5\u63d0\u524d\u53d1\u73b0\u6f5c\u5728\u7c7b\u578b\u9519\u8bef\u3002<\/p>\n<h5 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u9ad8\u6027\u80fd\u5e76\u53d1\u5904\u7406\u80fd\u529b\u200b\u200b<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\u5229\u7528C&#043;&#043;11\u7684\u7ebf\u7a0b\u5e93\u548c\u539f\u5b50\u64cd\u4f5c&#xff0c;\u914d\u5408JSON\u5e93\u7684\u591a\u7ebf\u7a0b\u4f18\u5316\u8bbe\u8ba1&#xff08;&#xff0c;\u53ef\u5b9e\u73b0JSON\u6570\u636e\u7684\u5e76\u884c\u5316\u5904\u7406\u3002\u5178\u578b\u573a\u666f\u5305\u62ec&#xff1a;<span style=\"background-color:yellow\"><span style=\"color:red\">\u901a\u8fc7<\/span><\/span><span style=\"background-color:yellow\"><span style=\"color:red\">std::async<\/span><\/span><span style=\"background-color:yellow\"><span style=\"color:red\">\u5f02\u6b65\u5904\u7406\u5927\u89c4\u6a21<\/span><\/span><span style=\"background-color:yellow\"><span style=\"color:red\">JSON<\/span><\/span><span style=\"background-color:yellow\"><span style=\"color:red\">\u6570\u636e\u96c6&#xff0c;\u6216\u4f7f\u7528\u8bfb\u5199\u9501<\/span><\/span><span style=\"background-color:yellow\"><span style=\"color:red\">(shared_mutex)<\/span><\/span><span style=\"background-color:yellow\"><span style=\"color:red\">\u5b9e\u73b0\u9ad8\u541e\u5410\u91cf\u7684\u5b9e\u65f6\u6570\u636e\u6d41\u89e3\u6790\u3002<\/span><\/span><\/p>\n<h5 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u5f00\u53d1\u6548\u7387\u63d0\u5347\u7b56\u7565\u200b\u200b<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">C&#043;&#043;11\u7684\u81ea\u52a8\u7c7b\u578b\u63a8\u5bfc(auto)\u548cLambda\u8868\u8fbe\u5f0f&#xff0c;\u7ed3\u5408JSON Schema\u7684\u81ea\u52a8\u5316\u6d4b\u8bd5\u6846\u67b6&#xff0c;\u53ef\u51cf\u5c11\u6837\u677f\u4ee3\u7801\u91cf\u3002\u4f8b\u5982\u4f7f\u7528nlohmann::json\u5e93\u65f6&#xff0c;\u901a\u8fc7auto\u63a8\u5bfc\u590d\u6742JSON\u7ed3\u6784&#xff0c;\u914d\u5408Schema\u9a8c\u8bc1\u5668\u7684\u9884\u5b9a\u4e49\u89c4\u5219&#xff0c;\u80fd\u5feb\u901f\u6784\u5efa\u5065\u58ee\u7684\u6570\u636e\u7ba1\u9053\u3002<\/p>\n<h5 style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#365f91\">\u200b\u200b\u5b89\u5168\u589e\u5f3a\u673a\u5236\u200b\u200b<\/span><\/h5>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">JSON Schema\u7684\u683c\u5f0f\u9a8c\u8bc1&#xff08;\u5982\u6b63\u5219\u8868\u8fbe\u5f0f\u7ea6\u675f&#xff09;\u53ef\u9632\u8303\u975e\u6cd5\u6570\u636e\u6ce8\u5165&#xff0c;\u800cC&#043;&#043;11\u7684\u667a\u80fd\u6307\u9488(shared_ptr)\u548c\u79fb\u52a8\u8bed\u4e49\u5219\u6709\u6548\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002\u5728\u6570\u636e\u53cd\u5e8f\u5217\u5316\u8fc7\u7a0b\u4e2d&#xff0c;\u8fd9\u79cd\u7ec4\u5408\u80fd\u5b9e\u73b0\u7f51\u7edc\u534f\u8bae\u5c42\u5230\u5185\u5b58\u7ba1\u7406\u7684\u5168\u94fe\u8def\u5b89\u5168\u4fdd\u969c\u3002<\/p>\n<h3 style=\"margin-left:0px;margin-right:0px;text-align:justify\">\u9644\u5f55<\/h3>\n<p>\u5b89\u88c5\u5305<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.5k\u6b21\uff0c\u70b9\u8d5e28\u6b21\uff0c\u6536\u85cf11\u6b21\u3002C++\u5bf9\u8c61\u7684JSON\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\u57fa\u4e8eJsonCpp\u5e93\u5b9e\u73b0C++\u5bf9\u8c61\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316JSON \u4ecb\u7ecdJSON\u4f5c\u4e3a\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\uff0c\u5728Web\u670d\u52a1\u548c\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u3002JSON\uff08Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u6613\u4e8e\u4eba\u9605\u8bfb\u548c\u7f16\u5199\uff0c\u540c\u65f6\u4e5f\u6613\u4e8e\u673a\u5668\u89e3\u6790\u548c\u751f\u6210\u3002\u5b83\u57fa\u4e8eJavaScript\u7684\u4e00\u4e2a\u5b50\u96c6\uff0c\u4f46\u662fJSON\u662f\u72ec\u7acb\u4e8e\u8bed\u8a00\u7684\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u90fd\u652f\u6301JSON\u683c\u5f0f\u6570\u636e\u7684\u751f\u6210\u548c\u89e3\u6790\u3002JSON\u6570\u636e\u7ed3\u6784\u4e24\u4e2a\u5173\u952e\u70b9: \u5bf9\u8c61\u548c\u6570\u7ec4\u5bf9\u8c61\u7531\u3002_c++\u5982\u4f55json\u5b57\u7b26\u4e32\u8f6c\u6362<\/p>\n","protected":false},"author":2,"featured_media":44912,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,93,190],"topic":[],"class_list":["post-44928","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-json","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u57fa\u4e8enlohmann\/json \u5b9e\u73b0 \u4eceC++\u5bf9\u8c61\u8f6c\u6362\u6210JSON\u6570\u636e\u683c\u5f0f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/44928.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u57fa\u4e8enlohmann\/json \u5b9e\u73b0 \u4eceC++\u5bf9\u8c61\u8f6c\u6362\u6210JSON\u6570\u636e\u683c\u5f0f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.5k\u6b21\uff0c\u70b9\u8d5e28\u6b21\uff0c\u6536\u85cf11\u6b21\u3002C++\u5bf9\u8c61\u7684JSON\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\u57fa\u4e8eJsonCpp\u5e93\u5b9e\u73b0C++\u5bf9\u8c61\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316JSON \u4ecb\u7ecdJSON\u4f5c\u4e3a\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\uff0c\u5728Web\u670d\u52a1\u548c\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u3002JSON\uff08Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u6613\u4e8e\u4eba\u9605\u8bfb\u548c\u7f16\u5199\uff0c\u540c\u65f6\u4e5f\u6613\u4e8e\u673a\u5668\u89e3\u6790\u548c\u751f\u6210\u3002\u5b83\u57fa\u4e8eJavaScript\u7684\u4e00\u4e2a\u5b50\u96c6\uff0c\u4f46\u662fJSON\u662f\u72ec\u7acb\u4e8e\u8bed\u8a00\u7684\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u90fd\u652f\u6301JSON\u683c\u5f0f\u6570\u636e\u7684\u751f\u6210\u548c\u89e3\u6790\u3002JSON\u6570\u636e\u7ed3\u6784\u4e24\u4e2a\u5173\u952e\u70b9: \u5bf9\u8c61\u548c\u6570\u7ec4\u5bf9\u8c61\u7531\u3002_c++\u5982\u4f55json\u5b57\u7b26\u4e32\u8f6c\u6362\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/44928.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-20T22:06:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220623-6855db5fa212d.jpg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"23 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44928.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/44928.html\",\"name\":\"\u57fa\u4e8enlohmann\/json \u5b9e\u73b0 \u4eceC++\u5bf9\u8c61\u8f6c\u6362\u6210JSON\u6570\u636e\u683c\u5f0f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-06-20T22:06:25+00:00\",\"dateModified\":\"2025-06-20T22:06:25+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/44928.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/44928.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44928.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u57fa\u4e8enlohmann\/json \u5b9e\u73b0 \u4eceC++\u5bf9\u8c61\u8f6c\u6362\u6210JSON\u6570\u636e\u683c\u5f0f\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u57fa\u4e8enlohmann\/json \u5b9e\u73b0 \u4eceC++\u5bf9\u8c61\u8f6c\u6362\u6210JSON\u6570\u636e\u683c\u5f0f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/44928.html","og_locale":"zh_CN","og_type":"article","og_title":"\u57fa\u4e8enlohmann\/json \u5b9e\u73b0 \u4eceC++\u5bf9\u8c61\u8f6c\u6362\u6210JSON\u6570\u636e\u683c\u5f0f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.5k\u6b21\uff0c\u70b9\u8d5e28\u6b21\uff0c\u6536\u85cf11\u6b21\u3002C++\u5bf9\u8c61\u7684JSON\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\u57fa\u4e8eJsonCpp\u5e93\u5b9e\u73b0C++\u5bf9\u8c61\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316JSON \u4ecb\u7ecdJSON\u4f5c\u4e3a\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\uff0c\u5728Web\u670d\u52a1\u548c\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u3002JSON\uff08Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u6613\u4e8e\u4eba\u9605\u8bfb\u548c\u7f16\u5199\uff0c\u540c\u65f6\u4e5f\u6613\u4e8e\u673a\u5668\u89e3\u6790\u548c\u751f\u6210\u3002\u5b83\u57fa\u4e8eJavaScript\u7684\u4e00\u4e2a\u5b50\u96c6\uff0c\u4f46\u662fJSON\u662f\u72ec\u7acb\u4e8e\u8bed\u8a00\u7684\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u90fd\u652f\u6301JSON\u683c\u5f0f\u6570\u636e\u7684\u751f\u6210\u548c\u89e3\u6790\u3002JSON\u6570\u636e\u7ed3\u6784\u4e24\u4e2a\u5173\u952e\u70b9: \u5bf9\u8c61\u548c\u6570\u7ec4\u5bf9\u8c61\u7531\u3002_c++\u5982\u4f55json\u5b57\u7b26\u4e32\u8f6c\u6362","og_url":"https:\/\/www.wsisp.com\/helps\/44928.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-06-20T22:06:25+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250620220623-6855db5fa212d.jpg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"23 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/44928.html","url":"https:\/\/www.wsisp.com\/helps\/44928.html","name":"\u57fa\u4e8enlohmann\/json \u5b9e\u73b0 \u4eceC++\u5bf9\u8c61\u8f6c\u6362\u6210JSON\u6570\u636e\u683c\u5f0f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-06-20T22:06:25+00:00","dateModified":"2025-06-20T22:06:25+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/44928.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/44928.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/44928.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u57fa\u4e8enlohmann\/json \u5b9e\u73b0 \u4eceC++\u5bf9\u8c61\u8f6c\u6362\u6210JSON\u6570\u636e\u683c\u5f0f"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/44928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=44928"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/44928\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/44912"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=44928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=44928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=44928"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=44928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}