Groovy 正在打印地图<String,List<Map<String,String>>> 数据
Groovy printing Map<String,List<Map<String,String>>> data
我有Map<String,List<Map<String,String>>> invoices
如下
invLineItems = [INV-Error_Test1:[[LINE:1, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test1, INVOICE_TOTAL:22], [LINE:2, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test1, INVOICE_TOTAL:24]],
INV-Error_Test2:[[LINE:3, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test2, INVOICE_TOTAL:26], [LINE:4, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test2, INVOICE_TOTAL:28,]]]
我正在尝试打印如下
println " INV-Error_Test2 Details "
invLineItems.get('INV-Error_Test2').each{
it.each{
print "LINE = "+ it['LINE']
}
}
并且低于错误 groovy.lang.MissingPropertyException: No such property: LINE for class: java.util.LinkedHashMap$Entry
有人可以帮我打印这些数据吗?
注意:实际上我总共有 24 个 headers,但为了使问题简单化,我刚刚发布了 4 个 headers(Line, Invoice_Date,Invoice-Number,Invoice_Total
) 并且我只尝试打印几个 headers 共 24
更新:我正在尝试用下面的发票错误更新 Map<String,List<Map<String,String>>> invoices
InvoiceError // is an entity with below attributes
{ String errorMessage,
String invoiceNumber
}
ErrorMessage invoiceNumber
------------- -------------------
File Error : The file is in an unsupported format INV-Error_Test1
Line : 1 Invoice does not foot Reported INV-Error_Test1
Line : 2 MATH ERROR INV-Error_Test1
Line : 3 MATH ERROR INV-Error_Test2
Line : 3 Invoice does not foot Reported INV-Error_Test2
我正在努力实现下面的地图
如果错误消息没有行号,则需要在顶层附加为 invLineItems.put('error',['INV-Error_Test1' :
文件错误:文件格式不受支持])
否则应将错误消息附加到匹配的发票和行号,如下所示
invLineItems = [INV-Error_Test1:[[LINE:1, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test1, INVOICE_TOTAL:22, error : `Line : 1 Invoice does not foot Reported`],
[LINE:2, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test1, INVOICE_TOTAL:24, error : `Line : 2 MATH ERROR`],
INV-Error_Test2:[[LINE:3, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test2, INVOICE_TOTAL:26, , error : `Line : 3 MATH ERROR | Line : 3 Invoice does not foot Reported`],
[LINE:4, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test2, INVOICE_TOTAL:28,]],
error : [[INV-Error_Test1:`File Error : The file is in an unsupported format`]]
我写了下面的方法来实现上面的
def regex = "^Line\s(?:(\d+)\s)?\s*:\s+(\d+)?.+";
for (e in invLineItems ){
def errors = lipErrors.findAll{it.invoiceNumber==e.key} // finding the error messages with the invoice number
errors.each{ // fetching the line numbre from error message and finding the matching record the invoice number and line number in invLineItems
int lineNumber
if (it.errorMessage.matches(regex)) {
Pattern p = Pattern.compile("\d+");
Matcher m = p.matcher(it.errorMessage);
if (m.find()) {
lineNumber = Integer.parseInt(m.group());
}
println "lineNumber = "+lineNumber
}
if(e.value['LINE_ITEM_NUMBER'].find{it==lineNumber.toString()}) {
def data = lipErrors.findAll{it.invoiceNumber==e.key && it.errorMessage.matches("^Line\s+"+lineNumber+"?\:\s+"+lineNumber+"?.+")}
e.getValue().each{it.put("error", data.errorMessage.join("|"))}
}
}
}
代码看起来不像Groovy并且主要使用传统的java代码,我想知道是否可以使用Groovy方法
简化代码
只需要一次迭代。
def invLineItems = [
'INV-Error_Test1': [
[LINE:1, INVOICE_DATE:20150301, INVOICE_NUMBER:'INV-Error_Test1', INVOICE_TOTAL:22],
[LINE:2, INVOICE_DATE:20150301, INVOICE_NUMBER:'INV-Error_Test1', INVOICE_TOTAL:24]
],
'INV-Error_Test2': [
[LINE:3, INVOICE_DATE:20150301, INVOICE_NUMBER:'INV-Error_Test2', INVOICE_TOTAL:26],
[LINE:4, INVOICE_DATE:20150301, INVOICE_NUMBER:'INV-Error_Test2', INVOICE_TOTAL:28,]
]
]
invLineItems['INV-Error_Test2'].each {
println "LINE = "+ it['LINE']
}
invLineItems['INV-Error_Test2']
给出第二个 Map 条目的值,迭代获取嵌套的 map 条目。
如果您只需要 LINE
的值,那么
assert invLineItems.'INV-Error_Test2'*.'LINE' == [3, 4]
应该够了。
我有Map<String,List<Map<String,String>>> invoices
如下
invLineItems = [INV-Error_Test1:[[LINE:1, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test1, INVOICE_TOTAL:22], [LINE:2, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test1, INVOICE_TOTAL:24]],
INV-Error_Test2:[[LINE:3, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test2, INVOICE_TOTAL:26], [LINE:4, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test2, INVOICE_TOTAL:28,]]]
我正在尝试打印如下
println " INV-Error_Test2 Details "
invLineItems.get('INV-Error_Test2').each{
it.each{
print "LINE = "+ it['LINE']
}
}
并且低于错误 groovy.lang.MissingPropertyException: No such property: LINE for class: java.util.LinkedHashMap$Entry
有人可以帮我打印这些数据吗?
注意:实际上我总共有 24 个 headers,但为了使问题简单化,我刚刚发布了 4 个 headers(Line, Invoice_Date,Invoice-Number,Invoice_Total
) 并且我只尝试打印几个 headers 共 24
更新:我正在尝试用下面的发票错误更新 Map<String,List<Map<String,String>>> invoices
InvoiceError // is an entity with below attributes
{ String errorMessage,
String invoiceNumber
}
ErrorMessage invoiceNumber
------------- -------------------
File Error : The file is in an unsupported format INV-Error_Test1
Line : 1 Invoice does not foot Reported INV-Error_Test1
Line : 2 MATH ERROR INV-Error_Test1
Line : 3 MATH ERROR INV-Error_Test2
Line : 3 Invoice does not foot Reported INV-Error_Test2
我正在努力实现下面的地图
如果错误消息没有行号,则需要在顶层附加为 invLineItems.put('error',['INV-Error_Test1' :
文件错误:文件格式不受支持])
否则应将错误消息附加到匹配的发票和行号,如下所示
invLineItems = [INV-Error_Test1:[[LINE:1, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test1, INVOICE_TOTAL:22, error : `Line : 1 Invoice does not foot Reported`],
[LINE:2, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test1, INVOICE_TOTAL:24, error : `Line : 2 MATH ERROR`],
INV-Error_Test2:[[LINE:3, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test2, INVOICE_TOTAL:26, , error : `Line : 3 MATH ERROR | Line : 3 Invoice does not foot Reported`],
[LINE:4, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test2, INVOICE_TOTAL:28,]],
error : [[INV-Error_Test1:`File Error : The file is in an unsupported format`]]
我写了下面的方法来实现上面的
def regex = "^Line\s(?:(\d+)\s)?\s*:\s+(\d+)?.+";
for (e in invLineItems ){
def errors = lipErrors.findAll{it.invoiceNumber==e.key} // finding the error messages with the invoice number
errors.each{ // fetching the line numbre from error message and finding the matching record the invoice number and line number in invLineItems
int lineNumber
if (it.errorMessage.matches(regex)) {
Pattern p = Pattern.compile("\d+");
Matcher m = p.matcher(it.errorMessage);
if (m.find()) {
lineNumber = Integer.parseInt(m.group());
}
println "lineNumber = "+lineNumber
}
if(e.value['LINE_ITEM_NUMBER'].find{it==lineNumber.toString()}) {
def data = lipErrors.findAll{it.invoiceNumber==e.key && it.errorMessage.matches("^Line\s+"+lineNumber+"?\:\s+"+lineNumber+"?.+")}
e.getValue().each{it.put("error", data.errorMessage.join("|"))}
}
}
}
代码看起来不像Groovy并且主要使用传统的java代码,我想知道是否可以使用Groovy方法
简化代码只需要一次迭代。
def invLineItems = [
'INV-Error_Test1': [
[LINE:1, INVOICE_DATE:20150301, INVOICE_NUMBER:'INV-Error_Test1', INVOICE_TOTAL:22],
[LINE:2, INVOICE_DATE:20150301, INVOICE_NUMBER:'INV-Error_Test1', INVOICE_TOTAL:24]
],
'INV-Error_Test2': [
[LINE:3, INVOICE_DATE:20150301, INVOICE_NUMBER:'INV-Error_Test2', INVOICE_TOTAL:26],
[LINE:4, INVOICE_DATE:20150301, INVOICE_NUMBER:'INV-Error_Test2', INVOICE_TOTAL:28,]
]
]
invLineItems['INV-Error_Test2'].each {
println "LINE = "+ it['LINE']
}
invLineItems['INV-Error_Test2']
给出第二个 Map 条目的值,迭代获取嵌套的 map 条目。
如果您只需要 LINE
的值,那么
assert invLineItems.'INV-Error_Test2'*.'LINE' == [3, 4]
应该够了。