如何在常规脚本中将null转换为0 [英] How to cast null to 0 in groovy scripting
本文介绍了如何在常规脚本中将null转换为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一笔款项,但我想将所有 null
值都转换为 0
值,所以它可以执行总和。但是,我收到此错误;
I am trying to create a sum but I want to cast all of the null
values as 0
values so that it can perform the sum. However I am receiving this error;
Cannot cast object '[null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null]'
with class 'java.util.ArrayList' to class 'int'
<
How can I cast all nulls into 0?
以下是我的代码:
Below is the code I have:
def resultSearchCostGroup = json.xxx.xxx.xxx.search
int totalSearchCostGroup = json.xxx.xxx.xxx.search.collectMany{ it ?: 0 }.sum()
log.info resultSearchCostGroup
推荐答案
c $ c> flatten 您的收藏,然后 collect
来取代空值?
Your you can just flatten
your collection and then collect
to replace nulls?
json.products.costGroups.costLines.search.flatten().collect{it?:0}.sum()
这篇关于如何在常规脚本中将null转换为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文