zoukankan      html  css  js  c++  java
  • python json

    #  -*- coding: utf-8 -*-
    # !/usr/bin/python
    
    import json
    import sys
    
    if __name__ == '__main__':
        # 将python对象test转换json对象
        test = {"username":"测试","age":16}
        print type(test)
        python_to_json = json.dumps(test,ensure_ascii=True)
        print python_to_json
        print type(python_to_json)
    
        # 将json对象转换成python对象
        json_to_python = json.loads(python_to_json)
        print type(json_to_python)
        print json_to_python['username']
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    <type 'dict'>
    {"username": "u6d4bu8bd5", "age": 16}
    <type 'str'>
    <type 'dict'>
    测试
    
    
    u6d4bu8bd5 转成中文 测试
    
    #  -*- coding: utf-8 -*-
    # !/usr/bin/python
    
    import json
    import sys
    
    if __name__ == '__main__':
        # 将python对象test转换json对象
        test = {"username":"测试","age":16}
        print type(test)
        python_to_json = json.dumps(test,ensure_ascii=False)
        print python_to_json
        print type(python_to_json)
    
        # 将json对象转换成python对象
        json_to_python = json.loads(python_to_json)
        print type(json_to_python)
        print json_to_python['username']
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    <type 'dict'>
    {"username": "测试", "age": 16}
    <type 'str'>
    <type 'dict'>
    测试
    
    
    ensure_ascii:默认值True,如果dict内含有non-ASCII的字符,则会类似uXXXX的显示数据,设置成False后,就能正常显示

  • 相关阅读:
    delphi 如何让ScrollBox的内容与滚动条一起实时滚动
    C# Wpf集合双向绑定
    C# Wpf异步修改UI,多线程修改UI(二)
    C# 自定义线程修改UI(一)
    WPF FindName()查找命名注册的元素
    数据绑定
    WPF绑定Binding及模式
    WPF-Binding的源
    WPF-Binding对数据的检验
    WPF-数据转换
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349588.html
Copyright © 2011-2022 走看看