Search code examples
jsongwtdouble-quotesjsni

GWT - JSNI - passing json object


I have a json like below-

String sdata = "[{ name : "AAPL", data:[[1112832000000,43.56],[1112918400000,43.74],[1113177600000,41.92],[1113264000000,null],[1113350400000,null]], tooltip: {valueDecimals: 2}}]";

I am using JSNI to pass this from java to javascript.

public static native JavaScriptObject drawStock(Element element,String cname, String sdata) /*-{
       var chart = new $wnd.Highcharts.StockChart({
           chart : {
               renderTo : element
           },

           rangeSelector : {
               selected : 1
           },

           title : {
               text : cname
           },

           series : sdata    //=====this is where the json should be assigned.

           });

return chart;

}-*/;

The above native function does not work as expected because "i believe", the sdata variable has a double quotes around it and therefore is not thought of as a json object by javascript.

How can I pass the json object from java without the quotes to the javascript native interface function?


Solution

  • Use the safeEval function to evaluate safely in java.

    JavaScriptObject jsdata= JsonUtils.safeEval(sdata.toString());

    drawStock(element,cname, jsdata);