Create pdf report with ireport in netbeans with JSON as datasource

3

I am new programming in java and I have encountered a small problem, I am generating a pdf report taking as datasource a JSON file, but I have not managed to pass the JSON to JasperFillManager.fillReport to generate the pdf, if I charge it through a bd, it works, but with the JSON NO. This is the code I have:

// clase que carga json
public class JsonParsing implements Connection {

   public static void main(String[] args) {

      try {
          JSONParser jsonParser = new JSONParser();
          File file = new File("web\WEB-INF\empleados.json");
          Object object = jsonParser.parse(new FileReader(file));
          //System.out.println(object); 

      } catch (Exception ex) {
        ex.printStackTrace();
      }
   }
}


// servlet que genera reporte.

public class ServletReport extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException, JRException {

        try{
            response.setContentType("application/pdf");
            JsonParsing metodo = new JsonParsing();

            ServletOutputStream out = response.getOutputStream();        
            JasperPrint jasperPrint = JasperFillManager.fillReport(getServletContext().getRealPath("WEB-INF/principal.jasper"), new HashMap<String, Object>(), metodo);
            JRExporter exporter = new JRPdfExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
            exporter.exportReport();
        }
        catch(IOException e){
        }
    }
}

Thank you.

    
asked by Victor 14.10.2016 в 21:49
source

1 answer

1

I have already managed to capture the json , by means of a url in the following way:

UserClass mapInf = mapper.readValue(new URL("https://api.myjson.com/bins/xxxxx"), 
                   UserClass.class);

parametros.put("infoEmpNmEmpresa", mapInf.getInfoEmpNmEmpresa());
parametros.put("tipoIdentificacionEmp", mapInf.getTipoIdentificacionEmp());
parametros.put("infoEmpNumeroIdentificacion", mapInf.getInfoEmpNumeroIdentificacion());
    
answered by 27.10.2016 в 22:34