码迷,mamicode.com
首页 > 编程语言 > 详细

【Python进阶】03、json

时间:2016-11-09 23:16:23      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:【python进阶】03、json

     本节学习如何使用 Python 语言来编码和解码 JSON 对象。

这里的编码不是指字符编码,而是指把python对象转化为能在网络上或其它介质上传输的文本信息。

一、json简介

JSON: JavaScript Object Notation(JavaScript 对象表示法)

JSON 是存储和交换文本信息的语法。类似 XML。

JSON 比 XML 更小、更快,更易解析。

       JSON 使用 Javascript语法来描述数据对象,但是 JSON 仍然独立于语言和平台。JSON 解析器和 JSON 库支持许多不同的编程语言。 目前非常多的动态(PHP,JSP,.NET)编程语言都支持JSON。


JSON 实例

{
"sites": [
{ "name":"菜鸟教程" , "url":"www.runoob.com" }, 
{ "name":"google" , "url":"www.google.com" }, 
{ "name":"微博" , "url":"www.weibo.com" }]}

这个 sites 对象是包含 3 个站点记录(对象)的数组。

类似Python的dict


json语法规则:

  • 数据在名称/值对中

  • 数据由逗号分隔

  • 花括号保存对象

  • 方括号保存数组


二、json模块

In [4]: import json

In [5]: json.
json.JSONDecoder  json.dump         json.load         
json.JSONEncoder  json.dumps        json.loads        
json.decoder      json.encoder      json.scanner

encode将 Python 对象编码成 JSON 字符串
decode

将已编码的 JSON 字符串解码为 Python 对

python与json支持以下几种结构转化:

|  Supports the following objects and types by default:
     |  
     |  +-------------------+---------------+
     |  | Python            | JSON          |
     |  +===================+===============+
     |  | dict              | object        |
     |  +-------------------+---------------+
     |  | list, tuple       | array         |
     |  +-------------------+---------------+
     |  | str, unicode      | string        |
     |  +-------------------+---------------+
     |  | int, long, float  | number        |
     |  +-------------------+---------------+
     |  | True              | true          |
     |  +-------------------+---------------+
     |  | False             | false         |
     |  +-------------------+---------------+
     |  | None              | null          |
     |  +-------------------+---------------+

案例:有一个保存用户信息的json文件,需要解析json为User类。

User = nametuple(‘User‘, [‘name‘, ‘age‘, ‘gender‘, ‘phone‘, ‘mail‘])

[root@Node3 src]# cat text.json
{
"users": [
    {
        "name": "xj",
        "age": 25,
        "gender": "man",
        "phone": "15711112222",
        "mail": "xj@magedu.com"
    }]
}

解决方案:

      解析json为字典,再处理字典

      使用object_hook

In [3]: ret = json.load(open("/tmp/src/text.json"))

In [4]: ret
Out[4]: 
{u‘users‘: [{u‘age‘: 25,
   u‘gender‘: u‘man‘,
   u‘mail‘: u‘xj@magedu.com‘,
   u‘name‘: u‘xj‘,
   u‘phone‘: u‘15711112222‘}]}

In [5]: type(ret)
Out[5]: dict

In [6]: ret.
ret.clear       ret.items       ret.pop         ret.viewitems
ret.copy        ret.iteritems   ret.popitem     ret.viewkeys
ret.fromkeys    ret.iterkeys    ret.setdefault  ret.viewvalues
ret.get         ret.itervalues  ret.update      
ret.has_key     ret.keys        ret.values    

In [7]: ret[‘users‘]
Out[7]: 
[{u‘age‘: 25,
  u‘gender‘: u‘man‘,
  u‘mail‘: u‘xj@magedu.com‘,
  u‘name‘: u‘xj‘,
  u‘phone‘: u‘15711112222‘}]
  
  
In [16]: type(ret[‘users‘])
Out[16]: list

In [17]: ret[‘users‘][0]
Out[17]: 
{u‘age‘: 25,
 u‘gender‘: u‘man‘,
 u‘mail‘: u‘xj@magedu.com‘,
 u‘name‘: u‘xj‘,
 u‘phone‘: u‘15711112222‘}
 
In [19]: ret[‘users‘][0]["age"]
Out[19]: 25


【Python进阶】03、json

标签:【python进阶】03、json

原文地址:http://xiexiaojun.blog.51cto.com/2305291/1871198

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!