Hello everyone,

first of all, I dont know if this should be in the java or the jsp forum. I put it here since is a web related question.

I am making a report module for a web page using java. For this I will use a jquery plugin called datatables (I'm not sure if it's allowed to link to it). The point is that I need to generate a json string dynamically in order to get my reports.

The problem is that along with my data I will also have to send some anonymous functions in the json. I have used some libraries to convert java objects to a json string but nothing on how to send a function.

The jsons will look something like this:

"aoColumns": [	
	{ "sTitle": "Column 1" },
	{ "sTitle": "Column2" },
	{ "sTitle": "Column3" },
	{ "sTitle": "Column centered", 
                     "sClass": "center" },
	{"sTitle": "Column rendered",
                  "sClass": "center",
	  "fnRender": function(obj) {
		var sReturn = obj.aData[ obj.iDataColumn ];		if ( sReturn == "A" ) {
			sReturn = "<b>A</b>";
	        return sReturn;

I know I can generate it manually as a string, but that seems quite complex and I would like to keep that as a las resort.

Anyone knows a library that can generate javascript functions from java?

Thank you for any help you can provide me. This place looks quite nice btw

8 Years
Discussion Span
Last Post by santiagozky

I am not sure sure if this will help, but i had done something a similar, what i did is create a json string using javascript on my jsp page after button was clicked, then sent the json string using ajax to a servlet. In the servlet i used request.getParameter to get the json string, then looped over it using the libraries from the json.org site


I think I have a decent solution. I got the libraries from json.org and modified the JSonWriter class to add a function method that will receive a string with the javascript function. I still have to generate the function by hand, but It's better than nothing.

It's quite simple, the string is composed of the arguments and the function body:

public JSONWriter function(String function) throws JSONException{
    	if(this.mode=='o' ) {
    		this.append("function "+function);
    	return this;	
    	 throw new JSONException("Misplaced function.");
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.