代码之家  ›  专栏  ›  技术社区  ›  Syed Sarek

双引号的Javascript json数据读取问题

  •  0
  • Syed Sarek  · 技术社区  · 6 年前

    我将json数据传递给googlechart的javascript。

    ["Md. Aslam",170972.7,"gold"]
    

    但是在google图表中javascript显示

    ["JIANGSU LTD",170972.7,"gold"]
    

    这是展示 " 关于 " 正如我所说。我怎么解决这个问题?

    def data1 = table.executeQuery("select name, point from table") as JSON
    render(view: "/report", model: [data1: data1])
    

    我的报告.gsp(谷歌图表视图):

    <script type="text/javascript">
            google.charts.load('current', {'packages':['corechart']});
            google.charts.setOnLoadCallback(drawVisualization);
    
            function drawVisualization() {
                google.charts.load("current", {packages:['corechart']});
                google.charts.setOnLoadCallback(drawChart);
                function drawChart() {
                    var data = google.visualization.arrayToDataTable([
                        [ 'Element', "Density", { role: "style" } ],
                        ${data1}
                    ]);
    
                    var view = new google.visualization.DataView(data);
                    view.setColumns([0, 1,
                        { calc: "stringify",
                            sourceColumn: 1,
                            type: "string",
                            role: "annotation" },
                        2]);
    
                    var options = {
                        title: "Density of Precious Metals, in g/cm^3",
                        width: 600,
                        height: 400,
                        bar: {groupWidth: "95%"},
                        legend: { position: "none" },
                    };
                    var chart = new google.visualization.ColumnChart(document.getElementById("ord_variation"));
                    chart.draw(view, options);
            }}
        </script>
    

    我使用的是groovy/grails3

    2 回复  |  直到 6 年前
        1
  •  0
  •   Syed Sarek    6 年前

    最后我得到了我的解决方案普惠制报告

    <g:applyCodec encodeAs="none">
        ${data1};
    </g:applyCodec>
    
        2
  •  0
  •   Rahul Mahadik    6 年前

    按如下方式更改代码:

    <script type="text/javascript">
    
    var j = "${data}"; // data is your json data came from controller
        var result = JSON.parse((j.split("&quot;").join('"')).split("=").join(':')); //use result object
    
    </script>
    

    希望这对你有帮助