如何序列化布尔数组
how to serialize array of booleans
我有一个用 python 编写的网络服务。它从 Web 服务的 angular code.in python 代码中调用,如下所示,Web 服务 returns isKeyWindowSegmentRepresentative
是一个数组布尔值
.当从 angular 代码调用服务时,我收到 isKeyWindowSegmentRepresentative
作为字符串。
我想接收上述数组作为包含布尔值的数组,以便我可以遍历其内容。
Web 服务 returns 和 angular 代码显示的是以下字符串:
isKeyWindowSegmentRepresentative: "[false, false, true, true, false, false, true, true, true, true]"
我想接收它作为布尔数组。
注:
它不是一个 numpy 数组。它是一个普通数组,声明如下:
isKeyWindowSegmentRepresentative=[]
更新:
for: "pixelsValuesSatisfyThresholdInWindowSegment":np.array(pixelsValuesSatisfyThresholdInWindowSegment,dtype=float).tolist()
i recieve the following error:
TypeError: float() argument must be a string or a number, not 'list'
the pixelsValuesSatisfyThresholdInWindowSegment contains _pixelsValuesSatisfyThresholdInWindowSegment
logger.debug(type(pixelsValuesSatisfyThresholdInWindowSegment)):<class 'list'>
logger.debug(type(pixelsValuesSatisfyThresholdInWindowSegment[0])):<class 'list'>
logger.debug(type(_pixelsValuesSatisfyThresholdInWindowSegment)):<class 'list'>
logger.debug(type(_pixelsValuesSatisfyThresholdInWindowSegment[0]))::<class 'numpy.float32'>
从 python 代码返回的字典为 json:
resultsDict = {
"isKeyWindowSegmentRepresentative":json.dumps(np.array(isKeyWindowSegmentRepresentative, dtype=bool).tolist())
}
只是不要将列表作为字符串转储到您的字典中,而且您似乎有一些不可 [=16=] 可序列化的 numpy 数据类型,请先转换它们:
resultsDict = {
"isKeyWindowSegmentRepresentative": [bool(x) for x in isKeyWindowSegmentRepresentative]
}
同样,如果你有 numpy 浮点数,使其 JSON 可序列化:
resultsDict = {
"isKeyWindowSegmentRepresentative": [float(x) for x in isKeyWindowSegmentRepresentative]
}
从评论中,我了解到 isKeyWindowSegmentRepresentative
变量是 dtype bool 的 NumPy 数组。
isKeyWindowSegmentRepresentative = np.array([True, False, False, True])
resultsDict = {
"isKeyWindowSegmentRepresentative": isKeyWindowSegmentRepresentative.tolist()
}
如果它是 float 类型,它也应该可以工作,因为 float 是 JSON 可序列化的。
my_floats = np.array([1.1, 2.2, 3.3, 4.4])
resultsDict = {
"my_floats": my_floats.tolist()
}
它可能不适用于所有数据类型,因为 NumPy arrays are not JSON serializable。
我有一个用 python 编写的网络服务。它从 Web 服务的 angular code.in python 代码中调用,如下所示,Web 服务 returns isKeyWindowSegmentRepresentative
是一个数组布尔值
.当从 angular 代码调用服务时,我收到 isKeyWindowSegmentRepresentative
作为字符串。
我想接收上述数组作为包含布尔值的数组,以便我可以遍历其内容。
Web 服务 returns 和 angular 代码显示的是以下字符串:
isKeyWindowSegmentRepresentative: "[false, false, true, true, false, false, true, true, true, true]"
我想接收它作为布尔数组。
注:
它不是一个 numpy 数组。它是一个普通数组,声明如下: isKeyWindowSegmentRepresentative=[]
更新:
for: "pixelsValuesSatisfyThresholdInWindowSegment":np.array(pixelsValuesSatisfyThresholdInWindowSegment,dtype=float).tolist()
i recieve the following error:
TypeError: float() argument must be a string or a number, not 'list'
the pixelsValuesSatisfyThresholdInWindowSegment contains _pixelsValuesSatisfyThresholdInWindowSegment
logger.debug(type(pixelsValuesSatisfyThresholdInWindowSegment)):<class 'list'>
logger.debug(type(pixelsValuesSatisfyThresholdInWindowSegment[0])):<class 'list'>
logger.debug(type(_pixelsValuesSatisfyThresholdInWindowSegment)):<class 'list'>
logger.debug(type(_pixelsValuesSatisfyThresholdInWindowSegment[0]))::<class 'numpy.float32'>
从 python 代码返回的字典为 json:
resultsDict = {
"isKeyWindowSegmentRepresentative":json.dumps(np.array(isKeyWindowSegmentRepresentative, dtype=bool).tolist())
}
只是不要将列表作为字符串转储到您的字典中,而且您似乎有一些不可 [=16=] 可序列化的 numpy 数据类型,请先转换它们:
resultsDict = {
"isKeyWindowSegmentRepresentative": [bool(x) for x in isKeyWindowSegmentRepresentative]
}
同样,如果你有 numpy 浮点数,使其 JSON 可序列化:
resultsDict = {
"isKeyWindowSegmentRepresentative": [float(x) for x in isKeyWindowSegmentRepresentative]
}
从评论中,我了解到 isKeyWindowSegmentRepresentative
变量是 dtype bool 的 NumPy 数组。
isKeyWindowSegmentRepresentative = np.array([True, False, False, True])
resultsDict = {
"isKeyWindowSegmentRepresentative": isKeyWindowSegmentRepresentative.tolist()
}
如果它是 float 类型,它也应该可以工作,因为 float 是 JSON 可序列化的。
my_floats = np.array([1.1, 2.2, 3.3, 4.4])
resultsDict = {
"my_floats": my_floats.tolist()
}
它可能不适用于所有数据类型,因为 NumPy arrays are not JSON serializable。