首页 > 编程语言 >Day 41 41.1 Python中json模块的loadloads方法实战及参数详解

Day 41 41.1 Python中json模块的loadloads方法实战及参数详解

时间:2023-05-21 18:34:13浏览次数:39  
标签:Python object 41.1 41 json 字符串 loads 转为

Python中json模块的load/loads方法实战及参数详解

【一】loads方法与load方法的异同

  • 在Python中json是一个非常常用的模块,这个主要有4个方法:

    • json.dumps

    • json.dump

    • json.loads

    • json.load

  • 这里主要分析讲解一下json的loads和load方法。

  • 这两个方法中都是把其他类型的对象转为Python对象,这里先说明一下Python对象

    • Python对象包括:
      • 所有Python基本数据类型,列表,元组,字典,自己定义的类,等等等等
      • 当然不包括Python的字符串类型,把字符串或者文件流中的字符串转为字符串会报错的

文件流:

​ 像水流一样把一个大的文件分成一段一段的流过去就叫文件流

【1】不同点

  • loads
    • 操作的是字符串
  • load
    • 操作的是文件流

【2】相同点

  • 除了第一个参数(要转换的对象)类型不同,其他所有的参数都相同
  • 最终都是转换成Python对象

【3】案例

  • 举例来说,除了要转换的对象,其他什么参数都不传:
# -*-coding: Utf-8 -*-
# @File : loads and load .py
# author: Chimengmeng
# blog_url : https://www.cnblogs.com/dream-ze/
# Time:2023/5/21
import json

s = '{"name": "dream", "age": 18, "gender": "man"}'
# json.loads读取字符串并转为Python对象
print(f"json.loads将字符串转为Python对象的类型:>>>>type(json.loads(s)) = {type(json.loads(s))}")
print(f"json.loads将字符串转为Python对象的值:>>>>json.loads(s) = {json.loads(s)}")
# json.loads将字符串转为Python对象的类型:>>>>type(json.loads(s)) = <class 'dict'>
# json.loads将字符串转为Python对象的值:>>>>json.loads(s) = {'name': 'dream', 'age': 18, 'gender': 'man'}


# json.load读取文件并将文件内容转为Python对象
# 数据文件要s.json的内容 --> {"name": "wade", "age": 54, "gender": "man"}
with open('text.json', 'r') as f:
    s1 = json.load(f)
    print(f"json.load将文件内容转为Python对象的类型:>>>>type(json.load(f)) = {type(s1)}")
    print(f"json.load将文件内容转为Python对象的值:>>>>json.load(f) = {s1}")
    # json.load将文件内容转为Python对象的类型:>>>>type(json.load(f)) = <class 'dict'>
    # json.load将文件内容转为Python对象的值:>>>>json.load(f) = {'name': 'dream', 'age': 18, 'gender': 'man'}

【二】转化成Python对象

  • 由于loads和load两个方法只是处理的数据源不同,其他的参数都是相同的,返回的结果类型也相同,故这是loads和load方法两个只说一个,

  • 日常工作中最常见的就是把字符串通过json.loads转为字典

    • 其实json的loads方法不仅可以把字符串转为字典
    • 还可以转为任何Python对象。
  • 比如说,转成python基本数据类型:

# -*-coding: Utf-8 -*-
# @File : 转换成Python对象 .py
# author: Chimengmeng
# blog_url : https://www.cnblogs.com/dream-ze/
# Time:2023/5/21
import json

print(f'json.loads 将整数类型的字符串转为int类型:>>>> type(json.loads("123456"))) --> {type(json.loads("123456"))}')
print(f'json.loads 将浮点类型的字符串转为float类型:>>>> type(json.loads("123.456")) --> {type(json.loads("123.456"))}')
print('json.loads 将boolean类型的字符串转为bool类型:>>>> type(json.loads("true")) --> {type(json.loads("true"))}')
print('json.loads 将列表类型的字符串转为列表:>>>> type(json.loads(\'["a", "b", "c"]\')) --> {}'.format(type(json.loads('["a", "b", "c"]'))))
print('json.loads 将字典类型的字符串转为字典:>>>> type(json.loads(\'{"a": 1, "b": 1.2, "c": true, "d": "ddd"}\')) --> %s' % str(type(json.loads('{"a": 1, "b": 1.2, "c": true, "d": "ddd"}'))))


# json.loads 将整数类型的字符串转为int类型:>>>> type(json.loads("123456"))) --> <class 'int'>
# json.loads 将浮点类型的字符串转为float类型:>>>> type(json.loads("123.456")) --> <class 'float'>
# json.loads 将boolean类型的字符串转为bool类型:>>>> type(json.loads("true")) --> {type(json.loads("true"))}
# json.loads 将列表类型的字符串转为列表:>>>> type(json.loads('["a", "b", "c"]')) --> <class 'list'>
# json.loads 将字典类型的字符串转为字典:>>>> type(json.loads('{"a": 1, "b": 1.2, "c": true, "d": "ddd"}')) --> <class 'dict'>
  • json模块会根据你的字符串自动转为最符合的数据类型
  • 但是需要注意的是不能把转为字符串,否则会报json.decoder.JSONDecodeError错误:
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

