zoukankan      html  css  js  c++  java
  • python之json模块

    json字符串对象转字典,采用loads方法,当转换后的字典有Boolean类型的时候,要注意json字符串的值小写:

    import json
    a='{"CityId":18,"CityName":"西安","ProvinceId":27,"CityOrder":1,"c":null,"d":true,"e":false}'
    b=json.loads(a)
    print (b)
    print (type(b))
    print (b["d"])
    print (type(b["d"]))
    
    E:python36python3.exe E:/uiautomator/test.py
    {'CityId': 18, 'CityName': '西安', 'ProvinceId': 27, 'CityOrder': 1, 'c': None, 'd': True, 'e': False}
    <class 'dict'>
    True
    <class 'bool'>

    字典转json字符串,采用dumps方法,如果转换的字典带有中文需要带参数ensure_ascii = False,确保原样输出:

    import json
    a={"CityId":18,"CityName":"西安","ProvinceId":27,"CityOrder":1,"c":False}
    b=json.dumps(a,ensure_ascii = False)
    print (b)
    print (type(b))
    
    E:python36python3.exe E:/uiautomator/test.py
    {"CityId": 18, "CityName": "西安", "ProvinceId": 27, "CityOrder": 1, "c": false}
    <class 'str'>
    

      

  • 相关阅读:
    030-B+树(三)
    028-B+树(一)
    027-B树(二)
    026-B树(一)
    025-红黑树(六)
    024-红黑树(五)
    023-红黑树(四)
    022-红黑树(三)
    021-红黑树(二)
    020-红黑树(一)
  • 原文地址:https://www.cnblogs.com/letmeiscool/p/8567566.html
Copyright © 2011-2022 走看看