分类: Python/Ruby
2010-03-22 15:03:25
http://summerbell.javaeye.com/blog/361934
首先从python.org/pypi/python-json下载python-json, 然后安装。
解压zip包 然后把json.py minjson.py 拷到 /usr/lib/python2.5/下面就行了。
怎样使用请看:
或者如下简单例子:
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"]
对象是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。
import json
d = {}
d['name'] = 'Beijing'
d['phone'] = '45738903'
d['age'] = 30
print "Dictionary Object:"
print d
print "JSON Data:"
print json.write(d)
输出结果:
Dictionary Object:
{'phone': '45738903', 'age': 30, 'name': 'Beijing'}
JSON Data:
{"phone":"45738903","age":30,"name":"Beijing"}