【三】json.load(s)的参数

  • 我们先看下 json.loads 方法的签名:
def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
        parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
    """Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON    # 把一个字符串反序列化为Python对象,这个字符串可以是str类型的,也可以是unicode类型的
    document) to a Python object.


    If ``s`` is a ``str`` instance and is encoded with an ASCII based encoding    # 如果参数s是以ASCII编码的字符串,那么需要手动通过参数encoding指定编码方式,
    other than utf-8 (e.g. latin-1) then an appropriate ``encoding`` name         # 不是以ASCII编码的字符串,是不被允许的,你必须把它转为unicode
    must be specified. Encodings that are not ASCII based (such as UCS-2)
    are not allowed and should be decoded to ``unicode`` first.


    ``object_hook`` is an optional function that will be called with the        # object_hook参数是可选的,它会将(loads的)返回结果字典替换为你所指定的类型
    result of any object literal decode (a ``dict``). The return value of        # 这个功能可以用来实现自定义解码器,如JSON-RPC
    ``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    # object_pairs_hook参数是可选的,它会将结果以key-value列表的形式返回
    result of any object literal decoded with an ordered list of pairs.  The      # 形式如:[(k1, v1), (k2, v2), (k3, v3)]
    return value of ``object_pairs_hook`` will be used instead of the ``dict``.   # 如果object_hook和object_pairs_hook同时指定的话优先返回object_pairs_hook
    This feature can be used to implement custom decoders that rely on the
    order that the key and value pairs are decoded (for example,
    collections.OrderedDict will remember the order of insertion). If
    ``object_hook`` is also defined, the ``object_pairs_hook`` takes priority.


    ``parse_float``, if specified, will be called with the string                 # parse_float参数是可选的,它如果被指定的话,在解码json字符串的时候,
    of every JSON float to be decoded. By default this is equivalent to           # 符合float类型的字符串将被转为你所指定的,比如说你可以指定为decimal.Decimal
    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                   # parse_int参数是可选的,它如果被指定的话,在解码json字符串的时候,
    of every JSON int to be decoded. By default this is equivalent to             # 符合int类型的字符串将被转为你所指定的,比如说你可以指定为float
    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              # parse_constant参数是可选的,它如果被指定的话,在解码json字符串的时候,
    following strings: -Infinity, Infinity, NaN.                                  # 如果出现以以下字符串: -Infinity, Infinity, NaN 那么指定的parse_constant方法将会被调用到
    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``         # 你也可以用cls参数通过实现一个JSONDecoder的子类,来代替JSONDecoder,通过这个功能你可以自定义上面的那些parse_xxx参数,这里就不举例了
    kwarg; otherwise ``JSONDecoder`` is used.


    """

【1】s参数

  • 把一个字符串反序列化为Python对象,这个字符串可以是str类型的,也可以是unicode类型的
  • 如果参数s是以ASCII编码的字符串,那么需要手动通过参数encoding指定编码方式,不是以ASCII编码的字符串,是不被允许的,你必须把它转为unicode
  • 对于loads方法来说,s是一个字符串,而对于load方法来说,是一个数据流文件

【2】object_hook参数

  • object_hook参数是可选的,它会将(loads的)返回结果字典替换为你所指定的类型,这个功能可以用来实现自定义解码器,如JSON-RPC
  • 这里先定义一个Person对象:
class Person:
    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender

    def toJSON(self):
        return {
            "name": self.name,
            "age": self.age,
            "gender": self.gender
        }

    @staticmethod
    def parseJSON(dct):
        if isinstance(dct, dict):
            p = Person(dct["name"], int(dct['age']), dct['gender'])
            return p
        return dct
  • 案例:
# -*-coding: Utf-8 -*-
# @File : 参数 .py
# author: Chimengmeng
# blog_url : https://www.cnblogs.com/dream-ze/
# Time:2023/5/21

import json


class Person:
    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender

    def toJSON(self):
        return {
            "name": self.name,
            "age": self.age,
            "gender": self.gender
        }

    @staticmethod
    def parseJSON(dct):
        if isinstance(dct, dict):
            p = Person(dct["name"], int(dct['age']), dct['gender'])
            return p
        return dct


s = '{"name": "dream", "age": 18, "gender": "man"}'
# 测试json.loads方法的object_hook参数
p = json.loads(s, object_hook=Person.parseJSON)
print("json.loads 是否将字符串转为字典了: --> " + str(isinstance(p, dict)))
print("json.loads 是否将字符串转为Person对象了: --> " + str(isinstance(p, Person)))

# json.loads 是否将字符串转为字典了: --> False
# json.loads 是否将字符串转为Person对象了: --> True

【3】object_pairs_hook参数

  • object_pairs_hook参数是可选的,它会将结果以key-value有序列表的形式返回
    • 形式如:
      • [(k1, v1), (k2, v2), (k3, v3)]
  • 如果object_hook和object_pairs_hook同时指定的话优先返回object_pairs_hook

