Jump to content

JsonDataSource problems


malkav.82l03

Recommended Posts

Hello guys,

i have this json String

{     "json":{        "Peoples":[           {              "name":"Joseph"         },         {              "name":"Ilda"         }      ]   }}[/code]

And this is my Java code:

InputStream is = new ByteArrayInputStream(json.getBytes("UTF-8"));JsonDataSource js = new JsonDataSource(is).subDataSource("json.Peoples");[/code]

on the second instruction i get always this exception:

Exception in thread "main" net.sf.jasperreports.engine.JRException: No node available. Iterate or rewind the data source.at net.sf.jasperreports.engine.data.JsonDataSource.subDataSource(JsonDataSource.java:455)[/code]

What is wrong?

The same problem if i don't do that instruction and pass the JsonDataSources without calling subDataSource method

these are my jrxml files:

MAIN:

<?xml version="1.0" encoding="UTF-8"?><jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"              xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"              name="master"              pageWidth="595"              pageHeight="842"              columnWidth="555"              leftMargin="20"              rightMargin="20"              topMargin="20"              bottomMargin="20"              uuid="357402bb-9c06-432f-9a61-8df8fc82c81d">    <property name="ireport.zoom" value="1.0"/>    <property name="ireport.x" value="0"/>    <property name="ireport.y" value="0"/>    <parameter name="SUBREPORT_DS" class="java.lang.Object"/>    <parameter name="REP_DS" class="java.lang.Object"/>    <background>        <band splitType="Stretch"/>    </background>    <title>        <band height="79" splitType="Stretch"/>    </title>    <pageHeader>        <band height="35" splitType="Stretch"/>    </pageHeader>    <columnHeader>        <band height="61" splitType="Stretch"/>    </columnHeader>    <detail>        <band height="125" splitType="Stretch">            <subreport>                <reportElement x="0" y="0" width="555" height="100" uuid="3f4dece2-5563-4df9-9cda-15623e0f6127"/>                <dataSourceExpression>                    <![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REP_DS}).subDataSource("json.Peoples")]]>                </dataSourceExpression>                <subreportExpression>                    <![CDATA[$P{SUBREPORT_DS}]]>                </subreportExpression>            </subreport>        </band>    </detail>    <columnFooter>        <band height="45" splitType="Stretch"/>    </columnFooter>    <pageFooter>        <band height="54" splitType="Stretch"/>    </pageFooter>    <summary>        <band height="42" splitType="Stretch"/>    </summary></jasperReport>[/code]

SUBREPORT:

<?xml version="1.0" encoding="UTF-8"?><jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"              xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"              name="slave"              pageWidth="555"              pageHeight="100"              columnWidth="555"              leftMargin="0"              rightMargin="0"              topMargin="0"              bottomMargin="0"              uuid="d001d4a5-f459-4ae5-b2ea-fda2f051cbab">    <property name="ireport.zoom" value="1.0"/>    <property name="ireport.x" value="0"/>    <property name="ireport.y" value="0"/>    <queryString language="json">        <![CDATA[]]>    </queryString>    <field name="Nome" class="java.lang.String"/>    <title>        <band height="79" splitType="Stretch">            <textField>                <reportElement x="34" y="22" width="100" height="20"                               uuid="4238a2f9-d7a1-4be0-8804-9c7c0ca30da6"/>                <textFieldExpression>                    <![CDATA[$F{Nome}]]>                </textFieldExpression>            </textField>        </band>    </title></jasperReport>[/code]
Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

I solved the problem of the exception using the default parameter REPORT_DATA_SOURCE and passing the json data source into the filling method instead of the JREmptyDataSource, but now I have another problem. It prints only the first Name and there isn't an iteration on the element. Is it a layout problem? How to make a iterable subreport?

Link to comment
Share on other sites

Ok now I solved the layout problem too.

Recap:

First: pass the JsonDataSource to the filler directly without calling subDataSet method:

JsonDataSource jds = new JsonDataSource(inputStream);JasperPrint report = JasperFillManager.fillReport(compiled,parameters, jds); [/code]

 

Second: use these expression in the main report to define the subreport

<subreport><reportElement x="0" y="0" width="555" height="100" uuid="3f4dece2-5563-4df9-9cda-15623e0f6127"/><dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("json.Peoples")]]></dataSourceExpression><subreportExpression><![CDATA[$P{SUBREPORT_OBJ}]]></subreportExpression></subreport>[/code]

The parameter SUBREPORT_OBJ is there because I'm compiling the subereport's .jrxml in my java application so I pass the subreport to the main report as an object. Normally we define a path for the .jasper of the subreport but in my project I do all the work at runtime.

Third: in the sub report jrxml file put the fields into the detail band and get rid of the <querystring> tag (I'm not sure of this, but because I'm doing the selection into the main report, I deleted that tag and It works.... )

These steps worked for me. I hope will be useful for other people.

 

 

Link to comment
Share on other sites

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...