>我正在创建一个日期数组,在我的解析器中,它只返回一个日期和计数的输出
我在整个谷歌中搜索以找到一些答案,我找到了如何制作列表数组的代码,但问题是它没有很好地返回
https://i.stack.imgur.com/bcRHc.jpg这是一个结果,我在图片中附加了我使用它的代码
#set ($tu = 0) #set ($pc = 0) #set ($fc = 0) #set ($da = [])#set ($cda = []) #foreach($item in $ctx.result.items)
#set($tu = $item.total_audits + $tu)
#set($pc = $item.passed_compliance + $pc)
#set($fc = $item.failed_compliance + $fc)
#set($date = $item.sort)
#set($count = $item.total_audits)
$util.qr($da.add("$date"))
$util.qr($cda.add("$count"))
#end
$util.toJson({"total_audits":$tu, "passed_compliance":$pc,
"failed_compliance":$fc, "daily_audit": [{"date": $da, "count": $cda}]})
这是错误
"errors": [
{
"path": [
"getAuditSummary",
"daily_audit",
0,
"date"
],
"locations": null,
"message": "Can't serialize value (/getAuditSummary/daily_audit[0]/date) : Unable to serialize `[2018-12-26, 2018-12-27, 2018-12-28]` as a valid date."
},
{
"path": [
"getAuditSummary",
"daily_audit",
0,
"count"
],
"locations": null,
"message": "Can't serialize value (/getAuditSummary/daily_audit[0]/count) : Expected type 'Int' but was 'ArrayList'."
我想做的是它会返回这样的东西
"daily_audit": [
{
"date": 2018-12-26,
"count": 1
}
{
"date": 2018-12-27,
"count": 4
}
{
"date": 2018-12-28,
"count": 2
}
]
发生这种情况是因为$da
和$cda
是数组。所以它可能会返回:
"daily_audit": [
{
"date": [2018-12-26,2018-12-27,2018-12-28],
"count": [1,4,2]
}
]
因此,在响应映射模板中,您可以尝试如下操作:
#set ($tu = 0)
#set ($pc = 0)
#set ($fc = 0)
#set ($da = [])
#foreach($item in $ctx.result.items)
#set($tu = $item.total_audits + $tu)
#set($pc = $item.passed_compliance + $pc)
#set($fc = $item.failed_compliance + $fc)
#set($date = $item.sort)
#set($count = $item.total_audits)
$util.qr($da.add({"date":$date, "count":$count}))
#end
$util.toJson({"total_audits":$tu, "passed_compliance":$pc, "failed_compliance":$fc, "daily_audit": $da})