send a function in a JSON through java

Reply

Join Date: Oct 2009
Posts: 18
Reputation: santiagozky is an unknown quantity at this point 
Solved Threads: 3
santiagozky santiagozky is offline Offline
Newbie Poster

send a function in a JSON through java

 
0
  #1
21 Days Ago
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:


  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 312
Reputation: Thirusha is an unknown quantity at this point 
Solved Threads: 48
Thirusha's Avatar
Thirusha Thirusha is offline Offline
Posting Whiz
 
0
  #2
20 Days Ago
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
Life... Is a Moment
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: santiagozky is an unknown quantity at this point 
Solved Threads: 3
santiagozky santiagozky is offline Offline
Newbie Poster
 
0
  #3
20 Days Ago
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:
  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. }
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC