之前寫的很簡單,只有幾句話,最近發現本文是本博客閱讀量最大的一篇文章,以爲這樣有種把人騙進來的感受,因而又細化了一些。若是還有很差的地方,歡迎指出。python
首先說明基本功能:json
dumps是將dict轉化成str格式,loads是將str轉化成dict格式。post
dump和load也是相似的功能,只是與文件操做結合起來了。spa
看代碼實例:指針
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
In [
1
]:
import
json
In [
2
]: a
=
{
'name'
:
'wang'
,
'age'
:
29
}
In [
3
]: b
=
json.dumps(a)
In [
4
]:
print
b,
type
(b)
{
"age"
:
29
,
"name"
:
"wang"
} <
type
'str'
>
In [
11
]: json.loads(b)
Out[
11
]: {u
'age'
:
29
, u
'name'
: u
'wang'
}
In [
12
]:
print
type
(json.loads(b))
<
type
'dict'
>
|
而後再看dump和dumps的區別,見代碼:code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
In [
1
]:
import
json
In [
2
]: a
=
{
'name'
:
'wang'
,
'age'
:
29
}
In [
3
]: b
=
json.dumps(a)
In [
4
]:
print
b,
type
(b)
{
"age"
:
29
,
"name"
:
"wang"
} <
type
'str'
>
In [
5
]: c
=
json.dump(a)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
TypeError Traceback (most recent call last)
<ipython
-
input
-
5
-
92dc0d929363
>
in
<module>()
-
-
-
-
>
1
c
=
json.dump(a)
TypeError: dump() takes at least
2
arguments (
1
given)
|
這裏提示咱們少一個參數,咱們看一下幫助文件(iPyhton中能夠直接使用help(json.dumps)來查看幫助文件):orm
dumps(obj, 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)
Serialize ``obj`` to a JSON formatted ``str``.對象
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)
Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
``.write()``-supporting file-like object).blog
簡單說就是dump須要一個相似於文件指針的參數(並非真的指針,可稱之爲類文件對象),能夠與文件操做結合,也就是說能夠將dict轉成str而後存入文件中;而dumps直接給的是str,也就是將字典轉成str。ip
例子見代碼(注意文件操做的一些小細節):
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
|
In [
1
]:
import
json
In [
2
]: a
=
{
'name'
:
'wang'
}
In [
3
]: fp
=
file
(
'test.txt'
,
'w'
)
In [
4
]:
type
(fp)
Out[
4
]:
file
In [
5
]: json.dump(a, fp)
In [
6
]: cat test.txt
In [
7
]: fp.close()
In [
8
]: cat test.txt
{
"name"
:
"wang"
}
In [
9
]: json.load(fp)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
ValueError Traceback (most recent call last)
<ipython
-
input
-
9
-
0064dabedb17
>
in
<module>()
-
-
-
-
>
1
json.load(fp)
/
usr
/
local
/
Cellar
/
python
/
2.7
.
11
/
Frameworks
/
Python.framework
/
Versions
/
2.7
/
lib
/
python2.
7
/
json
/
__init__.pyc
in
load(fp, encoding,
cls
, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook,
*
*
kw)
285
286
"""
-
-
>
287
return
loads(fp.read(),
288
encoding
=
encoding,
cls
=
cls
, object_hook
=
object_hook,
289
parse_float
=
parse_float, parse_int
=
parse_int,
ValueError: I
/
O operation on closed
file
In [
10
]: fp
=
file
(
'test.txt'
,
'r'
)
In [
11
]: json.load(fp)
Out[
11
]: {u
'name'
: u
'wang'
}
|
注:實際中dump用的較少。