如何使用groovy在JSR223 Post处理器中获取json数组的长度

时间:2016-09-28 07:24:00

标签: arrays json groovy jsr223

我有一个查询涉及如何使用groovy获取JSR223 Post处理器中json数组的长度。 " outBound"内有2个信息块。阵列。我需要得到阵列的长度" outBound"这样我就可以将长度放在for循环中。我还想得到一个信息json数组(原样),其中包含参数" taxType":" GST"。例如:这里第二个信息有GST值..所以想要获取第二个信息json数组

$this->load->library('excel');
$file_name = 'Demo';
$arrHeader = array('Name', 'Mobile');
$arrRows = array(0=>array('Name'=>'Jayant','Mobile'=>54545), 1=>array('Name'=>'Jayant1', 'Mobile'=>44454), 2=>array('Name'=>'Jayant2','Mobile'=>111222), 3=>array('Name'=>'Jayant3', 'Mobile'=>99999));
$this->excel->getActiveSheet()->fromArray($arrHeader,'','A1');
$this->excel->getActiveSheet()->fromArray($arrRows);
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Content-Disposition: attachment;filename="'.$file_name.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
$objWriter->save('php://output');

1 个答案:

答案 0 :(得分:0)

我不太确定我理解,但如果我这样做,那就是你需要的:

def res = new groovy.json.JsonSlurper().parseText(json)
def outBound = res.details[0].outBound
println outBound.size()
println groovy.json.JsonOutput.toJson(outBound.find { it.info.gstAmount?.taxType == "GST" })

json是持有json的String实例。

逐行:

  1. Groovy消耗你的Json。
  2. 您从outBound数组
  3. 的第一项检索details数组
  4. 打印outBound数组
  5. 的大小
  6. outBound属性为" GST" info.gstAmout.taxType数组的第一个元素格式化回Json。 (gstAmount之后的问号优雅地处理可能缺少该财产的情况)