1. #-*-coding:utf-8-*-
  2.  
  3. '''编码格式记得统一,不然容易出现中文乱码,推荐用utf-8'''
  4.  
  5. import json
  6.  
  7. ##################json单对象##############################
  8. #声明初始化一个变量
  9. obj={'name':'张三', 'email':'ljq@gmail.com'}
  10. print type(obj) #<type 'dict'>
  11.  
  12. #字典转为json
  13. #json_obj=json.dumps(obj)
  14. json_obj=json.dumps(obj, ensure_ascii=False) #解决中文乱码
  15. print type(json_obj) #<type 'str'>
  16. print json_obj #{"name": "ljq", "email": "ljq@gmail.com"}
  17.  
  18. #字符串转为json
  19. str="{'name':'李四', 'email':'lisi@gmail.com'}"
  20. #json_str=json.dumps(str)
  21. json_str=json.dumps(str, ensure_ascii=False)
  22. print json_str #"{'name':'张安', 'email':'ljq@gmail.com'}"
  23.  
  24. #json转为字典
  25. dict_obj=json.loads(json_obj)
  26. #如果传入的对象的编码不一致的话,需要用encoding指定字符编码
  27. #dict_obj=json.loads(json_obj, encoding="utf8")
  28. print type(dict_obj) #<type 'dict'>
  29. print dict_obj.get('name','')
  30.  
  31. #json转为字符串
  32. unicode_str=json.loads(json_str)
  33. dict_str=eval(unicode_str)
  34. print type(unicode_str) #<type 'unicode'>
  35. print type(dict_str)
  36. print dict_str.get('name', '')
  37.  
  38. ##################json数组##############################
  39. arrays=[
  40. {'name':'zhangsan', 'age':10},
  41. {'name':'lisi', 'age':20},
  42. {'name':'wangwu', 'age':30}
  43. ]
  44.  
  45. arrays_json=json.dumps(arrays)
  46. print type(arrays_json) #<type 'str'>
  47.  
  48. for obj in json.loads(arrays_json):
  49. #print type(obj) #<type 'dict'>
  50. print obj.get('age', '')
  51.  
  52. ##################django内置json---simplejson##############################
  53. '''
  54. import simplejson as json
  55.  
  56. #几个主要函数:dump、dumps、load、loads,带s跟不带s的区别: 带s的是对 字符串的处理,而不带 s的是对文件对像的处理。
  57.  
  58. json、simplejson效率比较:simplejson在效率上来得有优势,推荐用simplejson
  59.  
  60. '''

python json的更多相关文章

  1. python json基础学习01

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import json #全称(javascript object  ...

  2. python json数据的转换

    1  Python数据转json字符串 import json json_str = json.dumps(py_data) 参数解析: json_str = json.dumps(py_data,s ...

  3. python大法好——python json

    Python JSON 本章节我们将为大家介绍如何使用 Python 语言来编码和解码 JSON 对象. JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式, ...

  4. Python json 读取 json 文件并转为 dict

    Python json 读取 json 文件并转为 dict 在 D 盘 新建 test.json: { "test": "测试\n换行", "dic ...

  5. Python Json & Pickle模块

    用于序列化的两个模块 Json,用于字符串 和 python数据类型间进行转换 Pickle,用于python特有的类型 和 python的数据类型间进行转换 Json模块提供了四个功能:dumps. ...

  6. Python: json模块实例详解

    ref:https://www.jianshu.com/p/e29611244810 https://www.cnblogs.com/qq78292959/p/3467937.html https:/ ...

  7. python学习笔记——python JSON

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写. 1.JSON 函数 使用 JSON 函数需要导入 json 库:import json ...

  8. Python json使用

    转自:https://www.cnblogs.com/wangyayun/p/6699184.html?utm_source=tuicool&utm_medium=referral 使用Pyt ...

  9. Python -- Json 数据编码及解析

    Python  --  Json  数据编码及解析 Json 简单介绍 JSON: JavaScript Object Notation(JavaScript 对象表示法) JSON 是存储和交换文本 ...

  10. python json.dumps() 中文乱码问题

    python json.dumps() 中文乱码问题   python 输出一串中文字符,在控制台上(控制台使用UTF-8编码)通过print 可以正常显示,但是写入到文件中之后,中文字符都输出成as ...

随机推荐

  1. Guava学习笔记:Ordering犀利的比较器

    Ordering是Guava类库提供的一个犀利强大的比较器工具,Guava的Ordering和JDK Comparator相比功能更强.它非常容易扩展,可以轻松构造复杂的comparator,然后用在 ...

  2. Ampersand.js - 模块化的 JS 应用程序开发框架

    Ampersand.js 是一个高度模块化,松耦合,用于构建先进的 JavaScript 应用程序的框架.通过良好定义的方法,结合了一系列微小的 CommonJS 模块.条理清晰,没有多余的冗余代码. ...

  3. go语言 新手学习笔记 go基础教程

    目前这方面的资料相对较少,自己手动整理汇集. 第一章:安装 第一节:下载go语言 第二节:windows 安装 go语言 第三节: 第二章:基本语法 第一节:类型 .

  4. easyui日期在未加载easyui-lang-zh_CN.js出现英文的情况下加载中文的方法

    我们有时候在操作easyui的时候本来是加载了easyui-lang-zh_CN.js中文文件包,但是还是出现了英文.使得我们不得埋怨这框架咋这么不好用,其实我们仔细看看这个中文包就会发现里面很多都是 ...

  5. js资源加载优化

    互联网应用或者访问量大的应用,对js的加载优化是不可少的.下面记录几种优化方法 CDN  + 浏览器缓存 CDN(content delivery network)内容分发网络, 最传统的优化方式.其 ...

  6. crm2013关于contentIFrame不能使用

    在CRM2011里面,我们可以在页面的控制台里面输入: contentIFrame.Xrm.Page.data.entity.getEntityName(); contentIFrame.Xrm.Pa ...

  7. 劳动节脑洞大开!利用Debug API 获取 加壳客户端的MD5值

    系统 : Windows xp 程序 : 某游戏客户端 程序下载地址 :不提供 要求 : 远程注入 & 获取MD5值 使用工具 : vc++6.0 & OD 案例说明: 该游戏客户端对 ...

  8. DevExpress v15.2.4帮助文档下载(全)

    原文地址:http://www.devexpresscn.com/Resources/Documentation-498.html DevExpress v15.2帮助文档下载列表大全来啦!包含.ne ...

  9. CoreDataStack

  10. iOS开发中常用的单例

    定义:一个类的对象,无论在何时创建.无论创建多少次,创建出来的对象都是同一个对象. 使用场景:当有一些数据需要共享给别的类的时候,就可以把这些数据保存在单例对象中.   关键代码: + (instan ...