{"id":188603,"date":"2022-03-14T16:21:29","date_gmt":"2022-03-14T08:21:29","guid":{"rendered":"https:\/\/www.idc.net\/help\/188603\/"},"modified":"2022-03-14T16:21:29","modified_gmt":"2022-03-14T08:21:29","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91json%e6%95%99%e7%a8%8b%ef%bc%9a%e5%9c%a8-python3-%e4%b8%ad%e4%bd%bf%e7%94%a8-json","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/188603\/","title":{"rendered":"\u540e\u6d6a\u4e91JSON\u6559\u7a0b\uff1a\u5728 Python3 \u4e2d\u4f7f\u7528 JSON"},"content":{"rendered":"<p>\u672c\u6559\u7a0b\u5c06\u4f1a\u6559\u6211\u4eec\u5982\u4f55\u4f7f\u7528&nbsp;Python&nbsp;\u7f16\u7a0b\u8bed\u8a00\u7f16\u7801\u548c\u89e3\u7801 JSON\u3002<\/p>\n<h2>\u73af\u5883<\/h2>\n<p>\u5728python3\u4e2d\uff08\u4ee5\u53capython2.6\u7248\u672c\u540e\u7684python2\u7248\u672c\uff09\u5185\u7f6e\u4e86JSON\u6a21\u5757\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u53e6\u5916\u7684JSON\u6a21\u5757\u3002<\/p>\n<h2>\u7b80\u4ecb<\/h2>\n<p>JSON\u6a21\u5757\u662fpython\u5185\u7f6e\u7684\u7528\u6765\u8fdb\u884cpython\u5bf9\u8c61\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684\u6a21\u5757\u3002<\/p>\n<blockquote><p>\n  &nbsp;\u5e8f\u5217\u5316\uff0c\u6307\u5c06python\u5bf9\u8c61\u8f6c\u6362\u4e3ajson\u683c\u5f0f\u7684\u6570\u636e\u6d41\uff0c\u53cd\u5e8f\u5217\u5316\u5219\u662f\u5c06json\u683c\u5f0f\u7684\u6570\u636e\u6d41\u8f6c\u6362\u4e3apython\u5bf9\u8c61\u3002\n<\/p><\/blockquote>\n<p>\u8be5\u6a21\u5757\u4e2d\u5e38\u7528\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u56db\u4e2a\uff1a<\/p>\n<ul>\n<li>\u200bjson.dump\u200b&nbsp; \u5c06Python\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3aJson\u683c\u5f0f\u7684\u6570\u636e\u6d41\u5e76\u5199\u5165\u6587\u4ef6\u7c7b\u578b\u7684\u5bf9\u8c61\u4e2d<\/li>\n<\/ul>\n<ul>\n<li>\u200bjson.dumps\u200b&nbsp; \u5c06Python\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3aJson\u683c\u5f0f\u7684\u5b57\u7b26\u4e32<\/li>\n<\/ul>\n<ul>\n<li>\u200bjson.load\u200b&nbsp; \u4ece\u6587\u4ef6\u7c7b\u578b\u7684\u5bf9\u8c61\u4e2d\u8bfb\u53d6Json\u683c\u5f0f\u7684\u6570\u636e\u5e76\u53cd\u5e8f\u5217\u5316\u6210Python\u5bf9\u8c61<\/li>\n<\/ul>\n<ul>\n<li>\u200bjson.loads&nbsp;\u200b \u5c06\u5305\u542bJson\u683c\u5f0f\u6570\u636e\u7684\u5b57\u7b26\u4e32\u53cd\u5e8f\u5217\u5316\u6210Python\u5bf9\u8c61<\/li>\n<\/ul>\n<blockquote><p>\n  &nbsp;\u4e24\u4e2adump\u51fd\u6570\u662f\u5c06python\u5bf9\u8c61\u8f6c\u6362\u4e3ajson\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u7f16\u7801\uff08\u7c7b\u4f3cdemjson\u7684encode\u51fd\u6570\uff09\uff0c\u4e24\u4e2aload\u51fd\u6570\u662f\u5c06json\u8f6c\u6362\u4e3apython\u5bf9\u8c61\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3aJSON\u89e3\u6790\uff08\u7c7b\u4f3cdemjson\u7684code\u51fd\u6570\uff09\u3002\u56e0\u4e3a\u4e24\u4e2adump\u548c\u4e24\u4e2aload\u7684\u529f\u80fd\u76f8\u4f3c\uff0c\u6240\u4ee5\u5c0f\u7f16\u53ea\u4ecb\u7ecd\u5176\u4e2d\u4e00\u4e2a\uff08\u4ecb\u7ecdJSON\u683c\u5f0f\u6570\u636e\u7684\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u4e0e\u89e3\u6790\uff0c\u4e5f\u5c31\u662fdumps\u548cloads\u51fd\u6570\uff09\u3002\n<\/p><\/blockquote>\n<h3>json.dumps()<\/h3>\n<p>dumps\u53ef\u4ee5\u4f20\u9012\u7684\u53c2\u6570\u5982\u4e0b\uff1a<\/p>\n<pre><i><\/i><code>json.dumps(obj,  skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False)<\/code><\/pre>\n<p>\u5728\u65e5\u5e38\u4f7f\u7528\u4e2d\uff0c\u66f4\u591a\u7684\u60c5\u51b5\u6211\u4eec\u53ea\u4f20\u9012\u5fc5\u987b\u7684obj\u53c2\u6570\uff08\u8fd9\u662f\u4e00\u4e2a\u5bf9\u8c61\uff09\uff0c\u5176\u4ed6\u53c2\u6570\u4e3a\u53ef\u9009\u53c2\u6570\uff0c\u4e0b\u8868\u662f\u8be5\u51fd\u6570\u7684\u6240\u6709\u53c2\u6570\u7684\u4f5c\u7528\uff1a<\/p>\n<table>\n<tbody>\n<tr>\n<td>&nbsp;\u53c2\u6570<\/td>\n<td colspan=\"4\">&nbsp;\u4f5c\u7528<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;\u200b<code>obj<\/code>\u200b\uff08\u5fc5\u9009\u9879\uff09<\/td>\n<td colspan=\"4\">&nbsp;\u8981\u5e8f\u5217\u5316\u7684python\u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td>\u200b<code>&nbsp;skipkeys=False<\/code>\u200b<\/td>\n<td colspan=\"4\">&nbsp;\u662f\u5426\u8df3\u8fc7\u8981\u5e8f\u5217\u5316\u7684\u5bf9\u8c61\u4e2d\u5b57\u5178\u5143\u7d20\u7684key\u4e0d\u662f\u57fa\u672c\u7c7b\u578b\u7684\u6570\u636e\uff1b <br \/>\u5982\u679c\u4e3a&nbsp;<code>True<\/code>\uff0c\u5219\u8df3\u8fc7\uff0c\u5982\u679c\u4e3a&nbsp;<code>False<\/code>\uff0c\u5c06\u629b\u51fa&nbsp;<code>TypeError<\/code>\u5f02\u5e38\u3002<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;\u200b<code>ensure_ascii=True<\/code>\u200b<\/td>\n<td colspan=\"4\">&nbsp;\u662f\u5426\u5c06\u8981\u5e8f\u5217\u5316\u7684\u5bf9\u8c61\u4e2d\u7684\u5b57\u7b26\u4e32\u4e2d\u7684\u975eascii\u5b57\u7b26\u8fdb\u884c\u8f6c\u4e49\u3002 <\/p>\n<p>\u5982\u679c\u8be5\u53c2\u6570\u4e3a<code>True<\/code>\uff0c\u5219\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u975eascii\u5b57\u7b26\u8f6c\u4e49\u6210unicode\u5b57\u7b26\u4e32\uff0c\u5426\u5219\uff0c\u5c06\u4e0d\u4f1a\u8fdb\u884c\u8f6c\u4e49\u3002<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;\u200b<code>check_circular=True<\/code>\u200b<\/td>\n<td colspan=\"4\">&nbsp;\u662f\u5426\u8fdb\u884c\u5bb9\u5668\u7c7b\u578b\u7684\u5faa\u73af\u5f15\u7528\u68c0\u67e5\u3002 <\/p>\n<p>\u5982\u679c\u8be5\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>False<\/code>\uff0c\u5219\u4e0d\u8fdb\u884c\u68c0\u67e5\uff0c\u4f46\u662f\u53ef\u80fd\u4f1a\u5f15\u53d1&nbsp;<code>OverflowError<\/code>\u6216\u66f4\u4e25\u91cd\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u5982\u679c\u8be5\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>True<\/code>\uff0c\u5219\u5c06\u8fdb\u884c\u5bb9\u5668\u7c7b\u578b\u7684\u5faa\u73af\u5f15\u7528\u68c0\u67e5\uff0c\u5e76\u5728\u53d1\u73b0\u5faa\u73af\u5f15\u7528\u65f6\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;\u200b<code>allow_nan=True<\/code>\u200b<\/td>\n<td colspan=\"4\">&nbsp;\u662f\u5426\u5141\u8bb8\u5e8f\u5217\u5316\u8d85\u51fa\u8303\u56f4\u7684<code>float<\/code>&nbsp;\u7c7b\u578b\u7684\u503c\uff08\u5982<code>float('inf')<\/code>&nbsp;\u3001<code>float('-inf')<\/code>&nbsp;\u3001<code>float('nan')<\/code>&nbsp;\uff09\u3002 <\/p>\n<p>\u5982\u679c\u8be5\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>True<\/code>\uff0c\u5219\u4e0a\u9762\u5217\u51fa\u7684\u90a3\u4e9b\u503c\u5c06\u4f9d\u6b21\u4f7f\u7528JavaScript\u4e2d\u7b49\u4ef7\u7684\u503c\uff08&nbsp;<code>Infinity<\/code>\u3001<code>-Infinity<\/code>\u3001&nbsp;<code>NaN<\/code>\uff09\u6765\u8fdb \u884c\u66ff\u4ee3\uff1b<\/p>\n<p>\u5982\u679c\u8be5\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>False<\/code>\uff0c\u5e76\u4e14\u8981\u5e8f\u5217\u5316\u7684\u5bf9\u8c61\u4e2d\u51fa\u73b0\u4e86\u90a3\u4e9b\u8d85\u51fa\u8303\u56f4\u7684\u503c\uff0c\u5219\u5c06\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002  <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;\u200b<code>indent=None<\/code>\u200b<\/td>\n<td colspan=\"4\">&nbsp;\u662f\u5426\u5728\u6570\u7ec4\u5143\u7d20\u548c\u5bf9\u8c61\u6210\u5458\u524d\u589e\u52a0\u7f29\u8fdb\u4ee5\u4fbf\u4f7f\u683c\u5f0f\u66f4\u52a0\u7f8e\u89c2\u3002 <\/p>\n<p>\u5982\u679c\u8be5\u53c2\u6570\u8bbe\u7f6e\u4e3a\u5927\u4e8e\u7b49\u4e8e1\u7684\u6574\u6570\uff0c\u5219\u6dfb\u52a0\u6362\u884c\u7b26\u548c\u5bf9\u5e94\u6570\u91cf\u7684\u7a7a\u683c\u8868\u793a\u7f29\u8fdb\uff0c\u5982\u679c\u8bbe\u7f6e\u4e3a<code>0<\/code>\uff0c\u5219\u8868\u793a\u53ea\u6dfb\u52a0\u6362\u884c\u7b26\uff0c\u5982\u679c\u8bbe\u7f6e\u4e3a&nbsp;<code>None<\/code>\uff0c\u5219\u8868\u793a\u65e0\u7f29\u8fdb\u3002<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;\u200b<code>separators=None<\/code>\u200b<\/td>\n<td colspan=\"4\">&nbsp;\u8bbe\u7f6eJson\u4e2d\u5404\u9879\u4e4b\u95f4\u3001\u5bf9\u8c61\u7684\u952e\u548c\u503c\u4e4b\u95f4\u7684\u5206\u9694\u7b26\uff1b <\/p>\n<p>\u8be5\u53c2\u6570\u5fc5\u987b\u662f\u4e00\u4e2a2\u5143\u7ec4\uff0c\u5143\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u8868\u793aJson\u6570\u636e\u4e2d\u5404\u9879\u4e4b\u95f4\u7684\u5206\u9694\u7b26\uff0c\u5143\u7ec4\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u8868\u793aJson\u5bf9\u8c61\u7684\u952e\u548c\u503c\u4e4b\u95f4\u7684\u5206\u9694\u7b26\u3002\u9ed8\u8ba4\u7684\u5206\u9694\u7b26\u4e3a\uff08\u2019,\u2019, \u2018:\u2019\uff09<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;\u200b<code>default=None<\/code>\u200b<\/td>\n<td colspan=\"4\">&nbsp;\u6307\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u6765\u5c06\u4e0d\u53ef\u8fdb\u884c\u5e8f\u5217\u5316\u7684Python\u5bf9\u8c61\u8f6c\u5316\u4e3a\u53ef\u5e8f\u5217\u5316\u7684Python\u5bf9\u8c61\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u200b<code>&nbsp;cls=None<\/code>\u200b<\/td>\n<td colspan=\"4\">&nbsp;\u6307\u5b9a\u4e00\u4e2a\u5b9a\u5236\u7684<code>JSONEncoder<\/code>&nbsp;\u7684\u5b50\u7c7b\uff08\u4f8b\u5982\uff0c\u91cd\u5199\u4e86<code>.default()<\/code>&nbsp;\u65b9\u6cd5\u7528\u6765\u5e8f\u5217\u5316\u9644\u52a0\u7684\u7c7b\u578b\uff09\uff0c\u6307\u5b9a\u8be5\u53c2\u6570\u65f6\u8bf7\u4f7f\u7528<code>cls<\/code>&nbsp;\u5173\u952e\u5b57\u53c2\u6570\u3002\u5982\u679c\u672a\u6307\u5b9a\u8be5\u53c2\u6570\uff0c\u5219\u5c06\u4f7f\u7528\u9ed8\u8ba4\u7684<code>JSONEncoder<\/code>&nbsp;\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u200b<code>&nbsp;sort_keys=False<\/code>\u200b<\/td>\n<td colspan=\"4\">&nbsp;\u662f\u5426\u8981\u5c06\u5bf9\u8c61\u4e2d\u5b57\u5178\u5143\u7d20\u6309\u7167key\u8fdb\u884c\u6392\u5e8f\u3002 <\/p>\n<p>\u9ed8\u8ba4\u4e3a<code>False<\/code>\uff0c\u5373\u4e0d\u8fdb\u884c\u6392\u5e8f\uff0c\u82e5\u6307\u5b9a\u4e3a&nbsp;<code>True<\/code>\uff0c\u5219\u4f1a\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code>#!\/usr\/bin\/python3\nimport json\n# Python \u5b57\u5178\u7c7b\u578b\u8f6c\u6362\u4e3a JSON \u5bf9\u8c61\ndata = {\n    'no' : 1,\n    'name' : 'W3CSchool',\n    'url' : 'http:\/\/www.IDC.NET'\n}\njson_str = json.dumps(data)\nprint (\"Python \u539f\u59cb\u6570\u636e\uff1a\", repr(data))\nprint (\"JSON \u5bf9\u8c61\uff1a\", json_str)<\/code><\/pre>\n<p>python\u5bf9\u8c61\u8f6c\u5316\u4e3aJSON\u5b57\u7b26\u4e32\u7684\u65f6\u5019\u9075\u5faa\u4ee5\u4e0b\u8f6c\u6362\u89c4\u5219\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>Python<\/th>\n<th>Json<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>dict<\/td>\n<td>object<\/td>\n<\/tr>\n<tr>\n<td>list, tuple<\/td>\n<td>array<\/td>\n<\/tr>\n<tr>\n<td>str<\/td>\n<td>string<\/td>\n<\/tr>\n<tr>\n<td>int, float<\/td>\n<td>number<\/td>\n<\/tr>\n<tr>\n<td>True<\/td>\n<td>true<\/td>\n<\/tr>\n<tr>\n<td>False<\/td>\n<td>false<\/td>\n<\/tr>\n<tr>\n<td>None<\/td>\n<td>null<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>json.loads()<\/h3>\n<p>loads\u53ef\u4ee5\u4f20\u9012\u7684\u53c2\u6570\u5982\u4e0b\uff1a<\/p>\n<pre><i><\/i><code>json.loads(s,encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None)<\/code><\/pre>\n<p>\u5728\u65e5\u5e38\u4f7f\u7528\u4e2d\uff0c\u66f4\u591a\u7684\u60c5\u51b5\u6211\u4eec\u53ea\u4f20\u9012\u5fc5\u987b\u7684s\u53c2\u6570\uff08\u8fd9\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff09\uff0c\u5176\u4ed6\u53c2\u6570\u4e3a\u53ef\u9009\u53c2\u6570\uff0c\u4e0b\u8868\u662f\u8be5\u51fd\u6570\u7684\u6240\u6709\u53c2\u6570\u7684\u4f5c\u7528\uff1a<\/p>\n<table>\n<tbody>\n<tr>\n<td>&nbsp;\u53c2\u6570<\/td>\n<td colspan=\"4\">&nbsp;\u4f5c\u7528<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;\u200b<code>s<\/code>\u200b\uff08\u5fc5\u9009\u9879\uff09<\/td>\n<td colspan=\"4\">&nbsp;\u8981\u53cd\u5e8f\u5217\u5316\u7684JSON\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>encoding=None  <\/td>\n<td colspan=\"4\">&nbsp;\u8be5\u53c2\u6570\u5df2\u5f03\u7528\uff0c\u5c06\u4f1a\u88ab\u5ffd\u7565<\/td>\n<\/tr>\n<tr>\n<td>cls=None  <\/td>\n<td colspan=\"4\">&nbsp;\u6307\u5b9a\u4e00\u4e2a\u5b9a\u5236\u7684<code>JsonDecoder<\/code>&nbsp;\u5b50\u7c7b\uff0c\u4ee5\u4fbf\u5b9e\u73b0\u7279\u5b9a\u7684\u53cd\u5e8f\u5217\u5316\u9700\u6c42\uff1b<\/td>\n<\/tr>\n<tr>\n<td>object_hook=None  <\/td>\n<td colspan=\"4\">\n<p>\u63a5\u53d7\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u7528\u4e8e\u5904\u7406\u89e3\u7801\u540e\u751f\u6210\u7684Python\u5bf9\u8c61\u4e2d\u200b<code>dict<\/code>\u200b\u7c7b\u578b\u7684\u503c\u3002<\/p>\n<p>\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5904\u7406\u8fc7\u7a0b\u662f\u9012\u5f52\u8fdb\u884c\u7684\uff0c\u5373\u8fd4\u56de\u7684Python\u5bf9\u8c61\u5185\u90e8\u6240\u6709\u7684\u5b57\u5178\u7ed3\u6784\u90fd\u5c06\u88ab\u8fd9\u4e2a\u65b9\u6cd5\u5904\u7406<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\u200b<code>&nbsp;parse_float=None<\/code>\u200b<\/td>\n<td colspan=\"4\">\n<p>\u7528\u4e8e\u5904\u7406\u89e3\u7801\u540e\u7684Python\u5bf9\u8c61\u4e2d\u7684<code>float<\/code>\u7c7b\u578b\u7684\u503c\u3002  <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;\u200b<code>parse_int=None<\/code>\u200b<\/td>\n<td colspan=\"4\">&nbsp;\u63a5\u53d7\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u7528\u4e8e\u5904\u7406\u89e3\u7801\u540e\u7684Python\u5bf9\u8c61\u4e2d\u7684<code>int<\/code>&nbsp;\u7c7b\u578b\u7684\u503c\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u200b<code>&nbsp;parse_constant=None<\/code>\u200b<\/td>\n<td colspan=\"4\">\u63a5\u53d7\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u7528\u4e8e\u89e3\u7801\u65f6\u5bf9&nbsp;<code>Infinity<\/code>\u3001&nbsp;<code>-Infinity<\/code>\u3001&nbsp;<code>NaN<\/code>\u6216\u5176\u4ed6\u975e\u6cd5\u7684Json\u6570\u503c\u7684\u5904\u7406\u3002  <\/td>\n<\/tr>\n<tr>\n<td>&nbsp;\u200b<code>object_parse_hook=None<\/code>\u200b<\/td>\n<td colspan=\"4\">&nbsp;\u5982\u679c\u6307\u5b9a\u4e86\u8be5\u53c2\u6570\u5e76\u4e14\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u90a3\u4e48Json\u5bf9\u8c61\u5c06\u88ab\u89e3\u7801\u6210\u4e00\u4e2a\u5143\u7d20\u4e3a\u4e8c\u5143\u7ec4\u7684\u5217\u8868\uff0c\u4e8c\u5143\u7ec4\u7684\u4e24\u4e2a\u5143\u7d20\u5206\u522b\u4e3aJson\u5bf9\u8c61\u4e2d\u7684\u952e\u503c\u5bf9\u7684\u952e\u548c\u503c\uff0c\u5e76\u4e14\u5217\u8868\u4e2d\u5143\u7d20\u7684\u987a\u5e8f\u4e0eJson\u5bf9\u8c61\u4e2d\u952e\u503c\u5bf9\u7684\u987a\u5e8f\u4e00\u81f4\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code>#!\/usr\/bin\/python3\nimport json\n# Python \u5b57\u5178\u7c7b\u578b\u8f6c\u6362\u4e3a JSON \u5bf9\u8c61\ndata1 = {\n    'no' : 1,\n    'name' : 'W3CSchool',\n    'url' : 'http:\/\/www.IDC.NET'\n}\n\njson_str = json.dumps(data1)\nprint (\"Python \u539f\u59cb\u6570\u636e\uff1a\", repr(data1))\nprint (\"JSON \u5bf9\u8c61\uff1a\", json_str)\n\n# \u5c06 JSON \u5bf9\u8c61\u8f6c\u6362\u4e3a Python \u5b57\u5178\ndata2 = json.loads(json_str)\nprint (\"data2['name']: \", data2['name'])\nprint (\"data2['url']: \", data2['url'])<\/code><\/pre>\n<p>\u4e0e\u5e8f\u5217\u5316\u7684\u8fc7\u7a0b\u7c7b\u4f3c\uff0cJSON\u8f6c\u5316\u4e3apython\u5bf9\u8c61\u4e5f\u9075\u5faa\u4e00\u5b9a\u7684\u89c4\u5219\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>Json<\/th>\n<th>Python<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>object<\/td>\n<td>dict<\/td>\n<\/tr>\n<tr>\n<td>array<\/td>\n<td>list<\/td>\n<\/tr>\n<tr>\n<td>string<\/td>\n<td>str<\/td>\n<\/tr>\n<tr>\n<td>number\uff08int\uff09<\/td>\n<td>int<\/td>\n<\/tr>\n<tr>\n<td>number\uff08real\uff09<\/td>\n<td>float<\/td>\n<\/tr>\n<tr>\n<td>true<\/td>\n<td>True<\/td>\n<\/tr>\n<tr>\n<td>false<\/td>\n<td>False<\/td>\n<\/tr>\n<tr>\n<td>null<\/td>\n<td>None<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6559\u7a0b\u5c06\u4f1a\u6559\u6211\u4eec\u5982\u4f55\u4f7f\u7528&nbsp;Python&nbsp;\u7f16\u7a0b\u8bed\u8a00\u7f16\u7801\u548c\u89e3\u7801 JSON\u3002 \u73af\u5883 \u5728pytho [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-188603","post","type-post","status-publish","format-standard","hentry","category-website"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/188603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=188603"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/188603\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=188603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=188603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=188603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}