{"id":6523,"date":"2014-07-30T00:17:26","date_gmt":"2014-07-29T16:17:26","guid":{"rendered":"http:\/\/jpuyy.com\/?p=6523"},"modified":"2014-10-09T21:58:19","modified_gmt":"2014-10-09T13:58:19","slug":"python-json","status":"publish","type":"post","link":"https:\/\/jpuyy.com\/?p=6523","title":{"rendered":"python\u64cd\u4f5cjson"},"content":{"rendered":"<p>\u9996\u5148\u64cd\u4f5c\u4e00\u4e0bpython\u4e2d\u7684\u5b57\u5178\uff0c\u9996\u5148\u662f\u7a7a\u5b57\u5178eth,\u5728\u5176\u4e2d\u6dfb\u52a0\u6570\u636eeth0,eth1\u5e76\u5bf9\u5e94\u4e24\u4e2aip<\/p>\n<pre>&gt;&gt;&gt; eth = {}\r\n&gt;&gt;&gt; eth['eth0'] = '192.168.2.12'\r\n&gt;&gt;&gt; print eth\r\n{'eth0': '192.168.2.12'}\r\n&gt;&gt;&gt; eth['eth1'] = '223.5.5.5'\r\n&gt;&gt;&gt; print eth\r\n{'eth1': '223.5.5.5', 'eth0': '192.168.2.12'}<\/pre>\n<p>json\u4e0epython\u4e2ddict\u4e92\u76f8\u8f6c\u6362\uff0c\u628adict\u8f6c\u6362\u6210json-\u4f7f\u7528json.dumps()\uff0c\u5c06json\u8f6c\u6362\u4e3adict-\u4f7f\u7528json.loads()\uff0cjson.loads()\u8fd4\u56de\u7684\u662f\u4e00\u4e2adictionary.<\/p>\n<pre>&gt;&gt;&gt; import json\r\n&gt;&gt;&gt; ethjson = json.dumps(eth)\r\n&gt;&gt;&gt; type(ethjson)\r\n&lt;type 'str'&gt;\r\n&gt;&gt;&gt; print ethjson\r\n{\"eth1\": \"223.5.5.5\", \"eth0\": \"192.168.2.12\"}\r\n&gt;&gt;&gt; ethdict = json.loads(ethjson)\r\n&gt;&gt;&gt; type(ethdict)\r\n&lt;type 'dict'&gt;\r\n&gt;&gt;&gt; print ethdict\r\n{u'eth1': u'223.5.5.5', u'eth0': u'192.168.2.12'}\r\n&gt;&gt;&gt; print ethdict['eth0'], ethdict['eth1']\r\n192.168.2.12 223.5.5.5\r\n<\/pre>\n<p>\u5224\u65adjson\u91cc\u662f\u5426\u6709\u67d0\u4e2akey<\/p>\n<pre>if 'text' in post['caption'].keys():      #\u5728keys\u4e2d\u662f\u5426\u6709text\r\nif 'text' in post['caption']:    # \u5728caption\u4e0b\u662f\u5426\u6709text\r\nif 'ipv4' in output['ansible_facts']['ansible_int']:  # \u5728ansible_facts\u4e0b\u7684ansible_int\u4e0b\u662f\u5426\u6709ipv4<\/pre>\n<p>\u6709\u65f6\u5019\u9700\u8981\u4e34\u65f6\u751f\u6210\u4e00\u4e2a\u683c\u5f0f\u5316\u8fc7\u7684json\uff0c\u53ef\u4ee5\u4f7f\u7528json.tool\u6a21\u5757\u6765\u683c\u5f0f\u5316<\/p>\n<pre>echo '{\"json\":\"obj\"}' | python -m json.tool\r\n\r\n{\r\n\r\n    \"json\": \"obj\"\r\n\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u64cd\u4f5c\u4e00\u4e0bpython\u4e2d\u7684\u5b57\u5178\uff0c\u9996\u5148\u662f\u7a7a\u5b57\u5178eth,\u5728\u5176\u4e2d\u6dfb\u52a0\u6570\u636eeth0,eth1\u5e76\u5bf9\u5e94\u4e24\u4e2aip &gt;&gt;&gt; eth = {} &gt;&gt;&gt; eth[&#8216;eth0&#8217;] = &#8216;192.168.2.12&#8217; &gt;&gt;&gt; print eth {&#8216;eth0&#8217;: &#8216;192.168.2.12&#8217;} &gt;&gt;&gt; eth[&#8216;eth1&#8217;] = &#8216;223.5.5.5&#8217; &gt;&gt;&gt; print eth {&#8216;eth1&#8217;: &#8216;223.5.5.5&#8217;, &#8216;eth0&#8217;: &#8216;192.168.2.12&#8217;} json\u4e0epython\u4e2ddict\u4e92\u76f8\u8f6c\u6362\uff0c\u628adict\u8f6c\u6362\u6210json-\u4f7f\u7528json.dumps()\uff0c\u5c06json\u8f6c\u6362\u4e3adict-\u4f7f\u7528json.loads()\uff0cjson.loads()\u8fd4\u56de\u7684\u662f\u4e00\u4e2adictionary. &gt;&gt;&gt; import json &gt;&gt;&gt; ethjson = json.dumps(eth) &gt;&gt;&gt; type(ethjson) &lt;type &#8216;str&#8217;&gt; &gt;&gt;&gt; print ethjson {&#8220;eth1&#8221;: &#8220;223.5.5.5&#8221;, &#8220;eth0&#8221;: &#8220;192.168.2.12&#8221;} &gt;&gt;&gt; ethdict = json.loads(ethjson) &gt;&gt;&gt; type(ethdict) &lt;type &#8216;dict&#8217;&gt; &gt;&gt;&gt; print ethdict [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[],"class_list":["post-6523","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6523"}],"version-history":[{"count":8,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6523\/revisions"}],"predecessor-version":[{"id":6722,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6523\/revisions\/6722"}],"wp:attachment":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}