原文:http://www.pythonclub.org/modules/jsonjavascript
JSON是JavaScript Object Notation的缩写,SJON是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。html
它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。java
JSON采用彻底独立于语言的文本格式,可是也使用了相似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript,Perl, Python等)。python
这些特性使JSON成为理想的数据交换语言。json
Python在2.6之后的版本中报好了JSON模块,能够直接在Python中import json.api
Python 2.7中自带了JSON模块,直接import json就能够使用了。ui
#! /tools/cfr/bin/python import json s = '[{"name":"niaochao","point":{"lat":"39.990","lng":"116.397"},"desc":"aoyunhuizhuchangdi"},{"name":"beidapingpangqiuguan","point":{"lat":"39.988","lng":"116.315"},"desc":"pingpangqiubisaichangdi"},{"name":"beijinggongrentiyuchang","point":{"lat":"39.930","lng":"116.446"},"desc":"zuqiubisaichangdi"}]' locations = json.loads(s) print str(len(locations)) for location in locations: print location["name"] print location["point"]["lat"]
结果为:spa
3 niaochao 39.990 beidapingpangqiuguan 39.988 beijinggongrentiyuchang 39.930
可是对已Python 2.5之前的版本,这须要咱们下载json模块。code
首先从http://pypi.python.org/pypi/python-json下载python-json, 而后安装。htm
解压zip包 而后把json.py minjson.py 拷到 /usr/lib/python2.5/下面就好了。
怎样使用请看:http://docs.python.org/library/json.html
import json s = '[{"name":"niaochao","point":{"lat":"39.990","lng":"116.397"},"desc":"aoyunhuizhuchangdi"},{"name":"beidapingpangqiuguan","point":{"lat":"39.988","lng":"116.315"},"desc":"pingpangqiubisaichangdi"},{"name":"beijinggongrentiyuchang","point":{"lat":"39.930","lng":"116.446"},"desc":"zuqiubisaichangdi"}]' locations = json.read(s) print str(len(locations)) for location in locations: print location["name"] print location["point"]["lat"]