{"id":205424,"date":"2025-05-29T09:48:32","date_gmt":"2025-05-29T01:48:32","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205424\/"},"modified":"2025-05-29T09:48:32","modified_gmt":"2025-05-29T01:48:32","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e9%81%8d%e5%8e%86n%e7%ba%a7json%e5%b9%b6%e7%94%9f%e6%88%90%e6%a0%91%e7%8a%b6%e7%bb%93%e6%9e%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205424\/","title":{"rendered":"\u5982\u4f55\u7528Python\u904d\u5386N\u7ea7JSON\u5e76\u751f\u6210\u6811\u72b6\u7ed3\u6784\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u7528Python\u904d\u5386N\u7ea7JSON\u5e76\u751f\u6210\u6811\u72b6\u7ed3\u6784\uff1f<\/h1>\n<p>\u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u7528Python\u904d\u5386N\u7ea7JSON\u5e76\u751f\u6210\u6811\u72b6\u7ed3\u6784\uff1f\u300b<\/span>\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241201\/1733045092674c2b64dd8f2.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u904d\u5386 n \u7ea7 json\uff0c\u751f\u6210\u6811\u7ed3\u6784<\/strong><\/p>\n<p>\u672c\u6587\u6863\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 python \u904d\u5386\u5d4c\u5957 json \u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6811\u72b6\u7ed3\u6784\u3002<\/p>\n<p><strong>python \u65b9\u6848<\/strong><\/p>\n<p>python \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u904d\u5386\u590d\u6742 json \u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 json.loads() \u5c06 json \u5b57\u7b26\u4e32\u52a0\u8f7d\u4e3a python \u6570\u636e\u7ed3\u6784\uff1a<\/p>\n<pre>import json\n\njson_str = '''\n{\n  \"id\": \"series\",\n  \"css\": \"wrapper\",\n  \"html\": [\n    { \"id\": \"series\", \"css\": \"header\", \"html\": \"...\" }\n    # ...\n  ]\n}\n'''\n\njson_obj = json.loads(json_str)<\/pre>\n<p>\u8981\u904d\u5386\u5d4c\u5957\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\uff0c\u6839\u636e\u6570\u636e\u7c7b\u578b\uff08\u5b57\u5178\u6216\u5217\u8868\uff09\u8fdb\u884c\u4e0d\u540c\u7684\u9012\u5f52\u8c03\u7528\uff1a<\/p>\n<pre>def print_json_tree(json_obj, indent=0):\n    if isinstance(json_obj, dict):\n        for key, value in json_obj.items():\n            print('-' * indent + str(key))\n            print_json_tree(value, indent+2)\n    elif isinstance(json_obj, list):\n        for item in json_obj:\n            print_json_tree(item, indent+2)\n    else:\n        print('-' * indent + str(json_obj))<\/pre>\n<p>\u6b64\u51fd\u6570\u5c06 json \u5bf9\u8c61\u6253\u5370\u4e3a\u7f29\u8fdb\u7684\u6811\u72b6\u7ed3\u6784\u3002<\/p>\n<p><strong>\u6811\u72b6\u7ed3\u6784\u793a\u4f8b<\/strong><\/p>\n<p>\u4e0b\u9762\u662f\u539f\u59cb json \u5bf9\u8c61\u7684\u6811\u72b6\u7ed3\u6784\u793a\u4f8b\uff1a<\/p>\n<pre>- id\n- css\n- html\n  - id\n  - css\n    - topbar\n    - mask\n    - layer\n  - id\n  - css\n  - html\n    - id\n    - css\n    - html\n      - pic\n      - total\n      - news1\n      - new2\n      - ad1\n      - list\n      - pic\n      - video\n      - ad2\n  - id\n  - css\n    - ad3\n    - love\n    - brand\n    - type\n- position\n  - return_top\n  - side_nav\n- footer\n  - nav<\/pre>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u7528Python\u904d\u5386N\u7ea7JSON\u5e76\u751f\u6210\u6811\u72b6\u7ed3\u6784\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u7528Python\u904d\u5386N\u7ea7JSO&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205424","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205424"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205424\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}