JSON.parse()默默地失败

时间:2012-04-26 09:32:29

标签: json parsing jqplot

我正在开发一个Windows桌面应用程序,它包含System :: Windows :: Forms :: WebBrowser类型的对象。应用程序处理一些性能数据并显示带有各种图形(直方图,饼图等)的HTML页面。

它的工作原理如下:

  • 应用程序在本地临时路径上生成HTML文件(例如,... / local / path / myfile.html)
  • 应用程序的WebBrowser对象导航到生成的文件:

    webBrowser1->导航( “... /本地/路径/ myfile.html”)

  • jQuery和其他一些插件(例如,jqPlot)用于创建各种图形,myfile.html在应用程序中呈现

myfile.html包含一段代码:

<div id="frame-time-histograms">
    <div id="frame-time-histogram-1">
        <div id="frame-time-histogram-1-target-plot" class="histogram-target" style="height:250px; width:900px;"></div>
        <div id="frame-time-histogram-1-controller-plot" class="histogram-controller" style="height:100px; width:900px;"></div>
        <script id="frame-time-histogram-1-data" class="histogram-data" type="text/plain">
            [{"type" : "Type 1", "shortest" : 12, "longest" : 74}, [[0, 0], [1, 12.632], [2, 16.619], [3, 16.592], [4, 16.664], [5, 16.586]]]
        </script>
    </div>
    <div id="frame-time-histogram-2">
        <div id="frame-time-histogram-2-target-plot" class="histogram-target" style="height:250px; width:900px;"></div>
        <div id="frame-time-histogram-2-controller-plot" class="histogram-controller" style="height:100px; width:900px;"></div>
        <script id="frame-time-histogram-2-data" class="histogram-data" type="text/plain">
            [{"type" : "Type 2", "shortest" : 24, "longest" : 19}, [[0, 0], [1, 20.145], [2, 20.091], [3, 20.301], [4, 20.109], [5, 20.087]]]
        </script>
    </div>
</div>

注意:这里我使用脚本标签作为直方图的数据容器。

我的JavaScript文件包含一段代码:

var histograms = $('div#frame-time-histograms');
histograms.children().each(function(index) {
    var histogramTargetId = $(this).find('div.histogram-target').attr('id');
    var histogramControllerId = $(this).find('div.histogram-controller').attr('id');

    var histogramData = JSON.parse($(this).find('script.histogram-data').html());

然而,JSON.parse()似乎没有做任何事情。我在此行之前和之后添加了alert(“hello”),但只执行了第一行。

如果我转到临时路径并双击myfile.html,JSON.parse()工作正常。我可以在我的网络浏览器中看到所有图表(Chrome,FF和IE)。

有人能告诉我我做错了吗?

1 个答案:

答案 0 :(得分:1)

我刚注意到jqPlot提供了json2.js文件的版本:

...\plugins\jqplot.json2.js

要解决我遇到的问题,我已将此js文件包含在myfile.html中并更改了

JSON.parse($(this).find('script.histogram-data').html());

$.jqplot.JSON.parse($(this).find('script.histogram-data').html());