国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python學(xué)習(xí)筆記之解析json的方法分析

2020-02-23 04:34:44
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Python解析json的方法。分享給大家供大家參考,具體如下:

剛學(xué)習(xí)到Python中解析json的方法,覺得有必要在這里坐下筆記。

我是在python的內(nèi)部環(huán)境中看的

Encoding basic Python object hierarchies

>>> 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('//')"http://">>> 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"]'

Compact encoding::

>>> import json>>> json.dumps([1,2,3,{'4': 5, '6': 7}], sort_keys=True, separators=(',',':'))'[1,2,3,{"4":5,"6":7}]'Pretty printing::>>> import json>>> print json.dumps({'4': 5, '6': 7}, sort_keys=True,         indent=4, separators=(',', ': ')){  "4": 5,  "6": 7}

Decoding JSON::

>>> import json>>> obj = [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == objTrue>>> json.loads('"http://"foo//bar"') == u'"foo/x08ar'True>>> from StringIO import StringIO>>> io = StringIO('["streaming API"]')>>> json.load(io)[0] == 'streaming API'True

Specializing JSON object decoding::

>>> import json>>> def as_complex(dct):   if '__complex__' in dct:     return complex(dct['real'], dct['imag'])   return dct>>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',   object_hook=as_complex)(1+2j)>>> from decimal import Decimal>>> json.loads('1.1', parse_float=Decimal) == Decimal('1.1')True

Specializing JSON object encoding::

>>> import json>>> def encode_complex(obj):   if isinstance(obj, complex):     return [obj.real, obj.imag]   raise TypeError(repr(o) + " is not JSON serializable")>>> json.dumps(2 + 1j, default=encode_complex)'[2.0, 1.0]'>>> json.JSONEncoder(default=encode_complex).encode(2 + 1j)'[2.0, 1.0]'>>> ''.join(json.JSONEncoder(default=encode_complex).iterencode(2 + 1j))'[2.0, 1.0]'

或者也可以去看官方文檔,自己能學(xué)到東西才是真的!

PS:關(guān)于json操作,這里再為大家推薦幾款比較實(shí)用的json在線工具供大家參考使用:

在線JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
http://tools.jb51.net/code/json

JSON在線格式化工具:

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临沧市| 晋城| 健康| 巍山| 琼中| 龙江县| 齐齐哈尔市| 施甸县| 卢湾区| 古田县| 皋兰县| 虎林市| 南京市| 唐海县| 黄龙县| 桐乡市| 东台市| 东乌珠穆沁旗| 正镶白旗| 吉林省| 南安市| 囊谦县| 额尔古纳市| 锡林郭勒盟| 清新县| 馆陶县| 隆尧县| 古蔺县| 宁武县| 仪征市| 五莲县| 温州市| 临泉县| 中超| 三都| 当涂县| 瓦房店市| 巨鹿县| 勐海县| 南宫市| 高要市|