On 12/8/06, SP Z <[EMAIL PROTECTED]> wrote:
> The simplejson has some problem when you use chinese character in utf-8
>
> for example ,below chinese dictionary
>
> dict={"adviser":"n.顾问, <美> (学生的)指导老师","od":"dd"}
>
> show be encoded as ,
>
> {"adviser": "n.\u987e\u95ee, <\u7f8e>
> (\u5b66\u751f\u7684)\u6307\u5bfc\u8001\u5e08", "od": "dd"}
>
>
> but the simplejson encode it wrong as below
>
> {"adviser": "n.\u00e9\u00a1\u00be\u00e9\u0097\u00ae,
> <\u00e7\u00be\u008e>
> (\u00e5\u00ad\u00a6\u00e7\u0094\u009f\u00e7\u009a\u0084)\u00e6\u008c\u0087\u00e5\u00af\u00bc\u00e8\u0080\u0081\u00e5\u00b8\u0088",
> "od": "dd"}
>
> There is some codes can fix this problem , would some one merge this
> into svn ? :)
>
> from django.http import HttpResponse
> from django.utils import simplejson
> from django.conf import settings
>
> def json_to_response(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):
>         return unicode(a, encoding)
>     elif isinstance(a, unicode):
>         return a
>     else:
>         return a
>
>
I have a similar function, but it has some different from your:

def uni_str(a, encoding=None):
    if not encoding:
        encoding = settings.DEFAULT_CHARSET
    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, unicode):
        return a
    elif isinstance(a, str) or (hasattr(a, '__str__') and
callable(getattr(a, '__str__'))):
        if getattr(a, '__str__'):
            a = str(a)
        return unicode(a, encoding)
    else:
        return a


-----------------------------------------------

    elif isinstance(a, str) or (hasattr(a, '__str__') and
callable(getattr(a, '__str__'))):
        if getattr(a, '__str__'):
            a = str(a)
        return unicode(a, encoding)

is not like your. Because for some objects their type are not string,
but they can be convert to string. So you should convert them into
string.

-- 
I like python!
UliPad <<The Python Editor>>: http://wiki.woodpecker.org.cn/moin/UliPad
My Blog: http://www.donews.net/limodou

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to