Python version : 3.4.4


测试数据:

import json
aa = {"name": "kk", "age": 99 }
bb = [ {"name": "mm", "age": 10},{"name": "qq", "age": 20},{"name": "hh", "age": 50} ]


json模块提供的函数 json.dumps() 和 json.loads()


输出:

>>> print(bb)
[{'name': 'mm', 'age': 10}, {'name': 'qq', 'age': 20}, {'name': 'hh', 'age': 50}]
>>>
>>> data=json.dumps(bb)
>>> print(data)
[{"name": "mm", "age": 10}, {"name": "qq", "age": 20}, {"name": "hh", "age": 50}]
>>>
>>> load = json.loads(data)
>>> print(load)
[{'name': 'mm', 'age': 10}, {'name': 'qq', 'age': 20}, {'name': 'hh', 'age': 50}]
>>>


排序:

>>> data = json.dumps(bb,sort_keys=True)
>>> print(data)
[{"age": 10, "name": "mm"}, {"age": 20, "name": "qq"}, {"age": 50, "name": "hh"}]
>>>


格式化输出:

>>> data = json.dumps(bb,indent=4)
>>> print(data)
[
{
"name": "mm",
"age": 10
},
{
"name": "qq",
"age": 20
},
{
"name": "hh",
"age": 50
}
]
>>>


输出某行数据:

>>> print(bb[0])
{'name': 'mm', 'age': 10}
>>>
>>> print(bb[1])
{'name': 'qq', 'age': 20}
>>>
>>> print(bb[2])
{'name': 'hh', 'age': 50}
>>>


输出某行数据某个值:

>>> print(bb[1]["name"])
qq
>>>
>>> print(bb[1]["age"])
20
>>>



遍历/循环 JSON :

>>> bb = [ {"name": "mm", "age": 10},{"name": "qq", "age": 20},{"name": "hh", "age": 50} ]
>>>
>>> for item in bb:
... print(item,item["name"])
...
{'name': 'mm', 'age': 10} mm
{'name': 'qq', 'age': 20} qq
{'name': 'hh', 'age': 50} hh
>>>
>>> for i in range(len(bb)):
... print(i,bb[i],bb[i]["name"])
...
0 {'name': 'mm', 'age': 10} mm
1 {'name': 'qq', 'age': 20} qq
2 {'name': 'hh', 'age': 50} hh
>>>
>>> for i,item in enumerate(bb):
... print(i,item,item["name"])
...
0 {'name': 'mm', 'age': 10} mm
1 {'name': 'qq', 'age': 20} qq
2 {'name': 'hh', 'age': 50} hh
>>>
>>> for item in bb:
... print("name=%s,age=%s,age=%f"%(item["name"],item["age"],item["age"]))
...
name=mm,age=10,age=10.000000
name=qq,age=20,age=20.000000
name=hh,age=50,age=50.000000
>>>
>>> for item in bb:
... print("My name is {0} ".format(item["name"]))
...
My name is mm
My name is qq
My name is hh
>>>
>>> for i,j in bb:
... print("name: {0}, age: {1}".format(i,j))
...
name: name, age: age
name: name, age: age
name: name, age: age
>>>


json 转 python 数据格式定义:

+---------------+-------------------+
| JSON | Python |
+===============+===================+
| object | dict |
+---------------+-------------------+
| array | list |
+---------------+-------------------+
| string | str |
+---------------+-------------------+
| number (int) | int |
+---------------+-------------------+
| number (real) | float |
+---------------+-------------------+
| true | True |
+---------------+-------------------+
| false | False |
+---------------+-------------------+
| null | None |
+---------------+-------------------+


python 转 json 数据格式定义:

+-------------------+---------------+
| Python | JSON |
+===================+===============+
| dict | object |
+-------------------+---------------+
| list, tuple | array |
+-------------------+---------------+
| str | string |
+-------------------+---------------+
| int, float | number |
+-------------------+---------------+
| True | true |
+-------------------+---------------+
| False | false |
+-------------------+---------------+
| None | null |
+-------------------+---------------+



更多参考:JSON encoder and decoder

help("json")