目录
JSON编码和解码
JSON(),由(取代了RFC4627)和,是一种轻量级的数据交换格式,灵感来自对象文字语法(严格来说它不是JavaScript的子集,因为它额外支持了行分割U+2028和段分割U+2029语法)。
simplejson的API和标准库marshal及pickle模块类似。它是包含在Python2.6的JSON库的外部版本,目前仍保持兼容的Python2.5并有显著的性能优势,尽管没有使用C扩展加速。 simplejson也支持Python的3.3+。
simplejson的的发布下载地址:, 最新代码:
>>> import simplejson as json |
>>> json. dumps([ 'foo' , { 'bar' : ( 'baz' , None , 1.0 , 2 )}]) |
'["foo>", {"bar>": ["baz>", null, 1.0, 2]}]' |
>>> print ( json. dumps( " \>" foo \b ar" )) |
>>> print ( json. dumps( u' \u1234 ' )) |
>>> print ( json. dumps( ' \\ ' )) |
>>> print ( json. dumps({ "c>" : 0 , "b>" : 0 , "a>" : 0 }, sort_keys= True )) |
{ "a>" : 0 , "b>" : 0 , "c>" : 0 } |
>>> from simplejson.compat import StringIO |
>>> json. dump([ 'streaming API' ], io) |
>>> import simplejson as json |
>>> obj = [ 1 , 2 , 3 ,{ '4' : 5 , '6' : 7 }] |
>>> json. dumps( obj, separators= ( ',' , ':' ), sort_keys= True ) |
'[1,2,3,{"4>":5,"6>":7}]' |
>>> json. dumps( obj, sort_keys= True ) |
'[1, 2, 3, {"4>": 5, "6>": 7}]' |
>>> import simplejson as json |
>>> print ( json. dumps({ '4' : 5 , '6' : 7 }, sort_keys= True , indent= 4 * ' ' )) |
>>> import simplejson as json |
>>> obj = [ u'foo' , { u'bar' : [ u'baz' , None , 1.0 , 2 ]}] |
>>> json. loads( '["foo>", {"bar>":["baz>", null, 1.0, 2]}]' ) == obj |
>>> json. loads( '" \\ >"foo \\ bar"' ) == u'>"foo \x08 ar' |
>>> from simplejson.compat import StringIO |
>>> io = StringIO( '["streaming API>"]' ) |
>>> json. load( io)[ 0 ] == 'streaming API' |
>>> import simplejson as json |
>>> obj = [ u'foo' , { u'bar' : [ u'baz' , None , 1.0 , 2 ]}] |
>>> json. loads( '["foo>", {"bar>":["baz>", null, 1.0, 2]}]' ) == obj |
>>> json. loads( '" \\ >"foo \\ bar"' ) == u'>"foo \x08 ar' |
>>> from simplejson.compat import StringIO |
>>> io = StringIO( '["streaming API>"]' ) |
>>> json. load( io)[ 0 ] == 'streaming API' |
本文地址
原文:http://automationtesting.sinaapp.com/blog/m_json
0 Comments