{"id":1076445,"date":"2025-01-08T11:53:25","date_gmt":"2025-01-08T03:53:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1076445.html"},"modified":"2025-01-08T11:53:28","modified_gmt":"2025-01-08T03:53:28","slug":"python%e5%ba%93json%e5%a6%82%e4%bd%95%e8%af%bb%e6%95%b0%e6%8d%ae%e5%ba%93-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1076445.html","title":{"rendered":"python\u5e93json\u5982\u4f55\u8bfb\u6570\u636e\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181012\/3cab5725-e8b0-4642-a23e-9e3095da80c8.webp\" alt=\"python\u5e93json\u5982\u4f55\u8bfb\u6570\u636e\u5e93\" \/><\/p>\n<p><p> <strong>Python\u5e93json\u5982\u4f55\u8bfb\u6570\u636e\u5e93<\/strong>\uff1aPython\u4e2d\u7684json\u5e93\u65e0\u6cd5\u76f4\u63a5\u8bfb\u53d6\u6570\u636e\u5e93\uff0c\u9700\u8981\u7ed3\u5408\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff08\u5982sqlite3\u3001pymysql\u3001psycopg2\uff09\u8fdb\u884c\u64cd\u4f5c\u3001\u9996\u5148\u8fde\u63a5\u6570\u636e\u5e93\u3001\u7136\u540e\u5c06\u6570\u636e\u8f6c\u6362\u4e3ajson\u683c\u5f0f\u5b58\u50a8\u3002<strong>\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u8fdb\u884c\u8fde\u63a5\u3001\u8bfb\u53d6\u6570\u636e\u5e76\u8f6c\u6362\u4e3ajson\u683c\u5f0f\u3001\u4f7f\u7528json\u5e93\u8fdb\u884c\u6570\u636e\u5e8f\u5217\u5316<\/strong>\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u5b9e\u4f8b\u5b9e\u73b0\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u8fdb\u884c\u8fde\u63a5<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528json\u5e93\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff0c\u5e76\u901a\u8fc7\u5b83\u6765\u8fde\u63a5\u5230\u6570\u636e\u5e93\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff0c\u5982sqlite3\u3001pymysql\u3001psycopg2\u7b49\uff0c\u4e0b\u9762\u6211\u4eec\u5c06\u5206\u522b\u4ecb\u7ecd\u5b83\u4eec\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528sqlite3\u8fde\u63a5SQLite\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>SQLite\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u5e93\uff0c\u9002\u5408\u5b58\u50a8\u5c11\u91cf\u6570\u636e\u3002Python\u7684\u6807\u51c6\u5e93\u4e2d\u5305\u542b\u4e86sqlite3\u6a21\u5757\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884cSQL\u67e5\u8be2<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT * FROM users&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u7ed3\u679c<\/strong><\/h2>\n<p>rows = cursor.fetchall()<\/p>\n<h2><strong>\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8bfb\u53d6\u6570\u636e\u5e76\u8f6c\u6362\u4e3ajson\u683c\u5f0f<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u65e6\u6211\u4eec\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u4e86\u6570\u636e\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u5c06\u5176\u8f6c\u6362\u4e3ajson\u683c\u5f0f\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528json\u5e93\u4e2d\u7684<code>dumps<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u5c06\u6570\u636e\u5e93\u7ed3\u679c\u8f6c\u6362\u4e3ajson<\/strong><\/h2>\n<p>def convert_to_json(data):<\/p>\n<p>    columns = [&quot;id&quot;, &quot;name&quot;, &quot;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&quot;]  # \u5047\u8bbe\u8868\u4e2d\u7684\u5217\u540d<\/p>\n<p>    result = []<\/p>\n<p>    for row in data:<\/p>\n<p>        result.append(dict(zip(columns, row)))<\/p>\n<p>    return json.dumps(result, indent=4)<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [(1, &quot;John Doe&quot;, &quot;john@example.com&quot;), (2, &quot;Jane Doe&quot;, &quot;jane@example.com&quot;)]<\/p>\n<h2><strong>\u8f6c\u6362\u4e3ajson<\/strong><\/h2>\n<p>json_data = convert_to_json(data)<\/p>\n<p>print(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pymysql\u8fde\u63a5MySQL\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>MySQL\u662f\u4e00\u79cd\u6d41\u884c\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528pymysql\u5e93\u6765\u8fde\u63a5MySQL\u6570\u636e\u5e93\u3002\u9700\u8981\u5148\u5b89\u88c5pymysql\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install pymysql<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<h2><strong>\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = pymysql.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;yourusername&#39;,<\/p>\n<p>    password=&#39;yourpassword&#39;,<\/p>\n<p>    database=&#39;yourdatabase&#39;<\/p>\n<p>)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884cSQL\u67e5\u8be2<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT * FROM users&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u7ed3\u679c<\/strong><\/h2>\n<p>rows = cursor.fetchall()<\/p>\n<h2><strong>\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<h2><strong>\u8f6c\u6362\u4e3ajson<\/strong><\/h2>\n<p>json_data = convert_to_json(rows)<\/p>\n<p>print(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528psycopg2\u8fde\u63a5PostgreSQL\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>PostgreSQL\u662f\u4e00\u79cd\u529f\u80fd\u5f3a\u5927\u7684\u5f00\u6e90\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528psycopg2\u5e93\u6765\u8fde\u63a5PostgreSQL\u6570\u636e\u5e93\u3002\u9700\u8981\u5148\u5b89\u88c5psycopg2\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install psycopg2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<h2><strong>\u8fde\u63a5\u5230PostgreSQL\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = psycopg2.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;yourusername&#39;,<\/p>\n<p>    password=&#39;yourpassword&#39;,<\/p>\n<p>    dbname=&#39;yourdatabase&#39;<\/p>\n<p>)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884cSQL\u67e5\u8be2<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT * FROM users&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u7ed3\u679c<\/strong><\/h2>\n<p>rows = cursor.fetchall()<\/p>\n<h2><strong>\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<h2><strong>\u8f6c\u6362\u4e3ajson<\/strong><\/h2>\n<p>json_data = convert_to_json(rows)<\/p>\n<p>print(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u7ed3\u6784\u53ef\u80fd\u4f1a\u6bd4\u8f83\u590d\u6742\uff0c\u5305\u62ec\u5d4c\u5957\u5173\u7cfb\u3001\u591a\u8868\u5173\u8054\u7b49\u3002\u6211\u4eec\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\uff0c\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\uff0c\u7136\u540e\u518d\u8f6c\u6362\u4e3ajson\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u591a\u8868\u5173\u8054\u67e5\u8be2<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u5728\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e2d\uff0c\u591a\u8868\u5173\u8054\u67e5\u8be2\u662f\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u4e0b\u9762\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u8fdb\u884c\u591a\u8868\u5173\u8054\u67e5\u8be2\uff0c\u5e76\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3ajson\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>import json<\/p>\n<h2><strong>\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884c\u591a\u8868\u5173\u8054\u67e5\u8be2<\/strong><\/h2>\n<p>cursor.execute(&quot;&quot;&quot;<\/p>\n<p>SELECT users.id, users.name, orders.order_id, orders.amount<\/p>\n<p>FROM users<\/p>\n<p>JOIN orders ON users.id = orders.user_id<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u7ed3\u679c<\/strong><\/h2>\n<p>rows = cursor.fetchall()<\/p>\n<h2><strong>\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<h2><strong>\u8f6c\u6362\u4e3ajson<\/strong><\/h2>\n<p>columns = [&quot;id&quot;, &quot;name&quot;, &quot;order_id&quot;, &quot;amount&quot;]<\/p>\n<p>result = []<\/p>\n<p>for row in rows:<\/p>\n<p>    result.append(dict(zip(columns, row)))<\/p>\n<p>json_data = json.dumps(result, indent=4)<\/p>\n<p>print(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5904\u7406\u5d4c\u5957\u5173\u7cfb<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u5c06\u5d4c\u5957\u5173\u7cfb\u7684\u6570\u636e\u8f6c\u6362\u4e3ajson\u683c\u5f0f\u3002\u4e0b\u9762\u7684\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5904\u7406\u5d4c\u5957\u5173\u7cfb\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>import json<\/p>\n<h2><strong>\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884c\u67e5\u8be2\u83b7\u53d6\u7528\u6237\u4fe1\u606f<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT id, name FROM users&quot;)<\/p>\n<p>users = cursor.fetchall()<\/p>\n<h2><strong>\u83b7\u53d6\u6bcf\u4e2a\u7528\u6237\u7684\u8ba2\u5355\u4fe1\u606f<\/strong><\/h2>\n<p>user_data = []<\/p>\n<p>for user in users:<\/p>\n<p>    user_id, user_name = user<\/p>\n<p>    cursor.execute(&quot;SELECT order_id, amount FROM orders WHERE user_id = ?&quot;, (user_id,))<\/p>\n<p>    orders = cursor.fetchall()<\/p>\n<p>    user_data.append({<\/p>\n<p>        &quot;id&quot;: user_id,<\/p>\n<p>        &quot;name&quot;: user_name,<\/p>\n<p>        &quot;orders&quot;: [{&quot;order_id&quot;: order[0], &quot;amount&quot;: order[1]} for order in orders]<\/p>\n<p>    })<\/p>\n<h2><strong>\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<h2><strong>\u8f6c\u6362\u4e3ajson<\/strong><\/h2>\n<p>json_data = json.dumps(user_data, indent=4)<\/p>\n<p>print(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6570\u636e\u9a8c\u8bc1\u4e0e\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u9a8c\u8bc1\u4e0e\u9519\u8bef\u5904\u7406\u975e\u5e38\u91cd\u8981\u3002\u6211\u4eec\u9700\u8981\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u6b63\u786e\u6027\uff0c\u540c\u65f6\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p><strong>\u6570\u636e\u9a8c\u8bc1<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u5728\u5c06\u6570\u636e\u8f6c\u6362\u4e3ajson\u4e4b\u524d\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u9a8c\u8bc1\uff0c\u786e\u4fdd\u5176\u7b26\u5408\u9884\u671f\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def validate_data(data):<\/p>\n<p>    for user in data:<\/p>\n<p>        if not isinstance(user[&quot;id&quot;], int) or user[&quot;id&quot;] &lt;= 0:<\/p>\n<p>            raise ValueError(&quot;Invalid user ID&quot;)<\/p>\n<p>        if not isinstance(user[&quot;name&quot;], str) or not user[&quot;name&quot;]:<\/p>\n<p>            raise ValueError(&quot;Invalid user name&quot;)<\/p>\n<p>        for order in user[&quot;orders&quot;]:<\/p>\n<p>            if not isinstance(order[&quot;order_id&quot;], int) or order[&quot;order_id&quot;] &lt;= 0:<\/p>\n<p>                raise ValueError(&quot;Invalid order ID&quot;)<\/p>\n<p>            if not isinstance(order[&quot;amount&quot;], float) or order[&quot;amount&quot;] &lt;= 0:<\/p>\n<p>                raise ValueError(&quot;Invalid order amount&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>user_data = [<\/p>\n<p>    {&quot;id&quot;: 1, &quot;name&quot;: &quot;John Doe&quot;, &quot;orders&quot;: [{&quot;order_id&quot;: 101, &quot;amount&quot;: 99.99}]},<\/p>\n<p>    {&quot;id&quot;: 2, &quot;name&quot;: &quot;Jane Doe&quot;, &quot;orders&quot;: [{&quot;order_id&quot;: 102, &quot;amount&quot;: 49.99}]}<\/p>\n<p>]<\/p>\n<h2><strong>\u9a8c\u8bc1\u6570\u636e<\/strong><\/h2>\n<p>validate_data(user_data)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3ajson<\/strong><\/h2>\n<p>json_data = json.dumps(user_data, indent=4)<\/p>\n<p>print(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u9519\u8bef\u5904\u7406<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u548c\u6570\u636e\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u5404\u79cd\u9519\u8bef\u3002\u6211\u4eec\u9700\u8981\u901a\u8fc7\u9519\u8bef\u5904\u7406\u673a\u5236\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>import json<\/p>\n<p>try:<\/p>\n<p>    # \u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93<\/p>\n<p>    conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    # \u6267\u884c\u67e5\u8be2<\/p>\n<p>    cursor.execute(&quot;SELECT id, name FROM users&quot;)<\/p>\n<p>    users = cursor.fetchall()<\/p>\n<p>    # \u83b7\u53d6\u6bcf\u4e2a\u7528\u6237\u7684\u8ba2\u5355\u4fe1\u606f<\/p>\n<p>    user_data = []<\/p>\n<p>    for user in users:<\/p>\n<p>        user_id, user_name = user<\/p>\n<p>        cursor.execute(&quot;SELECT order_id, amount FROM orders WHERE user_id = ?&quot;, (user_id,))<\/p>\n<p>        orders = cursor.fetchall()<\/p>\n<p>        user_data.append({<\/p>\n<p>            &quot;id&quot;: user_id,<\/p>\n<p>            &quot;name&quot;: user_name,<\/p>\n<p>            &quot;orders&quot;: [{&quot;order_id&quot;: order[0], &quot;amount&quot;: order[1]} for order in orders]<\/p>\n<p>        })<\/p>\n<p>    # \u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/p>\n<p>    cursor.close()<\/p>\n<p>    conn.close()<\/p>\n<p>    # \u8f6c\u6362\u4e3ajson<\/p>\n<p>    json_data = json.dumps(user_data, indent=4)<\/p>\n<p>    print(json_data)<\/p>\n<p>except sqlite3.Error as e:<\/p>\n<p>    print(f&quot;SQLite error: {e}&quot;)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(f&quot;Data validation error: {e}&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;Unexpected error: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u80fd\u591f\u4f7f\u7528Python\u7684json\u5e93\u7ed3\u5408\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff0c\u6210\u529f\u5730\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3ajson\u683c\u5f0f\u3002\u786e\u4fdd\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u3001\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u4e0e\u9519\u8bef\u5904\u7406\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u9760\u6027\u548c\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5e93json\u8bfb\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u5e93json\u8bfb\u53d6\u6570\u636e\u5e93\u6570\u636e\u4e4b\u524d\uff0c\u9700\u8981\u5148\u4ece\u6570\u636e\u5e93\u4e2d\u63d0\u53d6\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528\u5982sqlite3\u3001pymysql\u7b49\u5e93\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u5e76\u6267\u884cSQL\u67e5\u8be2\u3002\u83b7\u53d6\u7684\u6570\u636e\u901a\u5e38\u662f\u4ee5\u5b57\u5178\u6216\u5217\u8868\u7684\u5f62\u5f0f\u8fd4\u56de\uff0c\u63a5\u7740\u53ef\u4ee5\u4f7f\u7528json\u5e93\u5c06\u5176\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>json.dumps()<\/code>\u65b9\u6cd5\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u5728\u8bfb\u53d6\u6570\u636e\u5e93\u6570\u636e\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7a7a\u503c\u6216\u7f3a\u5931\u503c\uff1f<\/strong><br \/>\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u7a7a\u503c\u6216\u7f3a\u5931\u503c\u662f\u5e38\u89c1\u7684\u60c5\u51b5\u3002\u53ef\u4ee5\u5728\u63d0\u53d6\u6570\u636e\u540e\uff0c\u4f7f\u7528Python\u7684\u6761\u4ef6\u8bed\u53e5\u68c0\u67e5\u8fd9\u4e9b\u503c\uff0c\u5e76\u6839\u636e\u9700\u6c42\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a<code>None<\/code>\uff0c\u6216\u7528\u7279\u5b9a\u7684\u5360\u4f4d\u7b26\u66ff\u4ee3\u3002\u5728\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u4e4b\u524d\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\uff0c\u4ee5\u907f\u514d\u5728\u540e\u7eed\u5904\u7406\u4e2d\u51fa\u73b0\u9519\u8bef\u3002<\/p>\n<p><strong>JSON\u683c\u5f0f\u7684\u6570\u636e\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u4f7f\u7528JSON\u683c\u5f0f\u5b58\u50a8\u6570\u636e\u5728\u6570\u636e\u5e93\u4e2d\u53ef\u4ee5\u63d0\u9ad8\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002\u7531\u4e8eJSON\u652f\u6301\u5d4c\u5957\u7ed3\u6784\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b58\u50a8\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\u3002\u540c\u65f6\uff0c\u8bb8\u591a\u73b0\u4ee3\u6570\u636e\u5e93\uff08\u5982PostgreSQL\u548cMongoDB\uff09\u539f\u751f\u652f\u6301JSON\u683c\u5f0f\uff0c\u8fd9\u4f7f\u5f97\u5728\u67e5\u8be2\u548c\u66f4\u65b0\u65f6\u66f4\u52a0\u9ad8\u6548\u3002\u6b64\u5916\uff0cJSON\u683c\u5f0f\u6613\u4e8e\u4e0eWeb\u5e94\u7528\u7a0b\u5e8f\u4ea4\u4e92\uff0c\u7279\u522b\u662f\u5728\u5904\u7406API\u8bf7\u6c42\u548c\u54cd\u5e94\u65f6\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5e93json\u5982\u4f55\u8bfb\u6570\u636e\u5e93\uff1aPython\u4e2d\u7684json\u5e93\u65e0\u6cd5\u76f4\u63a5\u8bfb\u53d6\u6570\u636e\u5e93\uff0c\u9700\u8981\u7ed3\u5408\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff08\u5982sq [&hellip;]","protected":false},"author":3,"featured_media":1076456,"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\/1076445"}],"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=1076445"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076445\/revisions"}],"predecessor-version":[{"id":1076459,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076445\/revisions\/1076459"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1076456"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1076445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1076445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1076445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}