-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello, I'm fighting this issue for a couple of days. Please help me !
I put the following code to pass JSON data to javascript function in zul.
String test_arg = "do_test('{\"clock\":[1422324111000,1422324411000,1422324711000,1422325011000,1422325311000],\"eth0in\":[2752,2704,2576,2592,2848]}')";
But, my javascript function failed. I add "alert( JSON.stringify( jdata));" in the script. Then I found that backslash is added before double quotation as below.
{\"clock\":[1422324111000,1422324411000,1422324711000,1422325011000,1422325311000],\"eth0in\":[2752,2704,2576,2592,2848]};
When I create JSON data in Javascript, then the alert does not show backslash, and it works fine.
How can I remove the backslash when I use Client.evalJavaScript ?
Thank you,
At first, I implement server side and zul javascript as below. Then, c3.generate() failed, and the alert() shows message with backslach like a "{\"clock\":[142232".
Server side code:
public void putJSON() { JSONArray clockArray = new JSONArray(); JSONArray valArray = new JSONArray(); Integer aClock[] = { 14324111, 14223244, 1422324}; Integer aValue[] = { 2752, 2704, 2576}; int nMax = aClock.length; for( int i = 0; i < nMax; ++i){ clockArray.add( aClock[ i]); valArray.add( aValue[ i]); } JSONObject json = new JSONObject(); json.put( "clock", clockArray); json.put( "eth0in", valArray); Clients.evalJavaScript( "do_hook('" + json.toJSONString() + "');"); }
zul javascript function
function do_hook( jdata) { alert( "debug = " + JSON.stringify( json)); var chart = c3.generate( { bindto: d3.select( '#chart'), data: { x: 'clock', y: 'eth0in', json: jdata, .... ....
Root cause is that argument of do_hook() was String but, I treat it as JSON structure. So, I changed do_hook() function as below and it works.
function do_hook( jdata_arg) { alert( "debug = " + JSON.stringify( json)); jdata = JSON.parse( jdata_arg); // add this line var chart = c3.generate( { bindto: d3.select( '#chart'), data: { x: 'clock', y: 'eth0in', json: jdata, .... ....
Asked: 2016-09-20 10:20:26 +0800
Seen: 41 times
Last updated: Sep 23 '16
Facing issue in masking on phone [closed]
coerceFromString_ override no longer works from 6.5.1 [closed]
Execute Javascript on component visible
Is there a way to resolve view model properties as input to client side javascripts?
scrollbar listner for 2 grid [closed]
Theme javascript wpd file not found
try
chillworld ( 2016-09-20 12:57:56 +0800 )edit\\\"
, I had such issue also that I needed double escapingThank you very much for your advice.
I found that it was my misunderstanding. At first, I though that Clients.evalJavaScript() pass JSON structure. But it was just string. I add JSON.parse( json) in javascript function, and it worked.
I'm sorry for the confusion.
takumi ( 2016-09-20 23:53:11 +0800 )edit@takumi what step you did for this issue to solve.Can you post as a answer then it will help others.
hswain ( 2016-09-21 04:18:46 +0800 )edit