JSON is widely used as a lightweight data format, and the need for reports to read JSON data sources for report development is also common. Dry report using a JSON data source requires that the result set be returned to the report after the JSON file is parsed in a custom dataset through a Java program, and the implementation of the run-dry report and the improvement method are described here.
Reporting requirements
Student performance in the application of the JSON file exists, it is necessary to query the specific subjects according to the subject parameters of student performance, and in descending order of student performance. The report style is as follows:
Run dry report implement custom Data set
Run dry report you need to complete the JSON file reading through a custom dataset, the following is the implementation process:
1. Prepare JSON parsing dependency package
Parsing a JSON file in a Java program requires 6 jar packages with the name:
1) Json-lib-2.4-jdk15.jar
Http://sourceforge.net/projects/json-lib/?source=typ_redirect
2) Ezmorph-1.0.6.jar
Http://sourceforge.net/projects/ezmorph/files/latest/download?source=files
3) Commons-lang-2.6.jar
http://commons.apache.org/proper/commons-lang/download_lang.cgi
4) Commons-beanutils-1.9.2.jar
http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi
5) Commons-logging-1.2.jar
http://commons.apache.org/proper/commons-logging/download_logging.cgi
6) Commons-collections-3.2.1.jar
http://commons.apache.org/proper/commons-collections/download_collections.cgi
2. Define student achievement class for storing student information
Publicclass Score {
// Omit Constructors and Get Set Method
StringclassName;
StringID;
Stringstudentname;
Stringsubject;
Stringscore;
}
3. Receive and parse report parameters in a custom data set
map map = Ctx.getparammap (false);
Stringsubject = Map.get ("Subject"). toString (); // Statistical Accounts
4. Read in the JSON file and save its contents to list
List<score>list = newarraylist<score> ();
Stringjsoncontext = readFile("E:\\score.json");
Jsonarrayjsonarray = Jsonarray. Fromobject (Jsoncontext);
int size =jsonarray.size ();
//JSON content to score Format Deposit List
for (int i = 0; i < size;i++) {
Jsonobjectjo = Jsonarray.getjsonobject (i);
if (Jo.getstring ("Subject"). Equals (subject) | | jo.getstring ("Subject") ==subject)
List.add (new score (jo.getstring ("ClassName"), jo.getstring ("id"), jo.getstring (" Name "), jo.getstring (" Subject "), Jo.getstring (" score "));
}
5, define sorting class, realize compare method compare student Achievement
Publicclass ComparatorscoreimplementsComparator {
publicint Compare (Objecto1,object O2) {
ScoreScore1 = (score) O1;
Scorescore2 = (score) O2;
returnScore2.getscore (). CompareTo (score1. Getscore ());
}
}
6. Complete list sorting using Collections.sort
Comparatorscorecs = newcomparatorscore ();
collections. Sort (List,cs);
7. Construct a dataset and set data for the dataset
// construct a data set DS1 , set the column name
DataSetDS1 =new DataSet ("DS1");
ds1. Addcol ("ClassName");
ds1. Addcol ("id");
ds1. Addcol ("name");
ds1. Addcol ("subject");
ds1. Addcol ("score");
// set up data in a data set
for (int i = 0; I <list.size (); i++) {
Scorescore = List.get (i);
COM.RUNQIAN.REPORT4.DATASET.ROWRR = ds1. AddRow ();
Rr.setdata (1,score.getclassname ());
Rr.setdata (2,score.getid ());
Rr.setdata (3,score.getstudentname ());
Rr.setdata (4,score.getsubject ());
Rr.setdata (5,score.getscore ());
}
return DS1;
Report calls
The report uses a custom DataSet type:
To set up a report template and an expression:
Dry report supports custom datasets, providing more choices for report developers, and the flexibility of dry reporting. However, relying too much on Java programming has undoubtedly increased the difficulty of report development, in addition to preparing the jar package to parse the JSON file, but also to complete the calculation goals such as data sorting in the Java program, and the complexity of the program will rise sharply when computing including grouping, connection and so on, it is not a small challenge for report developers. Of course, you can use the report tool to complete sorting operations, but this will result in a large amount of memory (especially when there are filtering actions, you need to take the data to the report side and then perform filtering), performance will be greatly affected, there are many complex file operations will be beyond the scope of the report calculation capabilities.
In this case, it will be a good choice to calculate the enhanced version of the report on the basis of the run-dry report. The collection report supports JSON data sources and enables rapid development of such reports. This example can be done in a set report.
Compiling a collection script for a collection report implementation
First use the set to complete the file read-in and sorting, the set of the script as follows:
A1: Read in JSON format file as string;
A2: Import and parse the order table from the JSON format string;
A3: Subject The data filter expression string according to the discipline parameter;
A4: Parse the expression and complete the data filtering;
A5: In descending order of results, the result is returned by A6.
Report calls
DataSet settings
To use the collector dataset type in a collection report, select the editor script file (fromjson.dfx) that you edited above:
Where the parameter name subject is the set-up script parameter, and the argument value is subject to the report parameter.
Report templates and expressions
Through the above implementation process can be seen, and dry report needs custom data set through the Java program implementation, the collector through a few lines of code to complete the JSON format file reading and presentation, compared to hundreds of lines of Java program is much simpler. The ability to recalculate (filter, group, sort, and so on) files directly using JSON-formatted reports can be very simple to develop in a collection report.
In addition, when the set-up script is simple, you can directly embed the script dataset into the report template by using the built-in script datasets of the collection report, rather than having to do it separately in the collector. Here's how it's implemented:
1. Click the "Add" button in the DataSet Settings window to pop up the dataset Type dialog box and select "Script DataSet":
2. Write the set-up script in the Popup Script DataSet editing window:
3, report calls, and other data sets used in the same manner, no longer repeat.
Dry report improved methods for using JSON data sources