BIRT : Scripted Data Source using JSON

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

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


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.

你可能感兴趣的:(script)