0

pass json data through evalJavaScript

asked 2016-09-20 10:20:26 +0800

takumi gravatar image takumi
12 3

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,

delete flag offensive retag edit

Comments

try \\\", I had such issue also that I needed double escaping

chillworld ( 2016-09-20 12:57:56 +0800 )edit

Thank 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

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-09-23 00:38:45 +0800

takumi gravatar image takumi
12 3

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,
            ....
            ....
link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
2 followers

RSS

Stats

Asked: 2016-09-20 10:20:26 +0800

Seen: 34 times

Last updated: Sep 23 '16

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More