nmau Posted April 9, 2014 Share Posted April 9, 2014 Hello EverybodyI have an Web-Application where I read JSON from a request in Java. Then I put the JSON in a JsonDataSource (with the help of spring).If the Json is one Object there is no Problem to display the Object. But if I have a list then I don't know how to use it.This is the Json Example{ "project": { "version": 1, "id": 1, "name": "Project1" }, "persons": [ { "name": "Test Person 1", "favoriteColor": "red" }, { "name": "Test Person 2", "favoriteColor": "blue" } ]}[/code]Java CodeInputStream stream = new ByteArrayInputStream(body.getBytes("UTF-8"));JRjsonsource = new JsonDataSource(stream);ModelAndView model = new ModelAndView( templatename + "/" + templatename + ".jrxml", createParameterMap(JRjsonsource));[/code] JRXMLThe project.name worksBut how I have to define the persons? That I can use it in a list or in a Subreport.....<field name="projectName" class="java.lang.String"> <fieldDescription><![CDATA[project.name]]></fieldDescription> </field> <field name="persons" class="net.sf.jasperreports.engine.data.JsonDataSource"> <fieldDescription><![CDATA[persons]]]></fieldDescription> </field>....<subreport> <reportElement x="0" y="72" width="554" height="98" isRemoveLineWhenBlank="true" backcolor="#FFCC99" uuid="4eb7e220-6758-465f-83c9-45cdfecc33eb"/> <subreportParameter name="net.sf.jasperreports.engine.data.JsonDataSource"> <subreportParameterExpression><![CDATA[$F{persons}]]></subreportParameterExpression> </subreportParameter> <subreportExpression><![CDATA["jsonPerson.jasper"]]></subreportExpression> </subreport> [/code]Thanks for your help. Link to comment Share on other sites More sharing options...
nmau Posted April 9, 2014 Author Share Posted April 9, 2014 Ok, for a Subreprot I found this:http://community.jaspersoft.com/questions/803726/how-pass-json-list-subreportBut for a List? Link to comment Share on other sites More sharing options...
Solution narcism Posted June 17, 2014 Solution Share Posted June 17, 2014 Hi,The following solution requires the jasperreports library to be at least version 5.0.4. Your jrxml fragment should look like this:.....<field name="projectName" class="java.lang.String"> <fieldDescription><![CDATA[project.name]]></fieldDescription></field>....<subreport> <reportElement x="0" y="72" width="554" height="98" isRemoveLineWhenBlank="true" backcolor="#FFCC99" uuid="4eb7e220-6758-465f-83c9-45cdfecc33eb"/> <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("persons")]]></dataSourceExpression> <subreportExpression><![CDATA["jsonPerson.jasper"]]></subreportExpression> </subreport>[/code]and in your subreport you should discard the usage of the JsonDataSource parameter.Hope this helps,Narcis Link to comment Share on other sites More sharing options...
nmau Posted June 18, 2014 Author Share Posted June 18, 2014 Thanks a lot. This does the trick! Link to comment Share on other sites More sharing options...
skumar_4 Posted February 14, 2019 Share Posted February 14, 2019 @Narcis in below soluation you have used subreportExpression <subreportExpression><![CDATA["jsonPerson.jasper"]]></subreportExpression> can you describe it what is jsonPerson.jasper Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now