原因:simpleJson把utf-8编码的字符串直接转成了unicode,但却是按字节来转的,不是真正的unicode
下面是转化为unicode的代码
from django.http import HttpResponse from django.utils import simplejson from django.conf import settings def json(data): encode = settings.DEFAULT_CHARSET return HttpResponse(simplejson.dumps(uni_str(data, encode))) def uni_str(a, encoding): if isinstance(a, (list, tuple)): s = [] for i, k in enumerate(a): s.append(uni_str(k, encoding)) return s elif isinstance(a, dict): s = {} for i, k in enumerate(a.items()): key, value = k s[uni_str(key, encoding)] = uni_str(value, encoding) return s elif isinstance(a, str) or (hasattr(a, ‘__str__’) and callable(getattr(a, ‘__str__’))): if getattr(a, ‘__str__’): a = str(a) return unicode(a, encoding) elif isinstance(a, unicode): return a else: return a
引用:http://blog.donews.com/limodou/archive/2006/09/14/1037959.aspx
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:django的json返回值带有汉字的处理 - Python技术站