Home Backend Development Python Tutorial 简单介绍Python中的JSON模块

简单介绍Python中的JSON模块

Jun 06, 2016 am 11:24 AM
json python

(一)什么是json:

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。

JSON建构于两种结构:

“名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。

值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。

这些都是常见的数据结构。事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。

(二)Python JSON模块

Python2.6开始加入了JSON模块,无需另外下载,Python的Json模块序列化与反序列化的过程分别是 encoding和 decoding。encoding-把一个Python对象编码转换成Json字符串;decoding-把Json格式字符串解码转换成Python对象。要使用json模块必须先导入:

1

import json

Copy after login

1,简单数据类型的处理

Python JSON模块可以直接处理简单数据类型(string、unicode、int、float、list、tuple、dict)。 json.dumps()方法返回一个str对象,编码过程中会存在从python原始类型向json类型的转化过程,具体的转化对照如下:

20154892843670.png (244×200)

json.dumps方法提供了很多好用的参数可供选择,比较常用的有sort_keys(对dict对象进行排序,我们知道默认dict是无序存放的)、separators,indent等参数,dumps方法的定义为:

1

json.dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True,cls=None, indent=None, separators=None, encoding="utf-8", default=None, sort_keys=False,**kw)

Copy after login

使用简单的json.dumps方法对简单数据类型进行编码,例如:

1

2

3

4

5

6

obj = [[1,2,3],123,123.123,'abc',{'key1':(1,2,3),'key2':(4,5,6)}]

encodedjson = json.dumps(obj)

print 'the original list:\n',obj

print 'length of obj is:',len(repr(obj))

print 'repr(obj),replace whiteblank with *:\n', repr(obj).replace(' ','*')

print 'json encoded,replace whiteblank with *:\n',encodedjson.replace(' ','*')

Copy after login

