以下是我的json请求。我的要求是如果“ cols”。“ name” =“ tran_amt”找到“ cols”。“ value”字段的总数。
{
"request" : {
"reqID" : "6839796504132",
"processID" : 97904250,
"tables" : [ {
"tableName" : "TABLE_NAME",
"records" : [ {
"recordID" : "record_id1",
"cols" : [ {
"name" : "tran_amt",
"type" : "VARCHAR2",
"value" : "562356"
}, {
"name" : "tran_id",
"type" : "VARCHAR2",
"value" : "123456"
} ]
}, {
"recordID" : "record_id2",
"cols" : [ {
"name" : "tran_amt",
"type" : "VARCHAR2",
"value" : "987098"
}, {
"name" : "tran_id",
"type" : "VARCHAR2",
"value" : "123456"
} ]
} ]
} ]
}
}
这是我尝试过的解决方案,
cols = request.getTables().get(0).getRecords().get(0).getCols();
total = cols.stream().filter(o -> "tran_amt".equalsIgnoreCase(o.getName())).mapToInt(o -> Integer.valueOf(o.getValue())).sum();
我的解决方案仅从“ cols”的第一个obj中检索“ value”(在这种情况下为562356)。它不对符合条件的所有“值”求和。
关于我错了的任何输入吗?
提前致谢。
试试这个:
total = request.getTables()
.stream()
.flatMap(t -> t.getRecords().stream())
.flatMap(r -> r.getCols().stream())
.filter(c -> "tran_amt".equalsIgnoreCase(c.getName()))
.mapToInt(c -> Integer.valueOf(c.getValue()))
.sum();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句