2012-04-26 110 views
1

我正在处理Windows桌面应用程序,它包含System :: Windows :: Forms :: WebBrowser类型的对象。应用程序处理一些性能数据并显示包含各种图形(直方图,饼图等)的HTML页面。JSON.parse()默默地失败

其工作原理如下:

  • 应用程序生成
  • 应用程序的web浏览器对象导航本地临时路径上的HTML文件(例如,... /本地/路/ myfile.html)到所生成的文件:

    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)。

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

+0

如果只有第一个'alert'被称为那么问题是,JSON失败。有没有某种JS​​ON库可用和工作? – Kirstein 2012-04-26 09:40:51

+0

@Kirstein - 感谢您的回复。事实证明,我在应用程序中使用的WebBrowser是 jpen 2012-04-26 10:17:13

回答

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()); 
+0

+1获得解决方案。如果这解决了您的问题,请继续接受您的答案。所以所有人都知道这个问题是回答。 – Boro 2012-04-27 09:06:12