{"id":1070204,"date":"2025-01-08T10:56:02","date_gmt":"2025-01-08T02:56:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1070204.html"},"modified":"2025-01-08T10:56:04","modified_gmt":"2025-01-08T02:56:04","slug":"python%e7%9a%84json%e5%a6%82%e4%bd%95%e8%bd%ac%e6%8d%a2%e6%88%90%e5%88%97%e8%a1%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1070204.html","title":{"rendered":"python\u7684json\u5982\u4f55\u8f6c\u6362\u6210\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101009\/fd565078-d487-4438-a90a-278d2f826951.webp\" alt=\"python\u7684json\u5982\u4f55\u8f6c\u6362\u6210\u5217\u8868\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528json\u6a21\u5757\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u5217\u8868\u3001\u4f7f\u7528json.loads()\u65b9\u6cd5\u3001\u89e3\u6790\u5d4c\u5957JSON\u3001\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528json.loads()\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u548c\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2aJSON\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u4e00\u4e2aPython\u7684\u6570\u636e\u7ed3\u6784\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528json.loads()\u65b9\u6cd5\uff1a<\/strong> \u8fd9\u662fPython\u5185\u7f6e\u7684json\u6a21\u5757\u63d0\u4f9b\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684Python\u6570\u636e\u7ed3\u6784\uff0c\u5305\u62ec\u5217\u8868\u3001\u5b57\u5178\u7b49\u3002json.loads()\u65b9\u6cd5\u5c06JSON\u5b57\u7b26\u4e32\u89e3\u6790\u4e3aPython\u5bf9\u8c61\uff0c\u53ef\u4ee5\u662f\u5217\u8868\u3001\u5b57\u5178\u3001\u5b57\u7b26\u4e32\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f8b\u5982\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_string = &#39;[1, 2, 3, 4]&#39;<\/p>\n<p>python_list = json.loads(json_string)<\/p>\n<p>print(python_list)<\/p>\n<h2><strong>\u8f93\u51fa: [1, 2, 3, 4]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u5e76\u5904\u7406\u66f4\u591a\u590d\u6742\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165JSON\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5904\u7406JSON\u6570\u636e\u9700\u8981\u5bfc\u5165json\u6a21\u5757\uff0c\u8fd9\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5c06\u7b80\u5355JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528json.loads()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528json.loads()\uff0c\u5b83\u53ef\u4ee5\u5c06JSON\u5b57\u7b26\u4e32\u89e3\u6790\u4e3aPython\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">json_string = &#39;[1, 2, 3, 4]&#39;<\/p>\n<p>python_list = json.loads(json_string)<\/p>\n<p>print(python_list)<\/p>\n<h2><strong>\u8f93\u51fa: [1, 2, 3, 4]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u5305\u542b\u5b57\u7b26\u4e32\u7684\u5217\u8868<\/h4>\n<\/p>\n<p><p>JSON\u5b57\u7b26\u4e32\u4e5f\u53ef\u4ee5\u5305\u542b\u5b57\u7b26\u4e32\u5143\u7d20\uff0c\u8fd9\u65f6\u8f6c\u6362\u540e\u4f1a\u5f97\u5230\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">json_string = &#39;[&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]&#39;<\/p>\n<p>python_list = json.loads(json_string)<\/p>\n<p>print(python_list)<\/p>\n<h2><strong>\u8f93\u51fa: [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u6790\u5d4c\u5957\u7684JSON\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>JSON\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u662f\u5d4c\u5957\u7684\uff0c\u5373\u5217\u8868\u4e2d\u5305\u542b\u5b57\u5178\uff0c\u6216\u8005\u5b57\u5178\u4e2d\u5305\u542b\u5217\u8868\u7b49\u3002\u4f7f\u7528json.loads()\u540c\u6837\u53ef\u4ee5\u89e3\u6790\u8fd9\u4e9b\u590d\u6742\u7684\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5d4c\u5957\u5217\u8868<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">json_string = &#39;[1, [2, 3], [4, 5]]&#39;<\/p>\n<p>python_list = json.loads(json_string)<\/p>\n<p>print(python_list)<\/p>\n<h2><strong>\u8f93\u51fa: [1, [2, 3], [4, 5]]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5217\u8868\u4e2d\u5305\u542b\u5b57\u5178<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">json_string = &#39;[{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30}, {&quot;name&quot;: &quot;Anna&quot;, &quot;age&quot;: 25}]&#39;<\/p>\n<p>python_list = json.loads(json_string)<\/p>\n<p>print(python_list)<\/p>\n<h2><strong>\u8f93\u51fa: [{&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 30}, {&#39;name&#39;: &#39;Anna&#39;, &#39;age&#39;: 25}]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef<\/h3>\n<\/p>\n<p><p>\u5728\u89e3\u6790JSON\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u9519\u8bef\u3002\u4f8b\u5982\uff0cJSON\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u6216\u8005\u5305\u542b\u4e0d\u652f\u6301\u7684\u6570\u636e\u7c7b\u578b\u3002\u9700\u8981\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6355\u83b7JSONDecodeError<\/h4>\n<\/p>\n<p><p>\u5f53JSON\u5b57\u7b26\u4e32\u683c\u5f0f\u4e0d\u6b63\u786e\u65f6\uff0cjson.loads()\u4f1a\u629b\u51fajson.JSONDecodeError\u5f02\u5e38\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e2a\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">json_string = &#39;[1, 2, 3, 4&#39;<\/p>\n<p>try:<\/p>\n<p>    python_list = json.loads(json_string)<\/p>\n<p>except json.JSONDecodeError as e:<\/p>\n<p>    print(f&quot;JSONDecodeError: {e}&quot;)<\/p>\n<h2><strong>\u8f93\u51fa: JSONDecodeError: Expecting &#39;,&#39; delimiter: line 1 column 10 (char 9)<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u975e\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6570\u636e<\/h4>\n<\/p>\n<p><p>json.loads()\u65b9\u6cd5\u53ea\u80fd\u89e3\u6790\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5982\u679c\u4f20\u5165\u7684\u662f\u975e\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6570\u636e\uff08\u4f8b\u5982\u5b57\u5178\u3001\u5217\u8868\u7b49\uff09\uff0c\u5219\u4f1a\u5f15\u53d1TypeError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">json_data = [1, 2, 3, 4]<\/p>\n<p>try:<\/p>\n<p>    python_list = json.loads(json_data)<\/p>\n<p>except TypeError as e:<\/p>\n<p>    print(f&quot;TypeError: {e}&quot;)<\/p>\n<h2><strong>\u8f93\u51fa: TypeError: the JSON object must be str, bytes or bytearray, not list<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5c06Python\u5217\u8868\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5217\u8868\uff0c\u6709\u65f6\u4e5f\u9700\u8981\u5c06Python\u5217\u8868\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u3002\u8fd9\u53ef\u4ee5\u4f7f\u7528json.dumps()\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">python_list = [1, 2, 3, 4]<\/p>\n<p>json_string = json.dumps(python_list)<\/p>\n<p>print(json_string)<\/p>\n<h2><strong>\u8f93\u51fa: &quot;[1, 2, 3, 4]&quot;<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u5d4c\u5957\u5217\u8868\u548c\u5b57\u5178<\/h4>\n<\/p>\n<p><p>json.dumps()\u65b9\u6cd5\u540c\u6837\u53ef\u4ee5\u5904\u7406\u5d4c\u5957\u7684\u5217\u8868\u548c\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">python_data = [1, {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30}, [2, 3]]<\/p>\n<p>json_string = json.dumps(python_data)<\/p>\n<p>print(json_string)<\/p>\n<h2><strong>\u8f93\u51fa: &#39;[1, {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30}, [2, 3]]&#39;<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5904\u7406JSON\u5b57\u7b26\u4e32\uff0c\u6709\u65f6\u9700\u8981\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e\u6216\u8005\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528json.load()\u65b9\u6cd5\u53ef\u4ee5\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e\u5e76\u8f6c\u6362\u4e3aPython\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;data.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<p>    print(data)<\/p>\n<h2><strong>\u5047\u8bbedata.json\u6587\u4ef6\u5185\u5bb9\u4e3a: [1, 2, 3, 4]<\/strong><\/h2>\n<h2><strong>\u8f93\u51fa: [1, 2, 3, 4]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5c06\u6570\u636e\u5199\u5165JSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528json.dump()\u65b9\u6cd5\u53ef\u4ee5\u5c06Python\u6570\u636e\u7ed3\u6784\u5199\u5165\u5230JSON\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4]<\/p>\n<p>with open(&#39;output.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(data, file)<\/p>\n<h2><strong>\u8fd9\u4f1a\u5c06\u6570\u636e\u5199\u5165\u5230output.json\u6587\u4ef6\u4e2d<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406JSON\u6570\u636e\u4e2d\u7684\u65e5\u671f\u548c\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>JSON\u683c\u5f0f\u4e0d\u652f\u6301\u76f4\u63a5\u8868\u793a\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u53ef\u4ee5\u5c06\u65e5\u671f\u548c\u65f6\u95f4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u8fdb\u884c\u5904\u7406\uff0c\u6216\u8005\u4f7f\u7528\u4e00\u4e9b\u5e93\u6765\u8f85\u52a9\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u5c06\u65e5\u671f\u548c\u65f6\u95f4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u518d\u8fdb\u884cJSON\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import json<\/p>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: &quot;John&quot;,<\/p>\n<p>    &quot;birthdate&quot;: datetime.now().isoformat()<\/p>\n<p>}<\/p>\n<p>json_string = json.dumps(data)<\/p>\n<p>print(json_string)<\/p>\n<h2><strong>\u8f93\u51fa: {&quot;name&quot;: &quot;John&quot;, &quot;birthdate&quot;: &quot;2023-10-12T15:30:00&quot;}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u8f85\u52a9\u5e93<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u5982<code>pytz<\/code>\u3001<code>dateutil<\/code>\u7b49\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>from dateutil import parser<\/p>\n<p>import json<\/p>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: &quot;John&quot;,<\/p>\n<p>    &quot;birthdate&quot;: datetime.now().isoformat()<\/p>\n<p>}<\/p>\n<p>json_string = json.dumps(data)<\/p>\n<p>parsed_data = json.loads(json_string)<\/p>\n<p>parsed_data[&#39;birthdate&#39;] = parser.isoparse(parsed_data[&#39;birthdate&#39;])<\/p>\n<p>print(parsed_data)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;name&#39;: &#39;John&#39;, &#39;birthdate&#39;: datetime.datetime(2023, 10, 12, 15, 30)}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5904\u7406JSON\u6570\u636e\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u4f7f\u7528\u5185\u7f6e\u7684json\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5217\u8868\uff0c\u4ee5\u53ca\u5c06Python\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u638c\u63e1json.loads()\u548cjson.dumps()\u65b9\u6cd5\uff0c\u5904\u7406\u5d4c\u5957\u7684JSON\u7ed3\u6784\uff0c\u6355\u83b7\u548c\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\uff0c\u4ee5\u53ca\u5904\u7406JSON\u6587\u4ef6\u548c\u65e5\u671f\u65f6\u95f4\u6570\u636e\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5b8c\u6210JSON\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u6765\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>json.loads()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3002\u5982\u679cJSON\u5b57\u7b26\u4e32\u8868\u793a\u4e00\u4e2a\u6570\u7ec4\uff0c\u7ed3\u679c\u5c06\u662f\u4e00\u4e2a\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import json\n\njson_string = &#39;[&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]&#39;\nlist_data = json.loads(json_string)\nprint(list_data)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]\n<\/code><\/pre>\n<p><strong>\u5728\u5904\u7406JSON\u6570\u636e\u65f6\uff0c\u5982\u4f55\u907f\u514d\u8f6c\u6362\u9519\u8bef\uff1f<\/strong><br \/>\u4e3a\u4e86\u907f\u514d\u5728\u8f6c\u6362JSON\u6570\u636e\u65f6\u51fa\u73b0\u9519\u8bef\uff0c\u786e\u4fdd\u60a8\u63d0\u4f9b\u7684\u5b57\u7b26\u4e32\u7b26\u5408JSON\u683c\u5f0f\u3002\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u4f7f\u7528\u5355\u5f15\u53f7\u800c\u975e\u53cc\u5f15\u53f7\uff0c\u4ee5\u53ca\u7f3a\u5c11\u5fc5\u8981\u7684\u62ec\u53f7\u3002\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u53ef\u4ee5\u5e2e\u52a9\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import json\n\njson_string = &#39;[&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;&#39;\ntry:\n    list_data = json.loads(json_string)\nexcept json.JSONDecodeError as e:\n    print(&quot;JSON\u89e3\u7801\u9519\u8bef:&quot;, e)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5c06Python\u5217\u8868\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u60f3\u5c06Python\u5217\u8868\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json.dumps()<\/code>\u65b9\u6cd5\u3002\u8fd9\u5bf9\u4e8e\u5c06\u6570\u636e\u53d1\u9001\u5230Web\u670d\u52a1\u6216\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import json\n\nlist_data = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]\njson_string = json.dumps(list_data)\nprint(json_string)  # \u8f93\u51fa: &#39;[&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]&#39;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528json\u6a21\u5757\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u5217\u8868\u3001\u4f7f\u7528json.loads()\u65b9\u6cd5\u3001\u89e3\u6790\u5d4c\u5957J [&hellip;]","protected":false},"author":3,"featured_media":1070211,"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\/1070204"}],"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=1070204"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1070204\/revisions"}],"predecessor-version":[{"id":1070213,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1070204\/revisions\/1070213"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1070211"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1070204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1070204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1070204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}