期望用双引号括起来的属性名称:尝试加载python字典时出现第1行第2列(char 1)错误。我的数据格式就像
{u'start_time': '1900-01-01T10:00:00Z', u'TU': {u'start_time': '1900-01-01T10:00:00Z'}, u'MO': {u'start_time': '1900-01-01T10:00:00Z'}}
使用json.dumps(days_dict)后的json字符串
当我打印type(json.dumps(days_dict))
时,它会打印<type 'unicode'>
{u'start_time': '1900-01-01T10:00:00Z', u'TU': {u'start_time': '1900-01-01T10:00:00Z'}, u'MO': {u'start_time': '1900-01-01T10:00:00Z'}}
我正在尝试将它与solr一起使用。我从django模型获取对象,并根据该模型将值分配给字典。我准备这本词典的代码是
all_schedules=obj.scheduler.all()
days_dict={}
start_time = all_schedules.aggregate(Min("start_time"))['start_time__min']
if start_time:
days_dict["start_time"] = start_time.strftime('%Y-%m-%dT%H:%M:%SZ')
for schedule in all_schedules:
if schedule.start_time:
days_dict[schedule.day]={"start_time":schedule.start_time.strftime('%Y-%m-%dT%H:%M:%SZ')}
return json.dumps(days_dict)
但是当我尝试在这个dict对象上使用json.loads时,它会给出上面给出的错误。
这是我原来的方法,我从一个函数返回json 我在这里使用django haystack,这是我的字段和方法
time_start = indexes.CharField()
def prepare_time_start(self, obj):
all_schedules=obj.scheduler.all()
days_dict={}
start_time = all_schedules.aggregate(Min("start_time"))['start_time__min']
if start_time:
days_dict["start_time"] = start_time.strftime('%Y-%m-%dT%H:%M:%SZ')
if len(all_schedules):
for schedule in all_schedules:
if schedule.start_time:
days_dict[schedule.day]={"start_time":schedule.start_time.strftime('%Y-%m-%dT%H:%M:%SZ')}
print json.dumps(days_dict)
return json.dumps(days_dict)