zoukankan      html  css  js  c++  java
  • python 字符与字节 json序列和反序列及支持的类型

    b = b"demo" 
    
    s = "demo"
    
    # 字符串转字节
    s = bytes(s, encoding = "utf8")
    
    s = str.encode(s)
    
    # 字节转字符串 
    s = str(b, encoding = "utf8")
    
    s = bytes.decode(b)

    json 序列与反序列化

    '''
    Supports the following objects and types by default:
    python支持的Json类型 +-------------------+---------------+ | Python | JSON | +===================+===============+ | dict | object | {} +-------------------+---------------+ | list, tuple | array |[]() +-------------------+---------------+ | str | string |"" +-------------------+---------------+ | int, float | number |124 +-------------------+---------------+ | True | true | +-------------------+---------------+ | False | false | +-------------------+---------------+ | None | null | +-------------------+---------------+ '''
    name = {'name': "gggg"}
    name01 = """{"name": "gggg"}"""
    # 转化成字符串(序列化)
    name_dumps = json.dumps(name)
    # 转化成json格式(反序列化)
    name01_loads = json.loads(name01)
    
    # json.dumps 序列化时对中文默认使用的ascii编码.想输出真正的中文需要指定ensure_ascii=False:
    print(type(name_dumps)) print(type(name01_loads))

    js中处理json

    序列化:
      Json.stringify()
    反序列化
    Json.parse()
  • 相关阅读:
    ubuntu安装RabbitMQ
    pycharm中引入相对路径错误
    python之pip
    ubuntu安装django
    ubuntu16安装python3.7
    ubuntu上用源码进行一键安装mysql
    tar命令
    linux之bc命令
    linux源码安装
    ubuntu安装搜狗输入法后无法使用goland的快捷键 ctrl+alt+B
  • 原文地址:https://www.cnblogs.com/ls1997/p/10892653.html
Copyright © 2011-2022 走看看