BIRT : Scripted Data Source using JSON

[url]http://www.birt-exchange.org/org/devshare/designing-birt-reports/1053-scripted-data-source-using-json/[/url]

下载地址: [url]http://www.birt-exchange.org/config/devshare/visit.php?aid=940[/url]


The attached resource provides everything you need to deploy a Scripted Data Source able to process JSON-formatted data for use in your BIRT reports.

BIRT VERSION:-
The resource attached to this project is an Eclipse Workspace with two projects. The first, called JSONParser is a Java class capable of manipulating JSON data such that it can be loaded into a BIRT scripted data set. The second project, called "Sample Reports", contains two BIRT reports with Scripted Data Sets bound to a JSON source.

There are three steps to processing JSON data with this class:

1) Create and empty report and add a hidden parameter called "JSONURL". Give this parameter the value of the URL where your JSON data can be found.

2) Add a new Scripted Data Source to your report. You will need to modify the "open" script on the data source to something like the following (sample taken from BIRTJobs.rptdesign):

// This will track your current row later on
count = 0;

// Create instance of the Controller class
controller = new Packages.com.actuate.json.JSONParser();

// Load the JSON Source
controller.loadData(params["JSONURL"]);

// Calculate the total rows we will have
totalCount = controller.getChildCount("value/items");


The JSON parser supports an XPath-style query syntax so you can target nested elements. The synatx above points to the "items" element and gets a count of the objects under it. The purpose of this script block is to instantiate the JSON parser and to get a total count of objects under "items". This count is in fact the number of rows on our Data Set.

3) Add a new Data Set and bind it to the Data Source you just created. Once the data set is created, you can add each of the columns you intend to populate from the JSON data. After creating these columns, modify the "fetch" script on the Data Set. This is the logic that will serve to populate the rows in the Data Set. The script will look something like this (sample taken from BIRTJobs.rptdesign):

if(count < totalCount){
row["URL"] = controller.getValue("value/items", "link", count);
row["title"] = controller.getValue("value/items", "title", count);
row["description"] = controller.getValue("value/items", "description", count);
row["type"] = controller.getValue("value/items", "g:job_type", count);
row["function"] = controller.getValue("value/items", "g:job_function", count);

// Location
row["city"] = controller.getValue("value/items", "g:location/city", count);
row["state"] = controller.getValue("value/items", "g:location/state", count);
row["zip"] = controller.getValue("value/items", "g:location/postal", count);
row["country"] = controller.getValue("value/items", "g:location/country", count);

count++;
return true;
}

return false;


The scirpt read values off the JSON stream via the parser. In some cases the values are stored in child objects nested in the stream (location). XPath-style syntax is used to drill into those child objects. Because the process of populating the rows is somewhat asynchronous, we use the count value to drill directly to a location in the items array (inside the "getValue()" method). This allows us to traverse an array wiuthout the parser maintianing any sort of state with respect to the Report Layer.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值