【4】parse_float参数

  • parse_float参数是可选的,它如果被指定的话,在解码json字符串的时候,符合float类型的字符串将被转为你所指定的
  • 比如说你可以指定为decimal.Decimal

【5】 parse_int参数

  • parse_int参数是可选的,它如果被指定的话,在解码json字符串的时候
    • 符合int类型的字符串将被转为你所指定的
      • 比如说你可以指定为float

【6】parse_constant参数

  • parse_constant参数是可选的,它如果被指定的话,在解码json字符串的时候
    • 如果出现以以下字符串:
    • -InfinityInfinityNaN
    • 那么指定的parse_constant方法将会被调用到

【7】 cls参数

  • 通过官方文档的注释我们可以知道json.load(s)方法具体的实现是通过JSONCoder类实现的,
  • 而cls参数是用于自定义一个JSONCoder的子类,用于替换JSONCoder类,
  • 通过这个功能你可以自定义上面的那些parse_xxx参数,

标签:Python,object,41.1,41,json,字符串,loads,转为
From: https://www.cnblogs.com/dream-ze/p/17418960.html

相关文章

  • Day 41 41.3 URL 解码 编码详解
    【一】URL解码/编码详解当URL路径或者查询参数中,带有中文或者特殊字符的时候,就需要对URL进行编码(采用十六进制编码格式)。URL编码的原则是使用安全字符去表示那些不安全的字符。安全字符,指的是没有特殊用途或者特殊意义的字符。【二】URL基本组成URL是由一些简单的......
  • Day 41 41.2 Python中json模块之dumps参数详解
    Python的JSON用法之dumps的各种参数用法(详细)JSON是用于存储和交换数据的语法。JSON(JavaScriptObjectNotation)最初是用JavaScript对象表示法编写的文本,但随后成为了一种常见格式,被包括Python在内的众多语言采用。在使用json方法的时候要记住先引进这个库importjson......
  • Python 设计模式-观察者模式
    观察者模式是一种行为设计模式,它允许你定义一种订阅机制,可以在对象事件发生时通知多个观察者对象。下面是一个简单的Python观察者模式代码示例:classSubject:"""被观察者类,维护观察者列表,并在状态发生改变时通知观察者"""def__init__(self):self._......
  • 【python】使用pyinstaller打包python程序为exe【转】
    pyinstaller介绍PyInstaller除了win32平台外还支持Linux,Unix平台.py2exe的用法在前面的博文里已经有了,现在看看PyInstaller,pyinstaller是目前应用最多的python打包工具,也是我最常用的。PyInstaller本身并不是Python模块,所以安装时随便放在哪儿都行。优点:可将python文件转......
  • 5年Python生涯总结出10个玩Python必备的网站
    “玩Python必备的10大精品网站”5年Python沉淀,总结一下作为一个Python玩家,必须要知道的十大精品网站。无用请吐槽!有用请点赞!!爱我请三连!!!目录一、模块查询二、闯关游戏三、Django教程四、Python资源大全五、数据/竞赛/学习六、Python语法/项目经验七、Flask资料大全八、机器学习中文......
  • Python数据加密方法详解|为你的隐私再上一把锁!
    前言数字化时代下,数据安全是各大公司及个人最关心的一点,作为一个Python语言使用者我们该如何进行数据的加密呢?本文二哥领着大家来了解一下。目录前言一、数据加密的概念二、直接编码加密三、Binascii编码加密三、Base64编码加密四、URL编码加密五、Hashlib加密1、MD52、SHA1加密六......
  • 左程云动态规划问题学习(python版本重写)
    哔哩哔哩:6.二次优化(3)_哔哩哔哩_bilibili第一个版本对动态规划的理解#问题有大量的重复问题,比如求feibolaqie(5)=feibolaqie(4)+feibolaqie(3),#所以有重复问题,通过缓存优化,把以前求过的问题做缓存#deffeibolaqie(n):#ifn==1:#return1#eli......
  • python爬取《肖申克的救赎》电影演员
    importrequestsfrombs4importBeautifulSoup#豆瓣电影页面链接url='https://movie.douban.com/subject/1292052/'#设置请求头信息,模拟浏览器请求headers={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,lik......
  • python-docx - 3
    1.样式1.1访问样式使用Document.styles属性访问样式。fromdocximportDocumentdocument=Document()#获取样式对象,这里面可以像字典一样访问,也可以迭代styles=document.stylesforstyleinstyles:print(style.name,"\t",style.type)#获取一个正文样式......
  • Python使用pip安装第三方包
    ​ 参考文章:如何安装第三方的Python包?-知乎​pipinstall-i网址包名称例如:pipinstall-ihttps://pypi.tuna.tsinghua.edu.cn/simple/numpy常用的网址有:清华:https://pypi.tuna.tsinghua.edu.cn/simple/阿里云:http://mirrors.aliyun.com/pypi/simple/......