python json
- #-*-coding:utf-8-*-
- '''编码格式记得统一,不然容易出现中文乱码,推荐用utf-8'''
- import json
- ##################json单对象##############################
- #声明初始化一个变量
- obj={'name':'张三', 'email':'ljq@gmail.com'}
- print type(obj) #<type 'dict'>
- #字典转为json
- #json_obj=json.dumps(obj)
- json_obj=json.dumps(obj, ensure_ascii=False) #解决中文乱码
- print type(json_obj) #<type 'str'>
- print json_obj #{"name": "ljq", "email": "ljq@gmail.com"}
- #字符串转为json
- str="{'name':'李四', 'email':'lisi@gmail.com'}"
- #json_str=json.dumps(str)
- json_str=json.dumps(str, ensure_ascii=False)
- print json_str #"{'name':'张安', 'email':'ljq@gmail.com'}"
- #json转为字典
- dict_obj=json.loads(json_obj)
- #如果传入的对象的编码不一致的话,需要用encoding指定字符编码
- #dict_obj=json.loads(json_obj, encoding="utf8")
- print type(dict_obj) #<type 'dict'>
- print dict_obj.get('name','')
- #json转为字符串
- unicode_str=json.loads(json_str)
- dict_str=eval(unicode_str)
- print type(unicode_str) #<type 'unicode'>
- print type(dict_str)
- print dict_str.get('name', '')
- ##################json数组##############################
- arrays=[
- {'name':'zhangsan', 'age':10},
- {'name':'lisi', 'age':20},
- {'name':'wangwu', 'age':30}
- ]
- arrays_json=json.dumps(arrays)
- print type(arrays_json) #<type 'str'>
- for obj in json.loads(arrays_json):
- #print type(obj) #<type 'dict'>
- print obj.get('age', '')
- ##################django内置json---simplejson##############################
- '''
- import simplejson as json
- #几个主要函数:dump、dumps、load、loads,带s跟不带s的区别: 带s的是对 字符串的处理,而不带 s的是对文件对像的处理。
- json、simplejson效率比较:simplejson在效率上来得有优势,推荐用simplejson
- '''
python json的更多相关文章
- python json基础学习01
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import json #全称(javascript object ...
- python json数据的转换
1 Python数据转json字符串 import json json_str = json.dumps(py_data) 参数解析: json_str = json.dumps(py_data,s ...
- python大法好——python json
Python JSON 本章节我们将为大家介绍如何使用 Python 语言来编码和解码 JSON 对象. JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式, ...
- Python json 读取 json 文件并转为 dict
Python json 读取 json 文件并转为 dict 在 D 盘 新建 test.json: { "test": "测试\n换行", "dic ...
- Python Json & Pickle模块
用于序列化的两个模块 Json,用于字符串 和 python数据类型间进行转换 Pickle,用于python特有的类型 和 python的数据类型间进行转换 Json模块提供了四个功能:dumps. ...
- Python: json模块实例详解
ref:https://www.jianshu.com/p/e29611244810 https://www.cnblogs.com/qq78292959/p/3467937.html https:/ ...
- python学习笔记——python JSON
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写. 1.JSON 函数 使用 JSON 函数需要导入 json 库:import json ...
- Python json使用
转自:https://www.cnblogs.com/wangyayun/p/6699184.html?utm_source=tuicool&utm_medium=referral 使用Pyt ...
- Python -- Json 数据编码及解析
Python -- Json 数据编码及解析 Json 简单介绍 JSON: JavaScript Object Notation(JavaScript 对象表示法) JSON 是存储和交换文本 ...
- python json.dumps() 中文乱码问题
python json.dumps() 中文乱码问题 python 输出一串中文字符,在控制台上(控制台使用UTF-8编码)通过print 可以正常显示,但是写入到文件中之后,中文字符都输出成as ...
随机推荐
- Guava学习笔记:Ordering犀利的比较器
Ordering是Guava类库提供的一个犀利强大的比较器工具,Guava的Ordering和JDK Comparator相比功能更强.它非常容易扩展,可以轻松构造复杂的comparator,然后用在 ...
- Ampersand.js - 模块化的 JS 应用程序开发框架
Ampersand.js 是一个高度模块化,松耦合,用于构建先进的 JavaScript 应用程序的框架.通过良好定义的方法,结合了一系列微小的 CommonJS 模块.条理清晰,没有多余的冗余代码. ...
- go语言 新手学习笔记 go基础教程
目前这方面的资料相对较少,自己手动整理汇集. 第一章:安装 第一节:下载go语言 第二节:windows 安装 go语言 第三节: 第二章:基本语法 第一节:类型 .
- easyui日期在未加载easyui-lang-zh_CN.js出现英文的情况下加载中文的方法
我们有时候在操作easyui的时候本来是加载了easyui-lang-zh_CN.js中文文件包,但是还是出现了英文.使得我们不得埋怨这框架咋这么不好用,其实我们仔细看看这个中文包就会发现里面很多都是 ...
- js资源加载优化
互联网应用或者访问量大的应用,对js的加载优化是不可少的.下面记录几种优化方法 CDN + 浏览器缓存 CDN(content delivery network)内容分发网络, 最传统的优化方式.其 ...
- crm2013关于contentIFrame不能使用
在CRM2011里面,我们可以在页面的控制台里面输入: contentIFrame.Xrm.Page.data.entity.getEntityName(); contentIFrame.Xrm.Pa ...
- 劳动节脑洞大开!利用Debug API 获取 加壳客户端的MD5值
系统 : Windows xp 程序 : 某游戏客户端 程序下载地址 :不提供 要求 : 远程注入 & 获取MD5值 使用工具 : vc++6.0 & OD 案例说明: 该游戏客户端对 ...
- DevExpress v15.2.4帮助文档下载(全)
原文地址:http://www.devexpresscn.com/Resources/Documentation-498.html DevExpress v15.2帮助文档下载列表大全来啦!包含.ne ...
- CoreDataStack
- iOS开发中常用的单例
定义:一个类的对象,无论在何时创建.无论创建多少次,创建出来的对象都是同一个对象. 使用场景:当有一些数据需要共享给别的类的时候,就可以把这些数据保存在单例对象中. 关键代码: + (instan ...