pycharm执行语句features_str = jsondumpsparamsfeaturesparamsfeatureshash = get_parameter_hashfeatures_strencodeutf-8出现错误提示:TypeError:Object of type bytes is not JSON serializable
这个错误提示是因为json.dumps()函数只能处理可序列化的对象,而参数features_str是一个bytes类型的对象,不可序列化。
为了解决这个问题,可以将features_str转换为字符串类型再进行序列化。可以使用decode()函数将bytes对象转换为字符串对象,如下所示:
features_str = json.dumps(params['features'].decode('utf-8'))
params['features']['hash'] = get_parameter_hash(features_str.encode('utf-8'))
这样就可以避免TypeError。
原文地址: https://www.cveoy.top/t/topic/hSqS 著作权归作者所有。请勿转载和采集!