Jump to content
Changes to the Jaspersoft community edition download ×

How to deal with JSON-List as JsonDataSource


nmau
Go to solution Solved by narcism,

Recommended Posts

Hello Everybody

I 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 Code

InputStream stream = new ByteArrayInputStream(body.getBytes("UTF-8"));JRjsonsource = new JsonDataSource(stream);ModelAndView model = new ModelAndView(                templatename + "/" + templatename + ".jrxml",                createParameterMap(JRjsonsource));[/code]

 

JRXML

The project.name works

But 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

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

  • 2 months later...
  • Solution

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

  • 4 years later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...