用Json.loads出来的内容是Unicode格式,写了一个简单的转换函数,把结果转成都转成UTF-8的格式:
-
def json_unicode2utf8(js):
-
if isinstance(js, dict):
-
for key, value in js.items():
-
k = copy.deepcopy(key).encode('utf-8')
-
v = copy.deepcopy(value)
-
del js[key]
-
js[k] = v
-
if isinstance(v, dict):
-
json_unicode2utf8(v)
-
elif isinstance(v, list):
-
json_unicode2utf8(v)
-
else:
-
if isinstance(v, (str, unicode)):
-
v = v.encode('utf-8')
-
js[k] = v
-
-
elif isinstance(js, list):
-
for n in range(len(js)):
-
if isinstance(js[n], dict):
-
json_unicode2utf8(js[n])
-
elif isinstance(js[n], list):
-
json_unicode2utf8(js[n])
-
else:
-
if isinstance(js[n], (str, unicode)):
-
js[n] =js[n].encode('utf-8')
-
else:
-
print js.encode('utf-8')
阅读(1941) | 评论(0) | 转发(0) |