weixin_33736048 2012-11-23 14:24 采纳率: 0%
浏览 83

JSON字符串转换为Highcharts

Im working on consuming REST response from web service that returns JSON string. The result response from AJAX is that i get alot of info and I use JQuery.parseJSON(result) to get the value from each JSON Tag.

From this result response I need to format it so it can fit to Highchart. Highchart need it to be formatted to:

series: [{
    name: 'Tokyo',
    data: [7.0, 6.9, 9.5, 14.5, 18.2],
    name: 'LA',
    data: [7.0, 6.9, 9.5, 14.5, 18.2]
}]

What is the best way for me to loop through the result data and create JSON string like Highchart wants to have? Here below is a start of solving this but I cant finish.

success: function (result) {
    var resultobj = jQuery.parseJSON(result);
    for (i = 0; i < resultobj.length; i++) {
        var objt = resultobj[i];
        // what TODO?????
        highchartresultarray.push(objt.name);
    }

Any suggestions?

  • 写回答

1条回答 默认 最新

  • 旧行李 2012-11-23 15:09
    关注

    I'm not familiar with Highcharts but based on the series array in the question (which I think has a small mistake because you have shown an object with has multiple properties with the same name - each name and data property should be encapsulated within their own respective objects from the looks of it) and the sample data you have provided, I have made a simple function which should give you the expected result - the premise of the function is to create an empty array for holding your objects, then iterate over the raw data putting the bits you need from the raw data into an empty object and pushing the populated object into the container array on each iteration - i don't explain things very well! so here's the code -

    function highchartdata(data) {
    
    highchartarray = [];
    
    $.each(data,function(i,v){
    
        var highchartobj = {};
    
        highchartobj.data = v.data.split(',');
    
        highchartobj.name = v.name;
    
        highchartarray.push(highchartobj);
    
    });
    
    return highchartarray;
    

    };

    and heres a working fiddle - http://jsfiddle.net/nuqJD/10/

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。