zoukankan      html  css  js  c++  java
  • json

    1、python中的数据类型和json中的对应关系。
       +-------------------+---------------+
        | Python                 | JSON              |
       +===========+=========+
        | dict                       | objects'{"":""}' |
        +-------------------+---------------+
        | list, tuple              |   array[]           |
        +-------------------+---------------+
        | str                         |      string  '""' |
        +-------------------+---------------+
        | int, float                | 'number'        |
        +-------------------+---------------+
        | True                      | 'true '            |
        +-------------------+---------------+
        | False                     | 'false'             |
        +-------------------+---------------+
        | None                    |  'null'              |
        +-------------------+---------------+
    注意:对应的objects'{"":""}'数据格式中,要想转化成python中的dict,其中的键值对必须是双引号括起来的(如此才符合json数据格式的标准规定,才能是一种json数据格式)。
     
    2、python数据格式和json相互转化的对应方法
    python---->json    json.dumps()
    json---->python    json.loads
     
    例如:
    print(json.loads("true")) ===>True
     
    3、写到本地操作
    import json
    info={"name":'alex'}
    
    #写到本地操作:
    with open("json.txt","w") as f:
        f.write(json.dumps(info))
    
    # 读取操作
    with open("json.txt",'r') as f:
        data=f.read()
    
    data=json.loads(data)
    print(data)
     
     
     
  • 相关阅读:
    什么是HTTP?
    什么是OSI的第7层
    OSI7层模型
    什么是WAF?
    什么是Mirai僵尸网络
    什么是僵尸网络?
    洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk
    如何给数组的结构体排序 关于sort用法
    洛谷 P1803 凌乱的yyy / 线段覆盖
    洛谷 P1007 独木桥
  • 原文地址:https://www.cnblogs.com/aberwang/p/10397513.html
Copyright © 2011-2022 走看看