Object DataSource supports not only JAVA files, but also JSON files. There is an example file named
Amount.json
. Here is how to read data from this file using the Object DataSource.
Review the sample file Amount.json
, as shown in Figure 10.3, “Sample JSON file”.
Remember the location of the JSON file as we will need to reference it.
Create a new Object DataSource called Object2
. Click Add.
The Add Column
dialog opens.
Fill in column names and data types according to those written in the JSON file. They must be exactly the same, so that data can be successfully loaded.
Click Next. In the JavaScript
tab page, fill in the following code:
function pushTo(/*PushContext*/ cxt, /*DataListener*/ dl)
{
dl.startData(this);
var contents = this.getRepositoryFileContents("FilePath/
Amount.json");
if (contents!=null) {
var barley = eval(""+contents);
var len = barley.length;
for (var i=0;i<len;i++)
{
var rec = this.buildRecord(barley[i]);
dl.processRecord(rec);
}
}
dl.endData(this);
}
Click Finish. The Object datasource is added to the repository.
Click Load Data in the Data window. View the data to verify if the correct data is returned, as shown in Figure 10.4, “Load data from JSON file”.