动态 json 转化序表

在iReport Designer中创建报告时,遇到从JSON文件动态获取并展示多个层级数据的问题。原始JSON包含客户及其车辆信息,目标是列出每个客户及其所有车辆。尽管能显示第一个车辆,但无法遍历所有车辆。解决方案建议使用集算器的SPL语言,在数据预处理阶段将多层JSON转换为二维表,从而简化报表脚本的复杂性。

【问题】

Hi,

I have a problem very similar to

http://community.jaspersoft.com/questions/844023/need-help-looping-throu…,

but I can’t just change the json file as done by the person asking that question.

Any help would be greatly appreciated.

----

I am trying to create a report in iReport Designer v5.6.0 that loops through a JSON file and returns all data from different levels. For example, I’ve built a very simple JSON file that has a list of customers, and for each customer it lists all of his/her vehicles including fields for make, model and mileage. The desired output would be a list of customers and underneath each customer a list of the vehicles he/she owns.

I have built a template that correctly displays each customer name, but I have been unable to figure out how to loop through each customer’s vehicles. (I can return the first vehicle by adding [0] to the field, but I can’t figure out how to loop through all vehicles.)

Json:

[{“custName”: “Alison Anderson”,“custCars”:[{“carMake”: “Chevrolet”,“carModel”: “Avalanche”,“carMiles”: 97740},{“carMake”: “Chevrolet”,“carModel”: “Beretta”,“carMiles”: 165500}]},{“custName”: “Bart Bloomfield”,“custCars”:[{“carMake”: “Ford”,“carModel”: “F150”,“carMiles”: 43252},{“carMake”: “Mercury”,“carModel”: “Marauder”,“carMiles”: 26699}]},{“custName”: “Charles Cominsky”,“custCars”:[{“carMake”: “Porsche”,“carModel”: “Cayman”,“carMiles”: 15520}]}]

【回答】

将多层 json 转为二维表,用报表脚本很麻烦,可以考虑用集算器在数据准备时用 SPL 处理:

A

1

=json(file(“D:/cars.json”).read())

2

=A1.news(custCars;A1.custName:custName,carMake,carModel,carMiles)

A1:将 json 文件读成文本,导入成序表。可以看出 A1 此时相当于按照 custName 分组的目标结果表。

A2:用对 A1 用 news 函数解析出 custCars 序表,合并到 A1

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值