{"id":50505,"date":"2024-12-03T13:49:10","date_gmt":"2024-12-03T05:49:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50505\/"},"modified":"2024-12-03T13:49:10","modified_gmt":"2024-12-03T05:49:10","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%8a%a8%e6%80%81%e4%bf%ae%e6%94%b9%e5%b5%8c%e5%a5%97json%e8%af%b7%e6%b1%82%e8%b4%9f%e8%bd%bd%e7%9a%84%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50505\/","title":{"rendered":"Python\u4e2d\u5982\u4f55\u52a8\u6001\u4fee\u6539\u5d4c\u5957JSON\u8bf7\u6c42\u8d1f\u8f7d\u7684\u503c\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python\u4e2d\u5982\u4f55\u52a8\u6001\u4fee\u6539\u5d4c\u5957JSON\u8bf7\u6c42\u8d1f\u8f7d\u7684\u503c\uff1f<\/h1>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aPython\u4e2d\u5982\u4f55\u52a8\u6001\u4fee\u6539\u5d4c\u5957JSON\u8bf7\u6c42\u8d1f\u8f7d\u7684\u503c\uff1f\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241121\/1732170116673ed1841b989.jpg\" class=\"aligncenter\" title=\"Python\u4e2d\u5982\u4f55\u52a8\u6001\u4fee\u6539\u5d4c\u5957JSON\u8bf7\u6c42\u8d1f\u8f7d\u7684\u503c\uff1f\u63d2\u56fe\" alt=\"Python\u4e2d\u5982\u4f55\u52a8\u6001\u4fee\u6539\u5d4c\u5957JSON\u8bf7\u6c42\u8d1f\u8f7d\u7684\u503c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 python \u4e2d\u52a8\u6001\u4fee\u6539 json \u8bf7\u6c42\u8d1f\u8f7d\u4e2d\u7684\u503c<\/strong><\/p>\n<p>\u5728\u6784\u5efa json \u8bf7\u6c42\u65f6\uff0c\u4fee\u6539\u8bf7\u6c42\u8d1f\u8f7d\u4e2d\u7684\u7279\u5b9a\u503c\u53ef\u80fd\u9700\u8981\u52a8\u6001\u4fee\u6539\u3002\u672c\u6587\u5c06\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u6848\u4f8b\u6765\u8bf4\u660e\u5982\u4f55\u5728 python \u4e2d\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\u3002<\/p>\n<p>\u5047\u8bbe\u8bf7\u6c42\u4f53\u7684\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre>{\n    \"shopattrresplist\": [\n        {\n            \"child\": [\n                {\n                    \"child\": [\n                        {\n                            \"value\": \"{{\\\"address\\\":\\\"{{address}}\\\",\\\"longitude\\\":{longitude},\\\"latitude\\\":{latitude},\\\"province\\\":{province},\\\"city\\\":{city},\\\"region\\\":{region}}}\",\n                            \"xxxx\": \"yyy\"\n                        }\n                    ]\n                }\n            ]\n        }\n    ]\n}<\/pre>\n<p>\u9700\u8981\u52a8\u6001\u4fee\u6539 &#8220;value&#8221; \u952e\u7684\u503c\uff0c\u4f7f\u5176\u5305\u542b\u7279\u5b9a\u53d8\u91cf\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u6784\u5efa\u66ff\u6362\u53d8\u91cf\u7684\u6620\u5c04\uff1a<\/p>\n<pre>replacement_map = {\n    \"\u91d1\u82b1\u8857\u9053\u9ad8\u7b2c\u574a39\u53f7\u8354\u6e7e\u533a\u91d1\u82b1\u8857\u9526\u7ee3\u793e\u533a\": address,  # \u66ff\u6362 address \u503c\n    \"113.252272\": longitude,  # \u66ff\u6362 longitude \u503c\n    # ... \u5176\u4ed6\u53d8\u91cf\u7684\u66ff\u6362\n}<\/pre>\n<p>\u7136\u540e\uff0c\u904d\u5386\u6620\u5c04\u5e76\u66ff\u6362 &#8220;value&#8221; \u952e\u7684\u503c\uff1a<\/p>\n<pre>for key, value in replacement_map.items():\n    data[\"shopattrresplist\"][0][\"child\"][0][\"child\"][0][\"value\"] = data[\"shopattrresplist\"][0][\"child\"][0][\"child\"][0][\"value\"].replace(key, value)<\/pre>\n<p>\u4f46\u662f\uff0cpython \u4e2d\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u4e0d\u80fd\u6b63\u786e\u5904\u7406 json \u683c\u5f0f\u3002\u56e0\u6b64\uff0c\u9700\u8981\u5c06\u66ff\u6362\u540e\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a json \u683c\u5f0f\uff1a<\/p>\n<pre>data[\"shopAttrRespList\"][0][\"child\"][0][\"child\"][0][\"value\"] = json.dumps(data[\"shopAttrRespList\"][0][\"child\"][0][\"child\"][0][\"value\"])<\/pre>\n<p>\u6700\u540e\uff0c\u4f7f\u7528\u7ecf\u8fc7\u4fee\u6539\u7684 &#8220;data&#8221; \u53d1\u9001\u8bf7\u6c42\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aPython\u4e2d\u5982\u4f55\u52a8\u6001\u4fee\u6539\u5d4c\u5957JSON\u8bf7\u6c42\u8d1f\u8f7d\u7684\u503c\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u5982\u4f55\u52a8\u6001\u4fee\u6539\u5d4c\u5957JSON\u8bf7\u6c42\u8d1f\u8f7d\u7684\u503c\uff1f \u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aPython\u4e2d\u5982\u4f55\u52a8\u6001\u4fee\u6539\u5d4c\u5957JSON\u8bf7\u6c42\u8d1f\u8f7d\u7684\u503c\uff1f\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01 \u5982\u4f55\u5728 python \u4e2d\u52a8\u6001\u4fee\u6539 json \u8bf7\u6c42\u8d1f\u8f7d\u4e2d\u7684\u503c \u5728\u6784\u5efa json \u8bf7\u6c42\u65f6\uff0c\u4fee\u6539\u8bf7\u6c42\u8d1f\u8f7d\u4e2d\u7684\u7279\u5b9a\u503c\u53ef\u80fd\u9700\u8981\u52a8\u6001\u4fee\u6539\u3002\u672c\u6587\u5c06\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u6848\u4f8b\u6765\u8bf4\u660e\u5982\u4f55\u5728 python \u4e2d\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\u3002 \u5047\u8bbe\u8bf7\u6c42\u4f53\u7684\u7ed3\u6784\u5982\u4e0b\uff1a { &#8220;shopattrresplist&#8221;: [ { &#8220;child&#8221;: [ { &#8220;child&#8221;: [ { &#8220;value&#8221;: &#8220;{{\\&#8221;address\\&#8221;:\\&#8221;{{address}}\\&#8221;,\\&#8221;longitude\\&#8221;:{longitude},\\&#8221;latitude\\&#8221;:{latitude},\\&#8221;province\\&#8221;:{province},\\&#8221;city\\&#8221;:{city},\\&#8221;region\\&#8221;:{region}}}&#8221;, &#8220;xxxx&#8221;: &#8220;yyy&#8221; } ] } ] } ] } \u9700\u8981\u52a8\u6001\u4fee\u6539 &#8220;value&#8221; \u952e\u7684\u503c\uff0c\u4f7f\u5176\u5305\u542b\u7279\u5b9a\u53d8\u91cf\u3002 \u89e3\u51b3\u65b9\u6848 \u9996\u5148\uff0c\u6784\u5efa\u66ff\u6362\u53d8\u91cf\u7684\u6620\u5c04\uff1a replacement_map = { &#8220;\u91d1\u82b1\u8857\u9053\u9ad8\u7b2c\u574a39\u53f7\u8354\u6e7e\u533a\u91d1\u82b1\u8857\u9526\u7ee3\u793e\u533a&#8221;: address, # \u66ff\u6362 address \u503c &#8220;113.252272&#8221;: longitude, # \u66ff\u6362 longitude \u503c # [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-50505","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=50505"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50505\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}