官方文档-JSON格式的编码器和解码器(可参考)概要.doc

官方文档-JSON格式的编码器和解码器(可参考)概要.doc

官方文档-JSON格式的编码器和解码器(可参考)概要

18.2.? HYPERLINK /python_278/library/json.html \l module-json \o json: Encode and decode the JSON format. json?—?JSON 格式的编码器和解码器 HYPERLINK /python_278/library/json.html \l module-json \o Permalink to this headline  在 2.6 版本新。  HYPERLINK / JSON (JavaScript 对象符号),由指定? HYPERLINK /html/rfc4627.html RFC 4627,是基于 HYPERLINK /wiki/JavaScript JavaScript语法的一个子集的轻量级数据交换格式 ( HYPERLINK /publications/files/ECMA-ST-ARCH/ECMA-262, 3rd edition, December 1999.pdf ECMA 262 第 3 版)。  HYPERLINK /python_278/library/json.html \l module-json \o json: Encode and decode the JSON format. json公开 API 的标准库 HYPERLINK /python_278/library/marshal.html \l module-marshal \o marshal: Convert Python objects to streams of bytes and back (with different constraints). marshal和 HYPERLINK /python_278/library/pickle.html \l module-pickle \o pickle: Convert Python objects to streams of bytes and back. pickle模块的用户所熟悉。 注:python的新版本一般都自带了json。 一些旧版本的python没有自带json,需要安装demjson模块 $ tar xvfz demjson-1.6.tar.gz $ cd demjson-1.6 $ python setup.py install 另外还有simplejson,从网上文档得知,如果没有c扩展加速,性能低下。对比python自带的json,json性能上可以接收,推荐使用自带的json模块 编码基本的 Python 对象层次结构:python自带了json模块 import json json.dumps([foo, {bar: (baz, None, 1.0, 2)}]) [foo, {bar: [baz, null, 1.0, 2]}] print json.dumps(\foo\bar) \foo\bar print json.dumps(u\u1234) \u1234 print json.dumps(\\) \\ print json.dumps({c: 0, b: 0, a: 0}, sort_keys=True) {a: 0, b: 0, c: 0} from StringIO import StringIO io = StringIO() json.dump([streaming API], io) io.getvalue() [streaming API] 压缩编码: import json json.dumps([1,2,3,{4: 5, 6: 7}], separators=(,,:)) [1,2,3,{4:5,6:7}] 漂亮的打印: import json print json.dumps({4: 5, 6: 7}, sort_keys=True, ... indent=4, separators=(,, : )) { 4: 5, 6: 7 } 解码 JSON: import json json.loads([foo, {bar:[baz, null, 1.0, 2]}]) [ufoo, {ubar: [ubaz, None, 1.0, 2]}] json.loads(\\foo\\bar) ufoo\x08ar from StringIO import StringIO io = StringIO([streaming API]) json.load(io) [ustreaming API] 专用

文档评论(0)

1亿VIP精品文档

相关文档