动态 json 转化序表

动态 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

imagepng

imagepng

imagepng