• python3.5和python3.6关于json模块的区别


    python3.5中

      无法反序列化bytes数据必须decode成str才可以

    >>> import json
    >>> a = b'{"username": "xxx"}'
    >>> c = json.loads(a)
    
    '''
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 312, in loads
        s.__class__.__name__))
    TypeError: the JSON object must be str, not 'bytes'
    
    '''

      3.5解决办法:

    >>> a = b'123'
    >>> c = json.loads(a)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 312, in loads
        s.__class__.__name__))
    TypeError: the JSON object must be str, not 'bytes'
    >>> c = json.loads(a.decode('utf-8'))
    >>> c
    123
    

      

    python3.6中

      无论bytes类型或者str类型都可以反序列化

    >>> import json
    >>> a = b'{"username": "xxx"}'
    >>> c = json.loads(a)
    >>> g = b'{"username": "xxx"}'
    >>> h = json.loads(g.decode("utf-8"))
    

      

  • 相关阅读:
    第07组 Alpha冲刺(2/4)
    第07组 Alpha冲刺(1/4)
    团队项目-需求分析报告
    团队项目-选题报告
    1381 硬币游戏
    1381 硬币游戏
    1347 旋转字符串
    1344 走格子
    1305 Pairwise Sum and Divide
    1384 全排列
  • 原文地址:https://www.cnblogs.com/liujiliang/p/8366708.html
Copyright © 2020-2023  润新知