jython的乱码问题

最近线上遇到一次乱码问题,记录如下.

使用java client发送的url,其中key_words是乱码:

 

searchService/queryBlog?q=["{\"sort_by\":\"desc\",\"sort\":\"last_replied_datetime\",\"page\":1,\"filter\":[\"-photo_id:0\",\"status:0\",\"-pop_score:0\",\"buyable:0\",\"category:9 OR category:1\"],\"key_words\":\"句子 OR 文字\",\"page_size\":18}"]

 

而python client不会有乱码问题:

searchService/queryBlog?q=["{\"sort\": \"last_replied_datetime\", \"page_size\": 18, \"sort_by\": \"desc\", \"filter\": [\"status:0\", \"category:9 OR category:1\", \"-pop_score:0\", \"-photo_id:0\", \"buyable:0\"], \"key_words\": \"\\u6587\\u5b57 OR \\u53e5\\u5b50\", \"page\": 1}"]

 看了一下python client对参数的处理方式:

        for param in params:
            if param is None:
                param = param
            elif    isinstance(param,(unicode,str,int, float)):
                param = param
            elif isinstance(param,datetime):
                param  = time.mktime(param.timetuple())
            else:
                param = json.dumps(param,cls=DbossObjectEncoder)
            query.append(param)

 可以看到如果是非基本类型会通过json.dumps,原来json.dumps会自动把unicode转换为utf-8:

 

>>> k = {'name':'中文'}
>>> k
{'name': '\xe4\xb8\xad\xe6\x96\x87'}
>>> json.dumps(k)
'{"name": "\\u4e2d\\u6587"}'

 

你可能感兴趣的:(jython)