输出:(Python默认的item separator是‘, '(不是','),所以list无论是转化成字符串还是json格式,成员之间都是有空格隔开的)

1

2

3

4

5

6

7

8

the original list:

[[1, 2, 3], 123, 123.123, 'abc', {'key2': (4, 5, 6), 'key1': (1, 2, 3)}]

length of obj is: 72

repr(obj),replace whiteblank with *:

[[1,*2,*3],*123,*123.123,*'abc',*{'key2':*(4,*5,*6),*'key1':*(1,*2,*3)}]

json encoded,replace whiteblank with *:

[[1,*2,*3],*123,*123.123,*"abc",*{"key2":*[4,*5,*6],*"key1":*[1,*2,*3]}]

<type 'list'>

Copy after login

我们接下来在对encodedjson进行decode,得到原始数据,需要使用的json.loads()函数。loads方法返回了原始的对象,但是仍然发生了一些数据类型的转化,上例中‘abc'转化为了unicode类型。需要注意的是,json字符串中的字典类型的key必须要用双引号“”json.loads()才能正常解析。从json到python的类型转化对照如下:

20154892947143.png (244×213)

1

2

3

4

decodejson = json.loads(encodedjson)

print 'the type of decodeed obj from json:', type(decodejson)

print 'the obj is:\n',decodejson

print 'length of decoded obj is:',len(repr(decodejson))

Copy after login

输出:

1

2

3

4

the type of decodeed obj from json: <type 'list'>

the obj is:

[[1, 2, 3], 123, 123.123, u'abc', {u'key2': [4, 5, 6], u'key1': [1, 2, 3]}]

length of decoded obj is: 75 #比原obj多出了3个unicode编码标示‘u'

Copy after login

sort_keys排序功能使得存储的数据更加有利于观察,也使得对json输出的对象进行比较。下例中,data1和data2数据应该是一样的,dict存储的无序性造成两者无法比较。

1

2

3

4

5

6

7

8

9

10

data1 = {'b':789,'c':456,'a':123}

data2 = {'a':123,'b':789,'c':456}

d1 = json.dumps(data1,sort_keys=True)

d2 = json.dumps(data2)

d3 = json.dumps(data2,sort_keys=True)

print 'sorted data1(d1):',d1

print 'unsorted data2(d2):',d2

print 'sorted data2(d3):',d3

print 'd1==d2&#63;:',d1==d2

print 'd1==d3&#63;:',d1==d3

Copy after login

输出:

1

2

3

4

5

sorted data1(d1): {"a": 123, "b": 789, "c": 456}

unsorted data2(d2): {"a": 123, "c": 456, "b": 789}

sorted data2(d3): {"a": 123, "b": 789, "c": 456}

d1==d2&#63;: False

d1==d3&#63;: True

Copy after login

indent参数是缩进的意思,它可以使数据的存储格式更优雅、可读性更强,这是通过增加一些冗余的空格进行填充的。但是在解码(json.loads())时,空白填充会被删除。

1

2

3

4

5

6

7

data = {'b':789,'c':456,'a':123}

d1 = json.dumps(data,sort_keys=True,indent=4)

print 'data len is:',len(repr(data))

print '4 indented data:\n',d1

d2 = json.loads(d1)

print 'decoded DATA:', repr(d2)

print 'len of decoded DATA:',len(repr(d2))

Copy after login

输出:(可见loads时会将dumps时增加的intent 填充空格去除)

1

2

3

4

5

6

7

8

9

data len is: 30

4 indented data:

{

  "a": 123, 

  "b": 789, 

  "c": 456

}

decoded DATA: {u'a': 123, u'c': 456, u'b': 789}

len of decoded DATA: 33

Copy after login

json主要是作为一种数据通信的格式存在的,无用的空格会浪费通信带宽,适当时候也要对数据进行压缩。separator参数可以起到这样的作用,该参数传递是一个元组,包含分割对象的字符串,其实质就是将Python默认的(‘, ',': ')分隔符替换成(',',':')。

1

2

3

4

5

6

data = {'b':789,'c':456,'a':123}

print 'DATA:', repr(data)

print 'repr(data)       :', len(repr(data))

print 'dumps(data)      :', len(json.dumps(data))

print 'dumps(data, indent=2) :', len(json.dumps(data, indent=4))

print 'dumps(data, separators):', len(json.dumps(data, separators=(',',':')))

Copy after login

输出:

1

2

3

4

5

DATA: {'a': 123, 'c': 456, 'b': 789}

repr(data)       : 30

dumps(data)      : 30

dumps(data, indent=2) : 46

dumps(data, separators): 25

Copy after login


另一个比较有用的dumps参数是skipkeys,默认为False。 dumps方法存储dict对象时key必须是str类型,其他类型会导致TypeError异常产生,如果将skipkeys设为True则会优雅的滤除非法keys。

1

2

3

data = {'b':789,'c':456,(1,2):123}

print'original data:',repr(data)

print 'json encoded',json.dumps(data,skipkeys=True)

Copy after login

输出:

1

2

original data: {(1, 2): 123, 'c': 456, 'b': 789}

json encoded {"c": 456, "b": 789}

Copy after login

2,JSON处理自定义数据类型

json模块不仅可以处理普通的python内置类型,也可以处理我们自定义的数据类型,而往往处理自定义的对象是很常用的。

如果直接通过json.dumps方法对Person的实例进行处理的话,会报错,因为json无法支持这样的自动转化。通过上面所提到的json和 python的类型转化对照表,可以发现,object类型是和dict相关联的,所以我们需要把我们自定义的类型转化为dict,然后再进行处理。这里,有两种方法可以使用。

方法一:自己写转化函数

自定义object类型和dict类型进行转化:encode-定义函数 object2dict()将对象模块名、类名以及__dict__存储在一个字典并返回;decode-定义dict2object()解析出模块名、类名、参数,创建新的对象并返回。在json.dumps()中通过default参数指定转化过程中调用的函数;json.loads()则通过 object_hook指定转化函数。

方法二:继承JSONEncoder和JSONDecoder类,覆写相关方法

JSONEncoder类负责编码,主要是通过其default函数进行转化,我们可以重载该方法。对于JSONDecoder,亦然。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

#handling private data type

#define class

class Person(object):

  def __init__(self,name,age):

    self.name = name

    self.age = age

  def __repr__(self):

    return 'Person Object name : %s , age : %d' % (self.name,self.age)

     

     

#define transfer functions

def object2dict(obj):

  #convert object to a dict

  d = {'__class__':obj.__class__.__name__, '__module__':obj.__module__}

  d.update(obj.__dict__)

  return d

    

def dict2object(d):

  #convert dict to object

  if'__class__' in d:

    class_name = d.pop('__class__')

    module_name = d.pop('__module__')

    module = __import__(module_name)

    print 'the module is:', module

    class_ = getattr(module,class_name)

    args = dict((key.encode('ascii'), value) for key, value in d.items()) #get args

    print 'the atrribute:', repr(args)

    inst = class_(**args) #create new instance

  else:

    inst = d

  return inst

#recreate the default method

class LocalEncoder(json.JSONEncoder):

  def default(self,obj):

    #convert object to a dict

    d = {'__class__':obj.__class__.__name__, '__module__':obj.__module__}

    d.update(obj.__dict__)

    return d

    

class LocalDecoder(json.JSONDecoder):

  def __init__(self):

    json.JSONDecoder.__init__(self,object_hook = self.dict2object)

  def dict2object(self, d):

    #convert dict to object

    if'__class__' in d:

      class_name = d.pop('__class__')

      module_name = d.pop('__module__')

      module = __import__(module_name)

      class_ = getattr(module,class_name)

      args = dict((key.encode('ascii'), value) for key, value in d.items()) #get args

      inst = class_(**args) #create new instance

    else:

      inst = d

    return inst

#test function

if __name__ == '__main__':

  p = Person('Aidan',22)

  print p

  #json.dumps(p)#error will be throwed

  d = object2dict(p)

  print 'method-json encode:', d

    

  o = dict2object(d)

  print 'the decoded obj type: %s, obj:%s' % (type(o),repr(o))

    

  dump = json.dumps(p,default=object2dict)

  print 'dumps(default = object2dict):',dump

  load = json.loads(dump,object_hook = dict2object)

  print 'loads(object_hook = dict2object):',load

  d = LocalEncoder().encode(p)

  o = LocalDecoder().decode(d)

    

  print 'recereated encode method: ',d

  print 'recereated decode method: ',type(o),o

Copy after login

输出:

1

2

3

4

5

6

7

8

9

10

11

Person Object name : Aidan , age : 22

method-json encode: {'age': 22, '__module__': '__main__', '__class__': 'Person', 'name': 'Aidan'}

the module is: <module '__main__' from 'D:/Project/Python/study_json'>

the atrribute: {'age': 22, 'name': 'Aidan'}

the decoded obj type: <class '__main__.Person'>, obj:Person Object name : Aidan , age : 22

dumps(default = object2dict): {"age": 22, "__module__": "__main__", "__class__": "Person", "name": "Aidan"}

the module is: <module '__main__' from 'D:/Project/Python/study_json'>

the atrribute: {'age': 22, 'name': u'Aidan'}

loads(object_hook = dict2object): Person Object name : Aidan , age : 22

recereated encode method: {"age": 22, "__module__": "__main__", "__class__": "Person", "name": "Aidan"}

recereated decode method: <class '__main__.Person'> Person Object name : Aidan , age : 22

Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1671
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles