{"id":368,"date":"2014-07-03T13:40:36","date_gmt":"2014-07-03T13:40:36","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=368"},"modified":"2014-07-03T13:40:36","modified_gmt":"2014-07-03T13:40:36","slug":"python%e8%a7%a3%e6%9e%90json%e5%ad%a6%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/368.html","title":{"rendered":"Python\u89e3\u6790json\u5b66\u4e60"},"content":{"rendered":"<h4>python json \u4e2d\u6587<\/h4>\n<p>python\u4e2d\u81ea\u5e26\u4e86\u5904\u7406python\u7684\u6a21\u5757\uff0c\u4f7f\u7528\u65f6\u5019\u76f4\u63a5import json\u5373\u53ef\u3002\u4f7f\u7528loads\u65b9\u6cd5\u5373\u53ef\u5c06json\u5b57\u7b26\u4e32\u8f6c\u6362\u6210python\u5bf9\u8c61\u3002<\/p>\n<p>\u4e00\u3001\u4f46\u5728\u4f7f\u7528json\u6a21\u5757\u7684\u65f6\u5019\u9700\u8981\u6ce8\u610f\u7684\u662f\u5bf9\u4e2d\u6587\u7684\u5904\u7406\uff0cloads\u65b9\u6cd5\u5982\u679c\u4f20\u5165\u7684\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u4e0d\u662fUTF-8\u7684\u8bdd\uff0c\u9700\u8981\u7528encoding\u6307\u5b9a\u5b57\u7b26\u7f16\u7801<\/p>\n<pre class=\"lang:python decode:true\">#!\/usr\/bin\/env python\n# -*- coding:utf-8 -*-\nimport json\njs = json.loads('{\"insun\": \"\u6cf0\u56e7 \/ \u4eba\u5728\u56e7\u90142 \/ Lost in Thailand \"}')\nprint json.dumps(js)\nprint json.dumps(js,ensure_ascii=False)<\/pre>\n<p>\u6253\u5370\u6548\u679c\u5982\u4e0b\uff1a<br \/>\n{&#8220;insun&#8221;: &#8220;u6cf0u56e7 \/ u4ebau5728u56e7u90142 \/ Lost in Thailand &#8220;}<br \/>\n{&#8220;insun&#8221;: &#8220;\u6cf0\u56e7 \/ \u4eba\u5728\u56e7\u90142 \/ Lost in Thailand &#8220;}<\/p>\n<p>\u4e8c\u3001\u5982\u679c\u4f20\u5165\u7684\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u4e0d\u662fUTF-8\u7684\u8bdd\uff0c\u9700\u8981\u7528encoding\u6307\u5b9a\u5b57\u7b26\u7f16\u7801\u3002\u5bf9\u4e8e\uff1a<br \/>\ndataDict = json.loads(dataJsonStr);<br \/>\n\u5176\u4e2ddataJsonStr\u662fjson\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u5176\u7f16\u7801\u672c\u8eab\u662f\u975eUTF-8\u7684\u8bdd\uff0c\u6bd4\u5982\u662fGB2312\u7684\uff0c\u90a3\u4e48\u4e0a\u8ff0\u4ee3\u7801\uff0c\u5c31\u4f1a\u5bfc\u81f4\u51fa\u9519\u3002\u6539\u4e3a\u5bf9\u5e94\u7684\uff1a<br \/>\ndataDict = json.loads(dataJsonStr, encoding=&#8221;GB2312&#8221;);<br \/>\n\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u6b64\u5904\uff0c\u5373\u5bf9\u5e94\u7740\u4e0a\u9762\u51fd\u6570\u89e3\u91ca\u4e2d\u7684\uff1a<\/p>\n<blockquote><p>If s is a str instance and is encoded with an ASCII based encoding other than UTF-8 (e.g. latin-1), then an appropriate encoding name must be specified.<\/p><\/blockquote>\n<p>\u4e09\u3001\u5982\u679c\u8981\u89e3\u6790\u7684\u5b57\u7b26\u4e32\uff0c\u672c\u8eab\u7684\u7f16\u7801\u7c7b\u578b\uff0c\u4e0d\u662f\u57fa\u4e8eASCII\u7684\uff0c\u90a3\u4e48\uff0c\u8c03\u7528json.loads\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5c06\u5bf9\u5e94\u5b57\u7b26\u4e32\uff0c\u8f6c\u6362\u4e3aUnicode\u7c7b\u578b\u3002<br \/>\n\u8fd8\u662f\u4ee5\u4e0a\u8ff0\u7684\uff1a<br \/>\ndataDict = json.loads(dataJsonStr, encoding=&#8221;GB2312&#8243;);<br \/>\n\u4e3a\u4f8b\uff0c\u5373\u4f7f\u4f60\u6b64\u5904\u7684\u5b57\u7b26\u4e32dataJsonStr\uff0c\u5df2\u7ecf\u901a\u8fc7encoding\u6307\u5b9a\u4e86\u5408\u9002\u7684\u7f16\u7801\uff0c\u4f46\u662f\u7531\u4e8e\u5176\u4e2d\uff0c\u5305\u542b\u4e86\u5176\u4ed6\u7684\u7f16\u7801\u7684\u5b57\u7b26\uff0c\u6bd4\u5982\u6211\u672c\u8eab dataJsonStr \u662fGB2312\u7684\u5b57\u7b26\uff0c\u4f46\u662f\u5176\u4e2d\u53c8\u5305\u542b\u4e86\u7684\u4e00\u4e9b\u65e5\u6587\u5b57\u7b26\uff0c\u6b64\u65f6\uff0cjson.loads\u8fd8\u662f\u4f1a\u51fa\u9519\uff0c\u56e0\u4e3a\u6b64\u5904\u7684 dataJsonStr \u4e0d\u662f\u4ee5ASCII\u4e3a\u57fa\u7840\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u6240\u4ee5\uff0c\u9700\u8981\u5148\u53bb\u5c06dataJsonStr\u8f6c\u6362\u4e3aUnicode\uff0c\u7136\u540e\u518d\u8c03\u7528 json.loads\uff0c\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:python decode:true \">dataJsonStrUni = dataJsonStr.decode(\"GB2312\");\ndataDict = json.loads(dataJsonStrUni, encoding=\"GB2312\");<\/pre>\n<p>&nbsp;<\/p>\n<p>\u5b98\u7f51\uff1a<br \/>\n18.2. json \u2014 JSON encoder and decoder<br \/>\n<a href=\"http:\/\/docs.python.org\/2\/library\/json.html\" target=\"_blank\">http:\/\/docs.python.org\/2\/library\/json.html<\/a><\/p>\n<p>json.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True,cls=None, indent=None, separators=None, encoding=&#8221;utf-8&#8243;, default=None, sort_keys=False, **kw)<br \/>\njson.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[,object_pairs_hook[, **kw]]]]]]]])<\/p>\n<p>\u53c2\u8003\uff1a<\/p>\n<ul>\n<li>Python JSON\u6a21\u5757\u89e3\u7801\u4e2d\u6587\u7684BUG <a href=\"http:\/\/www.douban.com\/note\/214822809\/\" target=\"_blank\">http:\/\/www.douban.com\/note\/214822809\/<\/a><\/li>\n<li>\u4f7f\u7528python\u5904\u7406json <a href=\"http:\/\/www.giser.net\/?p=990\" target=\"_blank\">http:\/\/www.giser.net\/?p=990<\/a><\/li>\n<li>python\u4e0b\u8c03\u7528json.dumps\u4e2d\u6587\u663e\u793a\u95ee\u9898\u89e3\u51b3\u529e\u6cd5 <a href=\"http:\/\/blog.csdn.net\/sagittar\/article\/details\/6169605\" target=\"_blank\">http:\/\/blog.csdn.net\/sagittar\/article\/details\/6169605<\/a><\/li>\n<li>Python\u89e3\u6790json\u6570\u636e\u7ed3\u6784\u8303\u4f8b <a href=\"http:\/\/hi.baidu.com\/leejun_2005\/item\/fc688affc196f8723c198b7c\" target=\"_blank\">http:\/\/hi.baidu.com\/leejun_2005\/item\/fc688affc196f8723c198b7c<\/a><\/li>\n<li>Python\u4e2djson.loads\u89e3\u6790\u5305\u542bn\u7684\u5b57\u7b26\u4e32\u4f1a\u51fa\u9519 <a href=\"http:\/\/www.crifan.com\/use_python_json_loads_parse_string_contain_newline_will_fail_error\/\" target=\"_blank\">http:\/\/www.crifan.com\/use_python_json_loads_parse_string_contain_newline_will_fail_error\/<\/a><\/li>\n<\/ul>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>Python\u89e3\u6790json\u6570\u636e\u7ed3\u6784\u8303\u4f8b<br \/>\n\u4e00\u3001JSON\u7684\u683c\u5f0f:<\/p>\n<p>1,\u5bf9\u8c61:<br \/>\n{name:&#8221;Peggy&#8221;,email:&#8221;peggy@gmail.com&#8221;,homepage:&#8221;http:\/\/www.peggy.com&#8221;}<br \/>\n{ \u5c5e\u6027 : \u503c , \u5c5e\u6027 : \u503c , \u5c5e\u6027 : \u503c }<\/p>\n<p>2,\u6570\u7ec4\u662f\u6709\u987a\u5e8f\u7684\u503c\u7684\u96c6\u5408\u3002\u4e00\u4e2a\u6570\u7ec4\u5f00\u59cb\u4e8e&#8221;[&#8220;\uff0c\u7ed3\u675f\u4e8e&#8221;]&#8221;\uff0c\u503c\u4e4b\u95f4\u7528&#8221;,&#8221;\u5206\u9694\u3002<br \/>\n[<br \/>\n{name:&#8221;Peggy&#8221;,email:&#8221;peggy@gmail.com&#8221;,homepage:&#8221;http:\/\/www.peggy.com&#8221;}, {name:&#8221;Peggy&#8221;,email:&#8221;peggy@gmail.com&#8221;,homepage:&#8221;http:\/\/www.peggy.com&#8221;},<br \/>\n{name:&#8221;Peggy&#8221;,email:&#8221;peggy@gmail.com&#8221;,homepage:&#8221;http:\/\/www.peggy.com&#8221;}<br \/>\n]<\/p>\n<p>3, \u503c\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001true\u3001false\u3001null\uff0c\u4e5f\u53ef\u4ee5\u662f\u5bf9\u8c61\u6216\u6570\u7ec4\u3002\u8fd9\u4e9b\u7ed3\u6784\u90fd\u80fd\u5d4c\u5957\u3002<\/p>\n<p>4,json\u793a\u4f8b\uff1a<\/p>\n<pre class=\"lang:python decode:true\">import json\n# Converting Python to JSON\njson_object = json.write( python_object )\n# Converting JSON to Python\npython_object = json.read( json_object )<\/pre>\n<p>5,simplejson \u793a\u4f8b\uff1a<\/p>\n<pre class=\"lang:python decode:true\">import simplejson\n# Converting Python to JSON\njson_object = simplejson.dumps( python_object )\n# Converting JSON to Python\npython_object = simplejson.loads( json_object )<\/pre>\n<p>\u4e8c\u3001python\u4eceweb\u63a5\u53e3\u4e0a\u67e5\u8be2\u4fe1\u606f<\/p>\n<p>1\uff0c\u5148\u770b\u4e2a\u4f8b\u5b50<br \/>\n&gt;&gt;&gt; import urllib<br \/>\n&gt;&gt;&gt; url=&#8217;http:\/\/www.example.com\/&#8217;<br \/>\n&gt;&gt;&gt; page=urllib.urlopen(url)<br \/>\n&gt;&gt;&gt; data=page.read()<br \/>\n&gt;&gt;&gt; print data \/\/\u8fd9\u4e2a\u5c31\u662fjson\u7684\u6570\u636e\u7ed3\u6784\uff0cstr\u7c7b\u578b<br \/>\n{&#8220;total&#8221;:1,&#8221;data&#8221;:[{&#8220;oume&#8221;:&#8221;&#8221;,&#8221;asNum&#8221;:&#8221;33333&#8243;,&#8221;cabinet&#8221;:&#8221;22&#8243;,&#8221;dMel&#8221;:&#8221;rr&#8221;,&#8221;hostname&#8221;:&#8221;h1.bkeep.com&#8221;,&#8221;logicSite&#8221;:&#8221;99&#8243;,&#8221;menfo&#8221;:{&#8220;amount&#8221;:4,&#8221;size&#8221;:8192},&#8221;ip&#8221;:&#8221;2.16.20.13&#8243;,&#8221;isOnline&#8221;:true,&#8221;useState&#8221;:&#8221;888&#8243;,&#8221;serviceTag&#8221;:&#8221;222X&#8221;,&#8221;cpuInfo&#8221;:{&#8220;amount&#8221;:2,&#8221;masterFrequency&#8221;:1995,&#8221;model&#8221;:&#8221;Intel(R) Xeon(R) CPU E5405 @ 2.00GHz&#8221;,&#8221;coreNum&#8221;:8,&#8221;l2CacheSize&#8221;:6144},&#8221;cabinetPositionNum&#8221;:&#8221;&#8221;,&#8221;buyTime&#8221;:&#8221;2009-06-29&#8243;,&#8221;manageIp&#8221;:&#8221;2.31.58.223&#8243;,&#8221;idc&#8221;:&#8221;\u817e\u8baf\u673a\u623f&#8221;,&#8221;resperson&#8221;:&#8221;\u9a6c\u5316\u817e&#8221;}],&#8221;errorMsg&#8221;:&#8221;&#8221;,&#8221;isSuccess&#8221;:true}<\/p>\n<p>&gt;&gt;&gt; type(data)<br \/>\n&lt;type &#8216;str&#8217;&gt;<\/p>\n<p>2\uff0c\u6709\u4e86json\u6570\u636e\u7ed3\u6784\uff0c\u6211\u5374\u4e0d\u77e5\u9053\u600e\u4e48\u628a\u5b83\u89e3\u6790\u51fa\u6765\uff0c\u5e78\u4e8f\u6709\u4e86\u674e\u5efa\u8f89\u7684\u6307\u5bfc\u3002\u5927\u6982\u601d\u8def\u662f\uff1a<\/p>\n<p><span style=\"color: #ff0000;\">\u9996\u5148\uff0cjson\u57fa\u672c\u4e0a\u662fkey\/value\u7684\uff0cpython\u4e2d\u5c31\u53eb\u5b57\u5178\u3002\u65e2\u7136\u662f\u5b57\u5178\uff0c\u90a3\u5c31\u5e94\u8be5\u5b89\u7167\u8bfb\u5b57\u5178\u7684\u65b9\u5f0f\u53bb\u8bfb\u3002<\/span><br \/>\n<span style=\"color: #ff0000;\">\u5c06\u4e0a\u9762\u7684data\u8f6c\u4e3a\u5b57\u5178\u7c7b\u578b\uff0c\u8fd9\u91cc\u7528json\u6a21\u5757\u7684read\u65b9\u6cd5\u3002<\/span><\/p>\n<p>&gt;&gt;&gt; import json<br \/>\n&gt;&gt;&gt; ddata=json.read(data)<br \/>\n&gt;&gt;&gt; ddata<br \/>\n{&#8216;isSuccess&#8217;: True, &#8216;errorMsg&#8217;: &#8221;, &#8216;total&#8217;: 1, &#8216;data&#8217;: [{&#8216;isOnline&#8217;: True, &#8216;idc&#8217;: &#8216;xe6x9dxadxe5xb7f&#8217;, &#8216;assetsNum&#8217;: &#8216;227003&#8217;, &#8216;responsibilityPerson&#8217;: &#8216;xe4xb9&#8217;, &#8216;deviceModel&#8217;: &#8216;a 1950&#8217;, &#8216;serviceTag&#8217;: &#8216;233&#8217;, &#8216;ip&#8217;: &#8216;2.1.20.163&#8217;, &#8216;hostname&#8217;: &#8216;h.bkeep.com&#8217;, &#8216;manageIp&#8217;: &#8216;2.31.58.223&#8217;, &#8216;cabinet&#8217;: &#8216;H05&#8217;, &#8216;buyTime&#8217;: &#8216;2009-06-29&#8217;, &#8216;useState&#8217;: &#8216;xe4xbd&#8217;, &#8216;memoryInfo&#8217;: {&#8216;amount&#8217;: 4, &#8216;size&#8217;: 8192}, &#8216;cpuInfo&#8217;: {&#8216;coreNum&#8217;: 8, &#8216;l2CacheSize&#8217;: 6144, &#8216;amount&#8217;: 2, &#8216;model&#8217;: &#8216;Intel(R) Xeon(R) CPU E5405 @ 2.00GHz&#8217;, &#8216;mastncy&#8217;: 1995}, &#8216;cabineionNum&#8217;: &#8221;, &#8216;outGuime&#8217;: &#8221;, &#8216;logicSite&#8217;: &#8216;xe46x969&#8217;}]}<br \/>\n&gt;&gt;&gt;<\/p>\n<p>\u770b\u770bddata\u5df2\u7ecf\u662fdict\u7c7b\u578b\u4e86<br \/>\n&gt;&gt;&gt; type(ddata)<br \/>\n&lt;type &#8216;dict&#8217;&gt;<\/p>\n<p>\u5176\u6b21\uff0c\u6211\u4eec\u4ee5\u8bfb\u5b57\u5178\u4e2dkey \u4e3a\u201ddata\u201d\u5bf9\u5e94\u7684\u952e\u503c<br \/>\n&gt;&gt;&gt; ddata[&#8216;data&#8217;] \/\/\u67e5\u770b\u5b57\u5178\u7684\u65b9\u6cd5\uff01<br \/>\n[{&#8216;isOnline&#8217;: True, &#8216;idc&#8217;: &#8216;xe6x9dxadxee6x88xbf&#8217;, &#8216;assetsNum&#8217;: &#8216;B50070100007003&#8217;, &#8216;responsibilityPerson&#8217;: &#8216;xe5xafx9a&#8217;, &#8216;deviceModel&#8217;: &#8216;PowerEdge 1950&#8217;, &#8216;serviceTag&#8217;: &#8216;7292X&#8217;, &#8216;ip&#8217;: &#8216;2.16.20.163&#8217;, &#8216;hostname&#8217;: &#8216;h1.bkeep.com&#8217;, &#8216;manageIp&#8217;: &#8216;2.31.58.223&#8217;, &#8216;cabinet&#8217;: &#8216;H05&#8217;, &#8216;buyTime&#8217;: &#8216;2009-06-29&#8217;, &#8216;useState&#8217;: &#8216;xe4xbdxbfe4xb8xad&#8217;, &#8216;memoryInfo&#8217;: {&#8216;amount&#8217;: 4, &#8216;size&#8217;: 8192}, &#8216;cpuInfo&#8217;: {&#8216;coreNum&#8217;: 8, &#8216;l2Caze&#8217;: 6144, &#8216;amount&#8217;: 2, &#8216;model&#8217;: &#8216;Intel(R) Xeon(R) CPU E5405 @ 2.00GHz&#8217;, &#8216;masterFrequency&#8217;: 1995}, &#8216;cabinetonNum&#8217;: &#8221;, &#8216;outeTime&#8217;: &#8221;, &#8216;logicSite&#8217;: &#8217;87xe7xabx99&#8242;}]<\/p>\n<p>&gt;&gt;&gt;type(ddata[\u2018data\u2019])<br \/>\n&lt;type &#8216;list&#8217;&gt;<\/p>\n<p>\u53d1\u73b0ddata[\u2018data\u2019]\u662f\u4e00\u4e2a\u5217\u8868\uff0c<span style=\"color: #ff0000;\">\u5217\u8868\u5c31\u8981\u7528\u5e8f\u53f7\u6765\u67e5\u8be2<\/span><br \/>\n&gt;&gt;&gt; ddata[&#8216;data&#8217;][0] \/\/\u67e5\u770b\u5217\u8868\u7684\u65b9\u6cd5\uff01<br \/>\n{&#8216;isOnline&#8217;: True, &#8216;idc&#8217;: &#8216;xe6x9d&#8217;, &#8216;assetsNum&#8217;: &#8216;07003&#8217;, &#8216;resposon&#8217;: &#8216;xe5xbc\\xafx9a&#8217;, &#8216;deviceModel&#8217;: &#8216;PowerEdge 1950&#8217;, &#8216;serviceTag&#8217;: &#8216;7002X&#8217;, &#8216;ip&#8217;: &#8216;2.16.20.163&#8217;, &#8216;hostname&#8217;: &#8216;h1.bkeep.com&#8217;, &#8216;manageIp&#8217;: &#8216;2.31.58.223&#8217;, &#8216;cabinet&#8217;: &#8216;H05&#8217;, &#8216;buyTime&#8217;: &#8216;2009-06-29&#8217;, &#8216;useState&#8217;: &#8216;x94xa8xe4x8xad&#8217;, &#8216;memoryInfo&#8217;: {&#8216;amount&#8217;: 4, &#8216;size&#8217;: 8192}, &#8216;cpuInfo&#8217;: {&#8216;coreNum&#8217;: 8, &#8216;l2CacheSize&#8217;: 6144, &#8216;amount&#8217;: 2, &#8216;model&#8217;: &#8216;Intel(R) Xeon(R) CPU E5405 @ 2.00GHz&#8217;, &#8216;masterFrequency&#8217;: 1995}, &#8216;cabinetPx99&#8217;}<br \/>\n&gt;&gt;&gt;<\/p>\n<p>\u5475\u5475\uff0cddata[\u2018data\u2019]\u5217\u8868\u76840\u53f7\u5143\u7d20\u662f\u4e2a\u5b57\u5178\u3002\u3002<\/p>\n<p>\u597d\uff0c\u90a3\u6211\u4eec\u67e5\u67e5key\u4e3aidc\u7684\u952e\u503c\u662f\u591a\u5c11<br \/>\n&gt;&gt;&gt; ddata[&#8216;data&#8217;][0][&#8216;idc&#8217;] \/\/\u67e5\u770b\u5b57\u5178\u7684\u65b9\u6cd5\uff01<br \/>\n&#8216;xe6x8xbf&#8217;<\/p>\n<p>&gt;&gt;&gt; print ddata[&#8216;data&#8217;][0][&#8216;idc&#8217;]<br \/>\n\u6df1\u5733\u817e\u8baf\u673a\u623f<\/p>\n<p><span style=\"color: #ff0000;\">\u770b\u5230\u8fd9\u91cc\u7ec8\u4e8e\u660e\u767d\u600e\u4e48\u89e3\u6790json\u6570\u636e\u7ed3\u6784\u4e86\u3002\u3002\u3002\u90a3\u5c31\u662f\u201c<strong>\u4e00\u5c42\u4e00\u5c42\u5f80\u4e0b\u5265<\/strong>\u201d<\/span><\/p>\n<p>\u3010Python\u3011 \u5982\u4f55\u89e3\u6790json\u6570\u636e\u7ed3\u6784 &#8211; bkeep\u7684\u65e5\u5fd7 &#8211; \u7f51\u6613\u535a\u5ba2 <a href=\"http:\/\/bkeep.blog.163.com\/blog\/static\/12341429020113156582685\/\" target=\"_blank\">http:\/\/bkeep.blog.163.com\/blog\/static\/12341429020113156582685\/<\/a><br \/>\n==============================<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<pre class=\"lang:python decode:true  \">#!\/usr\/bin\/env python\n#coding=utf-8\nimport json\n\ns = '[{\"name\":\"\u9e1f\u5de2\",\"point\":{\"lat\":\"39.990\",\"lng\":\"116.397\"},\"desc\":\"\u5965\u8fd0\u4f1a\u4e3b\u573a\u5730\"},{\"name\":\"\u5317\u5927\u4e52\u4e53\u7403\u9986\",\"point\":{\"lat\":\"39.988\",\"lng\":\"116.315\"},\"desc\":\"\u4e52\u4e53\u7403\u6bd4\u8d5b\u573a\u5730\"},{\"name\":\"\u5317\u4eac\u5de5\u4eba\u4f53\u80b2\u573a\",\"point\": {\"lat\":\"39.930\",\"lng\":\"116.446\"},\"desc\":\"\u8db3\u7403\u6bd4\u8d5b\u573a\u5730\"}]'\n\nlocations = json.read(s)\n\n#\u6ce8\uff1aread\u662fjson-py.py\u548cminijson.py\u7684\u65b9\u6cd5\uff0c\u800cpython2.6\u5f00\u59cb\u81ea\u5e26\u7684lib\u5e93\u91cc\u7528\u7684\u662fsimplejson.py\uff0c\u5176\u6ca1\u6709read\u65b9\u6cd5\uff0c\u800c\u662fload\/loads\nprint str(len(locations)) #\u56e0\u4e3ajson.load()\u4e4b\u540e\u7684\u5bf9\u8c61\u5c31\u662fdict\u7c7b\u578b\u7684\uff01\nfor location in locations:\n    print location[\"name\"]\n    print location[\"point\"][\"lat\"]<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<pre class=\"lang:python decode:true\">#!\/usr\/local\/bin\/python\n#coding = utf-8\nimport os\n\nimport json Res='[{\"brief\":\"ooooo\",\"class_extid\":13,\"create_time\":1131783174,\"face_id\":4,\"flag\":6777217,\"flag_ext\":0,\"level\":0,\"max_member\":100,\"memo\":\"m~F~M0m~Zm~@~B\",\"name\":\"10502\",\"option\":2,\"ul\":[{\"flag\":4,\"u\":285},{\"flag\":4,\"u\":35}]}]'\nqDic = json.loads(Res)\n\nfor i in qDic:\n\u00a0 \u00a0 print i[\"class_extid\"]\nfor j in i[\"ul\"]:\n\u00a0 \u00a0 print str(j[\"flag\"]) + \"===\" + str(j[\"u\"])<\/pre>\n<p>==========================================================<\/p>\n<p>\u66f4\u591a\u8303\u4f8b\u8bf7\u53c2\u8003<a href=\"%20http:\/\/docs.python.org\/library\/json.html\" target=\"_blank\"> http:\/\/docs.python.org\/library\/json.html<\/a><\/p>\n<p>=======================================<br \/>\n&gt;&gt;&gt; import json<br \/>\n&gt;&gt;&gt; dir(json)<br \/>\n[&#8216;JSONDecoder&#8217;, &#8216;JSONEncoder&#8217;, &#8216;__all__&#8217;, &#8216;__author__&#8217;, &#8216;__builtins__&#8217;, &#8216;__doc__&#8217;, &#8216;__file__&#8217;, &#8216;__name__&#8217;, &#8216;__package__&#8217;, &#8216;__path__&#8217;, &#8216;__version__&#8217;, &#8216;_default_decoder&#8217;, &#8216;_default_encoder&#8217;, &#8216;decoder&#8217;, &#8216;dump&#8217;, &#8216;dumps&#8217;, &#8216;encoder&#8217;, &#8216;load&#8217;, &#8216;loads&#8217;, &#8216;scanner&#8217;]<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<pre class=\"lang:python decode:true  \">#!\/usr\/bin\/env python\n# coding=utf-8\nimport json\n\nf = file(\"baidu_nav_json.txt\"); #baidu_nav_json.txt\u662f\u6211\u7684\u767e\u5ea6\u9996\u9875\u4e2d\u7684\u5bfc\u822a\uff0c\u67e5\u770b\u6e90\u7801\uff0c\u5c06\u5176\u5185\u5bb9\u4fdd\u5b58\u4e0b\u6765\u5b58\u4e3a\u6587\u4ef6\uff08utf-8\u683c\u5f0f\uff09\ns = json.load(f)\n#print type(s)\nprint str(len(s)) # 4\nprint s.keys() # [u'errNo', u'data', u'isNplus', u'defaultDirId']\n\n#print s[\"data\"]\n#print s[\"data\"][10][\"navs\"][0]\n#print s[\"data\"][10][\"navs\"][0][\"name\"], s[\"data\"][10][\"navs\"][0][\"url\"]\n\nfor i in range(0, len(s[\"data\"])):\n    print s[\"data\"][i][\"dirName\"]\n    for j in range(0, len(s[\"data\"][i][\"navs\"])):\n        print s[\"data\"][i][\"navs\"][j][\"name\"], s[\"data\"][i][\"navs\"][j][\"url\"]\n    print\n####\nf.close<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n\u4e0b\u9762\u7ed9\u51fa\u4e00\u4e2a\u4f7f\u7528python\u89e3\u6790json\u7684\u7b80\u5355\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"lang:python decode:true\">#!\/usr\/bin\/python\nimport json\n#Function:Analyze json script\n#Json is a script can descript data structure as xml,\n#for detail, please refer to \"http:\/\/json.org\/json-zh.html\".\n\n#Note:\n#1.Also, if you write json script from python,\n#you should use dump instead of load. pleaser refer to \"help(json)\".\n\n#json file:\n#The file content of temp.json is:\n#{\n# \"name\":\"00_sample_case1\",\n# \"description\":\"an example.\"\n#}\n#f = file(\"temp.json\");\n#s = json.load(f)\n#print s\n#f.close\n\n#json string:\ns = json.loads('{\"name\":\"test\", \"type\":{\"name\":\"seq\", \"parameter\":[\"1\", \"2\"]}}')\nprint s\nprint s.keys()\nprint s[\"name\"]\nprint s[\"type\"][\"name\"]\nprint s[\"type\"][\"parameter\"][1]<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>python json \u4e2d\u6587 python\u4e2d\u81ea\u5e26\u4e86\u5904\u7406python\u7684\u6a21\u5757\uff0c\u4f7f\u7528\u65f6\u5019\u76f4\u63a5import json\u5373 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[181,8],"class_list":["post-368","post","type-post","status-publish","format-standard","hentry","category-tools","tag-json","tag-python"],"views":4616,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/368","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=368"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/368\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}