{"id":195,"date":"2023-02-22T22:15:07","date_gmt":"2023-02-22T14:15:07","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=195"},"modified":"2023-04-30T15:31:48","modified_gmt":"2023-04-30T07:31:48","slug":"python-encode-and-decode-json-object","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/22\/python-encode-and-decode-json-object\/","title":{"rendered":"Python\u7f16\u7801\u548c\u89e3\u7801JSON\u5bf9\u8c61"},"content":{"rendered":"<h2>JSON \u51fd\u6570<\/h2>\n<p>\u4f7f\u7528 JSON \u51fd\u6570\u9700\u8981\u5bfc\u5165 json \u5e93\uff1aimport json<\/p>\n<p><!-- more --><\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: right;\">\u51fd\u6570<\/th>\n<th style=\"text-align: center;\">\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: right;\">json.dumps<\/td>\n<td style=\"text-align: center;\">\u5c06 Python \u5bf9\u8c61\u7f16\u7801\u6210 JSON \u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: right;\">json.loads<\/td>\n<td style=\"text-align: center;\">\u5c06\u5df2\u7f16\u7801\u7684 JSON \u5b57\u7b26\u4e32\u89e3\u7801\u4e3a Python \u5bf9\u8c61<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>json.dumps<\/h2>\n<p>json.dumps \u7528\u4e8e\u5c06 Python \u5bf9\u8c61\u7f16\u7801\u6210 JSON \u5b57\u7b26\u4e32\u3002<\/p>\n<ul>\n<li><strong>\u8bed\u6cd5<\/strong><\/li>\n<\/ul>\n<pre><code>json.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding=&quot;utf-8&quot;, default=None, sort_keys=False, **kw)<\/code><\/pre>\n<ul>\n<li><strong>\u5b9e\u4f8b<\/strong><\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u5c06\u6570\u7ec4\u7f16\u7801\u4e3a JSON \u683c\u5f0f\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-python\">import json\n\ndata = [ { &#039;a&#039; : 1, &#039;b&#039; : 2, &#039;c&#039; : 3, &#039;d&#039; : 4, &#039;e&#039; : 5 } ]\n\njson = json.dumps(data)\nprint json<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u6267\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre><code>[{&quot;a&quot;: 1, &quot;c&quot;: 3, &quot;b&quot;: 2, &quot;e&quot;: 5, &quot;d&quot;: 4}]<\/code><\/pre>\n<p>\u4f7f\u7528\u53c2\u6570\u8ba9 JSON \u6570\u636e\u683c\u5f0f\u5316\u8f93\u51fa\uff1a<\/p>\n<pre><code>>&gt;&gt; import json\n>&gt;&gt; print json.dumps({&#039;a&#039;: &#039;AppBlog.CN&#039;, &#039;b&#039;: 1}, sort_keys=True, indent=4, separators=(&#039;,&#039;, &#039;: &#039;))\n{\n    &quot;a&quot;: &quot;AppBlog.CN&quot;,\n    &quot;b&quot;: 1\n}<\/code><\/pre>\n<h2>json.loads<\/h2>\n<p>json.loads \u7528\u4e8e\u89e3\u7801 JSON \u6570\u636e\u3002\u8be5\u51fd\u6570\u8fd4\u56de Python \u5b57\u6bb5\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<ul>\n<li><strong>\u8bed\u6cd5<\/strong><\/li>\n<\/ul>\n<pre><code>json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])<\/code><\/pre>\n<ul>\n<li><strong>\u5b9e\u4f8b<\/strong><\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u5c55\u793a\u4e86Python \u5982\u4f55\u89e3\u7801 JSON \u5bf9\u8c61\uff1a<\/p>\n<pre><code class=\"language-python\">import json\n\njsonData = &#039;{&quot;a&quot;:1, &quot;b&quot;:2, &quot;c&quot;:3, &quot;d&quot;:4, &quot;e&quot;:5}&#039;;\n\ntext = json.loads(jsonData)\nprint text<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u6267\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre><code>{u&#039;a&#039;: 1, u&#039;c&#039;: 3, u&#039;b&#039;: 2, u&#039;e&#039;: 5, u&#039;d&#039;: 4}<\/code><\/pre>\n<p>\u66f4\u591a\u5185\u5bb9\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/docs.python.org\/2\/library\/json.html\">https:\/\/docs.python.org\/2\/library\/json.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON \u51fd\u6570 \u4f7f\u7528 JSON \u51fd\u6570\u9700\u8981\u5bfc\u5165 json \u5e93\uff1aimport json \u51fd\u6570 \u63cf\u8ff0 json.du [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/195\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}