如何在Python(2.6)中将JSON解码为str而不是unicode?

2024-05-18 00:42:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个JSON格式的配置文件,其中包含一些变量作为字符串(总是ascii)。默认情况下,这些字符串被解码为unicode,但是由于我必须将这些变量传递给PythonC扩展,所以我需要它们作为普通的Python字符串。目前,我正在使用str(unicode)来转换JSON字符串,但是一个更优雅、更简洁的解决方案将非常受欢迎。

有没有办法用自定义的JSONDecoder或对象挂钩将默认的字符串转换为unicode?


Tags: 对象字符串json格式配置文件asciiunicode情况
2条回答

如果你不想失去一些速度就不会。如果稍微慢一点是可以的,那么您必须考虑使用普通的json.loads并递归地转换为str可能更便宜,也可能更快。

尽管如此,如果您确实希望返回字符串的loads严重到可以接受不需要扩展代码的程度,这里有一个可能的结果(主要是通过copy-n-paste扩展)这很简单,感谢Lennart让我看到了曙光(即,您只需要扩展JSONDecoder和几个技巧):

import json
from json import decoder, scanner

from json.scanner import make_scanner
from _json import scanstring as c_scanstring

_CONSTANTS = json.decoder._CONSTANTS

py_make_scanner = scanner.py_make_scanner

# Convert from unicode to str
def str_scanstring(*args, **kwargs):
    result = c_scanstring(*args, **kwargs)
    return str(result[0]), result[1]

# Little dirty trick here
json.decoder.scanstring = str_scanstring

class StrJSONDecoder(decoder.JSONDecoder):
    def __init__(self, encoding=None, object_hook=None, parse_float=None,
            parse_int=None, parse_constant=None, strict=True,
            object_pairs_hook=None):
        self.encoding = encoding
        self.object_hook = object_hook
        self.object_pairs_hook = object_pairs_hook
        self.parse_float = parse_float or float
        self.parse_int = parse_int or int
        self.parse_constant = parse_constant or _CONSTANTS.__getitem__
        self.strict = strict
        self.parse_object = decoder.JSONObject
        self.parse_array = decoder.JSONArray
        self.parse_string = str_scanstring
        self.scan_once = py_make_scanner(self)

# And another little dirty trick there    
_default_decoder = StrJSONDecoder(encoding=None, object_hook=None,
                               object_pairs_hook=None)

json._default_decoder = _default_decoder

j = {1:'2', 1.1:[1,2,3], u'test': {12:12, 13:'o'}}
print json.loads(json.dumps(j))

看看对this question的回答是否有帮助(在这个问题上,询问者使用的是simplejson)。

相关问题 更多 >