码迷,mamicode.com
首页 > 编程语言 > 详细

Python3---标准库json

时间:2020-02-08 19:29:29      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:als   encoding   white   字符串   sort   cep   sub   hint   script   

json

该文章描述了json库的定义以及当中常见的方法使用。

2020-02-08

天象独行

  在编写接口传递数据时,需要使用JSON对数据进行封装。python和json数据类型的转换的转换。这个功能的方法包含在标准库json。

  查看标准库当中的包含的方法

import requests
import json
print(dir(json))

[JSONDecodeError, JSONDecoder, JSONEncoder, __all__, __author__, __builtins__, __cached__, __doc__, __file__, __loader__, __name__, __package__, __path__, __spec__, __version__, _default_decoder, _default_encoder, codecs, decoder, detect_encoding, dump, dumps, encoder, load, loads, scanner]
  •   json.dumps

  查看json.dumps使用帮助,我们知道json.dumps可以将python对象编码成JSON字符串。

import requests
import json
print(help(json.dumps))

Help on function dumps in module json:

dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
    Serialize ``obj`` to a JSON formatted ``str``.
    
    If ``skipkeys`` is true then ``dict`` keys that are not basic types
    (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
    instead of raising a ``TypeError``.
    
    If ``ensure_ascii`` is false, then the return value can contain non-ASCII
    characters if they appear in strings contained in ``obj``. Otherwise, all
    such characters are escaped in JSON strings.
    
    If ``check_circular`` is false, then the circular reference check
    for container types will be skipped and a circular reference will
    result in an ``OverflowError`` (or worse).
    
    If ``allow_nan`` is false, then it will be a ``ValueError`` to
    serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in
    strict compliance of the JSON specification, instead of using the
    JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
    
    If ``indent`` is a non-negative integer, then JSON array elements and
    object members will be pretty-printed with that indent level. An indent
    level of 0 will only insert newlines. ``None`` is the most compact
    representation.
    
    If specified, ``separators`` should be an ``(item_separator, key_separator)``
    tuple.  The default is ``(, , : )`` if *indent* is ``None`` and
    ``(,, : )`` otherwise.  To get the most compact JSON representation,
    you should specify ``(,, :)`` to eliminate whitespace.
    
    ``default(obj)`` is a function that should return a serializable version
    of obj or raise TypeError. The default simply raises TypeError.
    
    If *sort_keys* is true (default: ``False``), then the output of
    dictionaries will be sorted by key.
    
    To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
    ``.default()`` method to serialize additional types), specify it with
    the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.
  •   json.loads

  查看json.loads方法使用,用于解码JSON数据,该函数返回Python字段的数据类型。

import requests
import json
print(help(json.loads))

Help on function loads in module json:

loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
    Deserialize ``s`` (a ``str``, ``bytes`` or ``bytearray`` instance
    containing a JSON document) to a Python object.
    
    ``object_hook`` is an optional function that will be called with the
    result of any object literal decode (a ``dict``). The return value of
    ``object_hook`` will be used instead of the ``dict``. This feature
    can be used to implement custom decoders (e.g. JSON-RPC class hinting).
    
    ``object_pairs_hook`` is an optional function that will be called with the
    result of any object literal decoded with an ordered list of pairs.  The
    return value of ``object_pairs_hook`` will be used instead of the ``dict``.
    This feature can be used to implement custom decoders.  If ``object_hook``
    is also defined, the ``object_pairs_hook`` takes priority.
    
    ``parse_float``, if specified, will be called with the string
    of every JSON float to be decoded. By default this is equivalent to
    float(num_str). This can be used to use another datatype or parser
    for JSON floats (e.g. decimal.Decimal).
    
    ``parse_int``, if specified, will be called with the string
    of every JSON int to be decoded. By default this is equivalent to
    int(num_str). This can be used to use another datatype or parser
    for JSON integers (e.g. float).
    
    ``parse_constant``, if specified, will be called with one of the
    following strings: -Infinity, Infinity, NaN.
    This can be used to raise an exception if invalid JSON numbers
    are encountered.
    
    To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
    kwarg; otherwise ``JSONDecoder`` is used.
    
    The ``encoding`` argument is ignored and deprecated since Python 3.1.

None

 

  

Python3---标准库json

标签:als   encoding   white   字符串   sort   cep   sub   hint   script   

原文地址:https://www.cnblogs.com/aaron456-rgv/p/12284505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!