944,010 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 1411
  • JSP RSS
Nov 3rd, 2009
0

send a function in a JSON through java

Expand Post »
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:


JSP Syntax (Toggle Plain Text)
  1. {
  2. "aoColumns": [
  3. { "sTitle": "Column 1" },
  4. { "sTitle": "Column2" },
  5. { "sTitle": "Column3" },
  6. { "sTitle": "Column centered",
  7. "sClass": "center" },
  8. {"sTitle": "Column rendered",
  9. "sClass": "center",
  10. "fnRender": function(obj) {
  11. var sReturn = obj.aData[ obj.iDataColumn ]; if ( sReturn == "A" ) {
  12. sReturn = "<b>A</b>";
  13. }
  14. return sReturn;
  15. }
  16. }
  17. ]
  18. }

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
Similar Threads
Reputation Points: 11
Solved Threads: 3
Newbie Poster
santiagozky is offline Offline
18 posts
since Oct 2009
Nov 4th, 2009
0
Re: send a function in a JSON through java
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
Reputation Points: 36
Solved Threads: 57
Posting Whiz
Thirusha is offline Offline
355 posts
since Mar 2008
Nov 4th, 2009
0
Re: send a function in a JSON through java
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:
JSP Syntax (Toggle Plain Text)
  1. public JSONWriter function(String function) throws JSONException{
  2. if(this.mode=='o' ) {
  3. //this.push(null);
  4. this.append("function "+function);
  5. this.comma=true;
  6. return this;
  7. }
  8.  
  9. throw new JSONException("Misplaced function.");
  10. }
Reputation Points: 11
Solved Threads: 3
Newbie Poster
santiagozky is offline Offline
18 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: hi~need some help~
Next Thread in JSP Forum Timeline: Problem in running JSP





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC