【问题标题】:Convert string representing object to javascript object将表示对象的字符串转换为 javascript 对象
【发布时间】:2013-02-21 13:21:30
【问题描述】:

我目前正在使用 jqplot 来显示一些图表并使用 php 准备 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 对象。

我尝试将它转换为一个对象,但没有成功,我没有太多使用 javascript 的经验。

有没有人有解决方案或提示?

谢谢

【问题讨论】:

  • 这不是 JSON。如果是 JSON,您可以使用 JSON.parse 解析它。我建议修复您的服务器进程以创建正确的 JSON。
  • 我已经更新了我的问题。我得到的 JSON 是正确的,我可以毫无问题地访问它的属性。我唯一的问题是我将选项作为字符串获取,并且我必须将其转换为对象。
  • 这真的很不安全,不应该这样做。因此,我将refusegive 你的功能在这里会有所帮助:)
  • 答案还是一样:创建一个合适的JSON对象,即"options"的值不是字符串而是对象。

标签: javascript string object


【解决方案1】:

一些现代浏览器支持将 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 对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2016-07-16
    • 1970-01-01
    • 2011-09-17
    相关资源
    最近更新 更多