JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于JavaScript的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成。
JSON 语法是 JavaScript 对象表示法语法的子集。
JSON 数据的书写格式是:名称:值对。
名称/值对包括字段名称(在双引号中),后面写一个冒号,然后是值:
“firstName” : “John”
这很容易理解,等价于这条 JavaScript 语句:
firstName = “John”
JSON 值可以是:
[
{
"asset_type": "路由器",
"production_state": "库存",
"asset_owner_type": null,
"name": "lhx0055",
"asset_sn": "lhx00722",
"dev_sn": "lhx00722",
"is_mount": true,
"buy_time": null,
},
{
"asset_type": "路由器",
"production_state": "库存",
"asset_owner_type": null,
"name": "lhx0056",
"asset_sn": "lhx007221",
"dev_sn": "lhx007222",
"is_mount": false,
"buy_time": null,
}
]
综上所述,在相同带宽和流量的情况下,同一时间内,能够为我们传输更多的数据,处理更多的逻辑的JSON才是现在的主流配置,在互联网起步阶段,XML曾经辉煌过,但这已经是一种过去式,就像诺基亚手机一样,慢慢的淹没在历史长河中。
方法 | 实现功能 |
---|---|
json.dumps | 将python对象序列化为JSON字符串 |
json.loads | 将JSON字符串反序列化为python对象 |
json.dump | 将python对象序列化到文件中 |
json.load | 将文件流中的JSON字符串反序列化为python对象 |
序列化时把ensure_ascii
设置成False
就可以了。
import json
class Student(object):
def __init__(self, name, age, score):
self.name = name
self.age = age
self.score = score
def student2dict(std): # 对象转字典,不是类内部方法
return {
'name': std.name,
'age': std.age,
'score': std.score
}
def dict2student(d): # 字典转对象,不是类内部方法
return Student(d['name'], d['age'], d['score'])
# 自定义对象进行JSON序列化
print(json.dumps(s, default=student2dict))
# JSON反序列化回自定义对象
json_str = '{"age": 20, "score": 88, "name": "Bob"}'
print(json.loads(json_str, object_hook=dict2student),ensure_ascii=False)
import json
class Student(object):
def __init__(self, name, age, score):
self.name = name
self.age = age
self.score = score
s = Student(name='lisi',age = 18, score = 99.8)
print(json.dumps(s, default=lambda obj: obj.__dict__))
# {"name": "lisi", "age": 18, "score": 99.8}
import json
from datetime import datetime,date
import uuid
class CJsonEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(obj, date):
return obj.strftime('%Y-%m-%d')
elif: isinstance(obj,uuid.UUID):
return str(obj)
else:
return super().default(obj)
l = ['12',date.today(),datetime.today(),uuid.uuid4()]
print(json.dumps(l, cls=CJsonEncoder))
# ["12", "2020-11-25", "2020-11-25 16:40:42", "6c809fcf-baad-4236-81f2-f01200314ea9"]
import json
from datetime import datetime,date
class Foo(object):
def __init__(self,name,age,birth):
self.name = name
self.age = age
self.birth =birth
def obj2dict(self,obj):
return {
'name':obj.name,
'age':obj.age,
'birth':obj.birth.strftime('%Y-%m-%d')
}
f = Foo('xx',18,date.today())
json.dumps(f, default=f.obj2dict)
# {"name": "xx", "age": 18, "birth": "2020-11-25"}
import uuid
import json
from datetime import datetime, date
class CJsonEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(obj, date):
return obj.strftime('%Y-%m-%d')
elif isinstance(obj, uuid.UUID):
return str(obj)
else:
return super().default(obj)
class JSONHelper(object):
@staticmethod
def obj_to_json(obj):
return json.dumps(obj, ensure_ascii=False, default=None, cls=CJsonEncoder)
@staticmethod
def json_to_obj(json_str, object_hook=None):
return json.loads(json_str, object_hook=object_hook)
@staticmethod
def obj_to_json_file(obj, file_path):
with open(file_path, 'w') as fp:
return json.dump(obj, fp, ensure_ascii=False, default=None, cls=CJsonEncoder)
@staticmethod
def json_file_to_obj(file_path, object_hook=None, encoding='utf8'):
with open(file_path, 'rb') as fp:
return json.load(fp, object_hook=object_hook, encoding=encoding)
# 对象转化为json字符串
obj = [{'user_info': {'name': 'zhangsan', 'age': 18, 'gender': '男', 'is_admin': True, 'date': date.today()}},
{'connect': {'host': '127.0.0.1', 'user': 'db', 'password': 123456, 'port': 1521, 'db': 'zet'}}]
json_str = JSONHelper.obj_to_json(obj)
# 将对象转化为JSON字符串写入文件
save_path = 'obj.json'
JSONHelper.obj_to_json_file(obj, save_path)
print(type(json_str)) # <class 'list'>
print(json_str)
# [{"user_info": {"name": "zhangsan", "age": 18, "gender": "男", "is_admin": true, "date": "2020-11-25"}},
# {"connect": {"host": "127.0.0.1", "user": "db", "password": 123456, "port": 1521, "db": "zet"}}]
# json字符串转对象
str1 = """
[{"user_info": {"name": "zhangsan", "age": 18, "gender": "男", "is_admin": true}}]
"""
obj1 = JSONHelper.json_to_obj(str1)
print(type(obj1)) # <class 'list'>
print(obj1)
# [{'user_info': {'name': 'zhangsan', 'age': 18, 'gender': '男', 'is_admin': True}}]
# 读取接送文件到对象
file_path = 'data.json'
obj2 = JSONHelper.json_file_to_obj(file_path)
print(type(obj2))
print(obj2)