{"id":935690,"date":"2024-12-26T18:58:06","date_gmt":"2024-12-26T10:58:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/935690.html"},"modified":"2024-12-26T18:58:09","modified_gmt":"2024-12-26T10:58:09","slug":"python%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%bajson","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/935690.html","title":{"rendered":"python\u5982\u4f55\u521b\u5efajson"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072158\/a74695f4-7650-4d81-8230-28841f6af06c.webp\" alt=\"python\u5982\u4f55\u521b\u5efajson\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u521b\u5efaJSON\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u5178\u521b\u5efa\u3001\u4f7f\u7528\u5217\u8868\u521b\u5efa\u3001\u7ed3\u5408\u5b57\u5178\u548c\u5217\u8868\u521b\u5efa<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u7684\u5185\u7f6e\u5b57\u5178\u7c7b\u578b\u6765\u521b\u5efaJSON\u5bf9\u8c61\uff0c\u56e0\u4e3aJSON\u672c\u8d28\u4e0a\u662f\u952e\u503c\u5bf9\u7684\u96c6\u5408\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u5b57\u5178\u521b\u5efaJSON\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>Python\u5b57\u5178\u4e0eJSON\u5bf9\u8c61\u6709\u7740\u76f4\u63a5\u7684\u5bf9\u5e94\u5173\u7cfb\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u4f7f\u7528\u5b57\u5178\u6765\u521b\u5efaJSON\u5bf9\u8c61\u3002\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178\uff0c\u7136\u540e\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u4e2d\u7684<code>dumps()<\/code>\u51fd\u6570\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u975e\u5e38\u7b80\u5355\u4e14\u9ad8\u6548\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u7ed3\u6784\u5316\u7684\u6570\u636e\u3002\u5728Python\u4e2d\uff0c<code>json<\/code>\u6a21\u5757\u81ea\u5e26\u4e86\u8bb8\u591a\u6709\u7528\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406JSON\u6570\u636e\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8Python\u4e2d\u521b\u5efaJSON\u7684\u5404\u79cd\u65b9\u6cd5\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5b57\u5178\u521b\u5efaJSON<\/p>\n<\/p>\n<p><p>Python\u7684\u5b57\u5178\u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4e0eJSON\u5bf9\u8c61\u975e\u5e38\u76f8\u4f3c\u3002\u901a\u8fc7\u5b57\u5178\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u548c\u7ba1\u7406JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u7b80\u5355\u7684JSON\u5bf9\u8c61<\/li>\n<\/ol>\n<p><p>\u8981\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684JSON\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u7528\u6237\u4fe1\u606f\u7684JSON\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>user_info = {<\/p>\n<p>    &quot;name&quot;: &quot;John Doe&quot;,<\/p>\n<p>    &quot;age&quot;: 30,<\/p>\n<p>    &quot;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&quot;: &quot;john.doe@example.com&quot;<\/p>\n<p>}<\/p>\n<p>json_string = json.dumps(user_info)<\/p>\n<p>print(json_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>user_info<\/code>\u5b57\u5178\uff0c\u7136\u540e\u4f7f\u7528<code>json.dumps()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<code>json.dumps()<\/code>\u51fd\u6570\u4f1a\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\uff0c\u5e76\u8fd4\u56de\u8fd9\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5d4c\u5957\u5b57\u5178\u521b\u5efa\u590d\u6742\u7684JSON\u5bf9\u8c61<\/li>\n<\/ol>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u5f80\u5f80\u662f\u5d4c\u5957\u7684\u7ed3\u6784\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u5b57\u5178\u6765\u521b\u5efa\u66f4\u590d\u6742\u7684JSON\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>user_info = {<\/p>\n<p>    &quot;name&quot;: &quot;John Doe&quot;,<\/p>\n<p>    &quot;age&quot;: 30,<\/p>\n<p>    &quot;email&quot;: &quot;john.doe@example.com&quot;,<\/p>\n<p>    &quot;address&quot;: {<\/p>\n<p>        &quot;street&quot;: &quot;123 Main St&quot;,<\/p>\n<p>        &quot;city&quot;: &quot;Anytown&quot;,<\/p>\n<p>        &quot;state&quot;: &quot;CA&quot;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>json_string = json.dumps(user_info, indent=4)<\/p>\n<p>print(json_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>user_info<\/code>\u5b57\u5178\u4e2d\u5305\u542b\u4e86\u4e00\u4e2a\u5d4c\u5957\u7684<code>address<\/code>\u5b57\u5178\u3002\u4f7f\u7528<code>json.dumps()<\/code>\u65f6\uff0c\u6211\u4eec\u8fd8\u6307\u5b9a\u4e86<code>indent=4<\/code>\u53c2\u6570\uff0c\u4ee5\u751f\u6210\u66f4\u5177\u53ef\u8bfb\u6027\u7684\u683c\u5f0f\u5316JSON\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u521b\u5efaJSON<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u5b57\u5178\uff0cJSON\u8fd8\u652f\u6301\u5217\u8868\u7ed3\u6784\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5217\u8868\u6765\u521b\u5efaJSON\u6570\u7ec4\u3002<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u7b80\u5355\u7684JSON\u6570\u7ec4<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u521b\u5efa\u7b80\u5355JSON\u6570\u7ec4\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<p>json_string = json.dumps(fruits)<\/p>\n<p>print(json_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u6c34\u679c\u540d\u79f0\u7684\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528<code>json.dumps()<\/code>\u5c06\u5176\u8f6c\u6362\u4e3aJSON\u6570\u7ec4\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u521b\u5efa\u590d\u6742\u7684JSON\u6570\u7ec4<\/li>\n<\/ol>\n<p><p>\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0cJSON\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u53ef\u80fd\u662f\u590d\u6742\u7684\u5bf9\u8c61\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u7b80\u5355\u7684\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>users = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 30},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 35}<\/p>\n<p>]<\/p>\n<p>json_string = json.dumps(users, indent=4)<\/p>\n<p>print(json_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code>users<\/code>\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u591a\u4e2a\u5b57\u5178\uff0c\u6bcf\u4e2a\u5b57\u5178\u4ee3\u8868\u4e00\u4e2a\u7528\u6237\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u7ed3\u5408\u5b57\u5178\u548c\u5217\u8868\u521b\u5efaJSON<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u7ed3\u5408\u4f7f\u7528\u5b57\u5178\u548c\u5217\u8868\u6765\u521b\u5efaJSON\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li>\u590d\u6742\u6570\u636e\u7ed3\u6784\u793a\u4f8b<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7ed3\u5408\u5b57\u5178\u548c\u5217\u8868\u7684\u590d\u6742JSON\u7ed3\u6784\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {<\/p>\n<p>    &quot;company&quot;: &quot;TechCorp&quot;,<\/p>\n<p>    &quot;employees&quot;: [<\/p>\n<p>        {&quot;name&quot;: &quot;Alice&quot;, &quot;role&quot;: &quot;Engineer&quot;},<\/p>\n<p>        {&quot;name&quot;: &quot;Bob&quot;, &quot;role&quot;: &quot;Manager&quot;},<\/p>\n<p>        {&quot;name&quot;: &quot;Charlie&quot;, &quot;role&quot;: &quot;CEO&quot;}<\/p>\n<p>    ]<\/p>\n<p>}<\/p>\n<p>json_string = json.dumps(data, indent=4)<\/p>\n<p>print(json_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>data<\/code>\u5b57\u5178\u5305\u542b\u4e86\u4e00\u4e2a<code>employees<\/code>\u5217\u8868\uff0c\u8be5\u5217\u8868\u4e2d\u5305\u542b\u591a\u4e2a\u5b57\u5178\uff0c\u6bcf\u4e2a\u5b57\u5178\u8868\u793a\u4e00\u540d\u5458\u5de5\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5e94\u7528\u573a\u666f<\/li>\n<\/ol>\n<p><p>\u8fd9\u79cd\u7ed3\u5408\u5b57\u5178\u548c\u5217\u8868\u7684JSON\u7ed3\u6784\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u7279\u522b\u662f\u5728\u5904\u7406API\u54cd\u5e94\u3001\u914d\u7f6e\u6587\u4ef6\u548c\u6570\u636e\u4ea4\u6362\u683c\u5f0f\u65f6\u3002\u901a\u8fc7\u5408\u7406\u5730\u7ec4\u7ec7\u6570\u636e\u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001Python JSON\u6a21\u5757\u7684\u5176\u4ed6\u529f\u80fd<\/p>\n<\/p>\n<p><p>Python\u7684<code>json<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u6709\u7528\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5904\u7406JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5c06JSON\u5b57\u7b26\u4e32\u89e3\u6790\u4e3aPython\u5bf9\u8c61<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>json.loads()<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06JSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u89e3\u6790\u4e3aPython\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_string = &#39;{&quot;name&quot;: &quot;John Doe&quot;, &quot;age&quot;: 30}&#39;<\/p>\n<p>user_info = json.loads(json_string)<\/p>\n<p>print(user_info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>json.loads()<\/code>\u51fd\u6570\u5c06<code>json_string<\/code>\u89e3\u6790\u4e3a\u4e00\u4e2a\u5b57\u5178\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5c06Python\u5bf9\u8c61\u5199\u5165JSON\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>json.dump()<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06Python\u5bf9\u8c61\u5199\u5165JSON\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>user_info = {<\/p>\n<p>    &quot;name&quot;: &quot;John Doe&quot;,<\/p>\n<p>    &quot;age&quot;: 30,<\/p>\n<p>    &quot;email&quot;: &quot;john.doe@example.com&quot;<\/p>\n<p>}<\/p>\n<p>with open(&#39;user_info.json&#39;, &#39;w&#39;) as json_file:<\/p>\n<p>    json.dump(user_info, json_file, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06<code>user_info<\/code>\u5b57\u5178\u5199\u5165\u4e86\u4e00\u4e2a\u540d\u4e3a<code>user_info.json<\/code>\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u4eceJSON\u6587\u4ef6\u8bfb\u53d6Python\u5bf9\u8c61<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>json.load()<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4eceJSON\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\u5e76\u5c06\u5176\u89e3\u6790\u4e3aPython\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>with open(&#39;user_info.json&#39;, &#39;r&#39;) as json_file:<\/p>\n<p>    user_info = json.load(json_file)<\/p>\n<p>    print(user_info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4ece<code>user_info.json<\/code>\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\uff0c\u5e76\u5c06\u5176\u89e3\u6790\u4e3a\u4e00\u4e2a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001JSON\u5728Python\u4e2d\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>JSON\u5728Python\u4e2d\u6709\u8bb8\u591a\u5e94\u7528\u573a\u666f\uff0c\u7279\u522b\u662f\u5728\u6570\u636e\u4ea4\u6362\u548c\u5b58\u50a8\u65b9\u9762\u3002<\/p>\n<\/p>\n<ol>\n<li>API\u6570\u636e\u4ea4\u6362<\/li>\n<\/ol>\n<p><p>JSON\u662f\u73b0\u4ee3Web API\u7684\u6807\u51c6\u6570\u636e\u683c\u5f0f\u3002\u901a\u8fc7\u4f7f\u7528JSON\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u524d\u7aef\u548c\u540e\u7aef\u4e4b\u95f4\u4ea4\u6362\u6570\u636e\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>requests<\/code>\u5e93\u6765\u5904\u7406API\u8bf7\u6c42\uff0c\u5e76\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u89e3\u6790\u548c\u751f\u6210JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u914d\u7f6e\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>JSON\u662f\u4e00\u79cd\u975e\u5e38\u6d41\u884c\u7684\u914d\u7f6e\u6587\u4ef6\u683c\u5f0f\u3002\u5728Python\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528JSON\u6587\u4ef6\u6765\u5b58\u50a8\u914d\u7f6e\u4fe1\u606f\uff0c\u5e76\u5728\u5e94\u7528\u542f\u52a8\u65f6\u8bfb\u53d6\u8fd9\u4e9b\u914d\u7f6e\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u6570\u636e\u5b58\u50a8<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u636e\u5b58\u50a8\u9700\u6c42\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528JSON\u6587\u4ef6\u6765\u4fdd\u5b58\u6570\u636e\u3002JSON\u6587\u4ef6\u6613\u4e8e\u9605\u8bfb\u548c\u7f16\u8f91\uff0c\u975e\u5e38\u9002\u5408\u5b58\u50a8\u7ed3\u6784\u5316\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u5728Python\u4e2d\u521b\u5efaJSON\u6570\u636e\u3002\u4f7f\u7528Python\u7684\u5b57\u5178\u548c\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u5404\u79cd\u590d\u6742\u7684JSON\u7ed3\u6784\u3002\u540c\u65f6\uff0cPython\u7684<code>json<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u89e3\u6790\u548c\u751f\u6210JSON\u6570\u636e\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cJSON\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u7684\u6570\u636e\u683c\u5f0f\uff0c\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u4ea4\u6362\u548c\u5b58\u50a8\u3002\u901a\u8fc7\u638c\u63e1JSON\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\uff0c\u63d0\u9ad8\u5e94\u7528\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684JSON\u5bf9\u8c61\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u6765\u521b\u5efaJSON\u5bf9\u8c61\u3002\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2aPython\u5b57\u5178\uff0c\u7136\u540e\u4f7f\u7528<code>json.dumps()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\ndata = {\n    &quot;name&quot;: &quot;Alice&quot;,\n    &quot;age&quot;: 30,\n    &quot;is_student&quot;: False\n}\n\njson_data = json.dumps(data)\nprint(json_data)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u4f1a\u8f93\u51fa\u4e00\u4e2aJSON\u5b57\u7b26\u4e32\uff1a<code>{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30, &quot;is_student&quot;: false}<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06JSON\u6570\u636e\u5199\u5165\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5c06JSON\u6570\u636e\u5199\u5165\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json.dump()<\/code>\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u53ef\u4ee5\u5c06Python\u5bf9\u8c61\u76f4\u63a5\u5199\u5165\u6587\u4ef6\u5e76\u4ee5JSON\u683c\u5f0f\u4fdd\u5b58\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\ndata = {\n    &quot;name&quot;: &quot;Bob&quot;,\n    &quot;age&quot;: 25,\n    &quot;is_student&quot;: True\n}\n\nwith open(&#39;data.json&#39;, &#39;w&#39;) as json_file:\n    json.dump(data, json_file)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u540e\uff0c\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>data.json<\/code>\u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542bJSON\u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u4eceJSON\u5b57\u7b26\u4e32\u52a0\u8f7d\u6570\u636e\u5230Python\u5bf9\u8c61\uff1f<\/strong><br \/>\u4f7f\u7528<code>json.loads()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\njson_string = &#39;{&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 28, &quot;is_student&quot;: false}&#39;\ndata = json.loads(json_string)\n\nprint(data)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7684<code>data<\/code>\u5c06\u662f\u4e00\u4e2a\u5b57\u5178\uff1a<code>{&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 28, &#39;is_student&#39;: False}<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u521b\u5efaJSON\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u5178\u521b\u5efa\u3001\u4f7f\u7528\u5217\u8868\u521b\u5efa\u3001\u7ed3\u5408\u5b57\u5178\u548c\u5217\u8868\u521b\u5efa\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u89c1 [&hellip;]","protected":false},"author":3,"featured_media":935693,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/935690"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=935690"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/935690\/revisions"}],"predecessor-version":[{"id":935694,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/935690\/revisions\/935694"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/935693"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=935690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=935690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=935690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}