2013-02-21 140 views
0

我目前使用jqplot來顯示一些圖表並使用php編寫javascript代碼。將表示對象的字符串轉換爲javascript對象

在我的圖表之一中,我使用Ajax獲取新圖表。對於這一點,我得到的價值觀和圖表選項列表中選擇一個JSON數組像這樣的字符串:

這是JSON我得到:

{"idGraphe":"bar_chart_5","conditions":"[]","data":"[[[1,80423],[2,62634],[3,70625],[4,72187],[5,72739],[6,70078],[7,72751],[8,74300],[9,75550],[10,72482],[11,70971],[12,77579]],[[1,73386],[2,70068],[3,85018],[4,69761],[5,75317],[6,68240],[7,72487],[8,74716],[9,74340],[10,75012],[11,74800],[12,83105]]]","options":"{series : [{label : 'consommationtotale Pilote1' , yaxis : 'yaxis' },{label : 'consommationtotale Pilote2' , yaxis : 'yaxis' }] , seriesDefaults : {renderer : $.jqplot.BarRenderer , rendererOptions : {barWidth : null} } , cursor : {show : true , zoom : true, showTooltip : false}, highlighter : {showTooltip : true , tooltipAxes : 'both' , tooltipContentEditor : getPointInfo , show : true} , title : 'Bar chart', legend : {show : true , renderer: $.jqplot.EnhancedLegendRenderer}, axes : {xaxis:{ tickRenderer:$.jqplot.CanvasAxisTickRenderer , renderer: $.jqplot.CategoryAxisRenderer,pad : 0 , label : 'mois' , labelRenderer: $.jqplot.CanvasAxisLabelRenderer}, yaxis:{ label : 'consommationtotale' , labelRenderer: $.jqplot.CanvasAxisLabelRenderer }}}"} 

這是我有問題的一部分:

"{series : [{label : 'consommationtotale Pilote1' , yaxis : 'yaxis' },{label : 'consommationtotale Pilote2' , yaxis : 'yaxis' }] , seriesDefaults : {renderer : $.jqplot.BarRenderer , rendererOptions : {barWidth : null} } , cursor : {show : true , zoom : true, showTooltip : false}, highlighter : {showTooltip : true , tooltipAxes : 'both' , tooltipContentEditor : getPointInfo , show : true} , title : 'Bar chart', legend : {show : true , renderer: $.jqplot.EnhancedLegendRenderer}, axes : {xaxis:{ tickRenderer:$.jqplot.CanvasAxisTickRenderer , renderer: $.jqplot.CategoryAxisRenderer,pad : 0 , label : 'mois' , labelRenderer: $.jqplot.CanvasAxisLabelRenderer}, yaxis:{ label : 'consommationtotale' , labelRenderer: $.jqplot.CanvasAxisLabelRenderer }}}" 

我的問題是,爲了繪製圖表與正確的選項,我需要通過這個作爲一個JavaScript對象。而且,我無法想象如何將字符串轉換爲相應的javascript對象。

我試圖把它作爲一個對象,但沒有成功,我沒有這麼多ex ^使用JavaScript。

有沒有人會有解決方案或提示?

感謝

+4

這不是JSON。如果它是JSON,你可以用'JSON.parse'解析它。我建議您修復您的服務器進程以創建適當的JSON。 – 2013-02-21 13:22:24

+0

這應該做到這一點:http://www.php.net/manual/en/function.json-encode.php – filmor 2013-02-21 13:25:06

+0

我已經更新了我的問題。我得到的JSON是正確的,我沒有問題來訪問它的屬性。我唯一的問題是,我將選項作爲字符串獲取,並且必須將其轉換爲對象。 – user2095568 2013-02-21 13:30:22

回答

0

一些現代的瀏覽器都支持JSON解析成一個本地對象:

var x = "{series : [{label : 'consommationtotale Pilote1' , yaxis : 'yaxis' },{label : 'consommationtotale Pilote2' , yaxis : 'yaxis' }] , seriesDefaults : {renderer : $.jqplot.BarRenderer , rendererOptions : {barWidth : null} } , cursor : {show : true , zoom : true, showTooltip : false}, highlighter : {showTooltip : true , tooltipAxes : 'both' , tooltipContentEditor : getPointInfo , show : true} , title : 'Bar chart', legend : {show : true , renderer: $.jqplot.EnhancedLegendRenderer}, axes : {xaxis:{ tickRenderer:$.jqplot.CanvasAxisTickRenderer , renderer: $.jqplot.CategoryAxisRenderer,pad : 0 , label : 'mois' , labelRenderer: $.jqplot.CanvasAxisLabelRenderer}, yaxis:{ label : 'consommationtotale' , labelRenderer: $.jqplot.CanvasAxisLabelRenderer }}}"; 
var result = JSON.parse(x); 

對於不支持它的瀏覽器,你可以從json.org下載json2.js爲安全地解析JSON對象。