python解析json对象和数组

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import json

data = {
    'name': 'test',
    'age': 100,
    'vip': True,
    'address': {'province': 'Jiangsu', 'city': 'Changzhou'}
}
# 将 Python 字典类型转换为 JSON 对象
json_str = json.dumps(data)
 
# 将 JSON 对象类型转换为 Python 字典
user_dic = json.loads(json_str)
print(user_dic['name']+"--"+str(user_dic['address']))
#print(user_dic['name'])
#print(user_dic['address'])
print('')
address = user_dic['address']
json_str = json.dumps(address)
user_dic = json.loads(json_str)
print(user_dic['province'])

print('')
data = [{'name': 'test','age': 100,'vip': True,'address': {'province': 'Jiangsu', 'city': 'Changzhou'}}]
print(data[0])
json_str = json.dumps(data[0])
# 将 JSON 对象类型转换为 Python 字典
user_dic = json.loads(json_str)
print('')
print(user_dic['name']+"--"+str(user_dic['address']))
#print(user_dic['address'])
print('')
address = user_dic['address']
json_str = json.dumps(address)
user_dic = json.loads(json_str)
print(user_dic['province'])

你可能感兴趣的:(python,json,python,后端)