Python学习笔记之解析json的方法分析

Python学习笔记之解析json的方法分析,第1张

概述本文实例讲述了Python解析json的方法。分享给大家供大家参考,具体如下:刚学习到Python中解析json的方法,觉得有必要在这里坐下笔记。

本文实例讲述了Python解析Json的方法。分享给大家供大家参考,具体如下:

刚学习到Python中解析Json的方法,觉得有必要在这里坐下笔记。

我是在python的内部环境中看的

EnCoding basic Python object hIErarchIEs

>>> import Json>>> Json.dumps(['foo',{'bar': ('baz',None,1.0,2)}])'["foo",{"bar": ["baz",null,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,"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,{"4":5,"6":7}]'Pretty printing::>>> import Json>>> print Json.dumps({'4': 5,'6': 7},indent=4,': ')){  "4": 5,"6": 7}

DeCoding JsON::

>>> import Json>>> obj = [u'foo',{u'bar': [u'baz',2]}]>>> Json.loads('["foo",{"bar":["baz",2]}]') == objTrue>>> Json.loads('"\"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]'

或者也可以去看官方文档,自己能学到东西才是真的!

PS:关于Json *** 作,这里再为大家推荐几款比较实用的Json在线工具供大家参考使用:

在线JsON代码检验、检验、美化、格式化工具:
http://tools.jb51.net/code/json

JsON在线格式化工具:
http://tools.jb51.net/code/jsonformat

在线XML/JsON互相转换工具:
http://tools.jb51.net/code/xmljson

Json代码在线格式化/美化/压缩/编辑/转换工具:
http://tools.jb51.net/code/jsoncodeformat

在线Json压缩/转义工具:
http://tools.jb51.net/code/json_yasuo_trans

更多Python相关内容感兴趣的读者可查看本站专题:《Python *** 作json技巧总结》、《@L_404_6@》、《Python图片 *** 作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串 *** 作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录 *** 作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

总结

以上是内存溢出为你收集整理的Python学习笔记之解析json的方法分析全部内容,希望文章能够帮你解决Python学习笔记之解析json的方法分析所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1202266.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-04
下一篇2022-06-04

发表评论

登录后才能评论

评论列表(0条)

    保存