扩展JSON

 1 from django.shortcuts import render
 2 # Create your views here.
 3 # json扩展:支持时间序列化
 4 import json
 5 from datetime import datetime
 6 from datetime import date
 7 
 8 class JsonCustomEncoder(json.JSONEncoder):
 9     def default(self, value):
10         if isinstance(value, datetime):
11             return value.strftime('%Y-%m-%d %H:%M:%S')
12         elif isinstance(value, date):
13             return value.strftime('%Y-%m-%d')
14         else:
15             return json.JSONEncoder.default(self, value)
16 
17 v = models.Server.objects.values('id', 'hostname', 'create_time')
18 data = json.dumps(list(v), cls=JsonCustomEncoder)

 

posted @ 2018-04-25 11:59  生如夏花和秋叶  阅读(268)  评论(0编辑  收藏  举报