Tags:python • json 直接通过 JSON.loads 获取到 UTF8 类型的数据 处理函数为 json.loads(data, object_hook=_decode_object) def _decode_list(data): rv = [] for item in data: if isinstance(item, unicode): item = item.encode('utf-8') elif isinstance(item, list): item = _decode_list(item) elif isinstance(item, dict): item = _decode_dict(item) rv.append(item) return rv def _decode_dict(data): rv = {} for key, value in data.iteritems(): if isinstance(key, unicode): key = key.encode('utf-8') if isinstance(value, unicode): value = value.encode('utf-8') elif isinstance(value, list): value = _decode_list(value) elif isinstance(value, dict): value = _decode_dict(value) rv[key] = value return rv def _decode_object(data): rv = [] if isinstance(data, list): return _decode_list(data) if isinstance(data, dict): return _decode_dict(data) return data 处理函数为 json.loads(data, object_hook=byteify) def byteify(data): if isinstance(data, dict): return dict([(byteify(k), byteify(v)) for k, v in data.iteritems()]) elif isinstance(data, list): return [byteify(v) for v in data] elif isinstance(data, unicode): return data.encode('utf-8') else: return data Previous article Next article