转换器json到二维数组

时间:2012-10-09 06:19:59

标签: jquery json

我的json数据:

{
   "d":[
      {
         "__type":"DashBoardControl.Info.DashBoardSettingInfo",
         "VisitTime":"183",
         "Browser":"Firefox"
      },
      {
         "__type":"DashBoardControl.Info.DashBoardSettingInfo",
         "VisitTime":"28",
         "Browser":"Chrome"

      },
      {
         "__type":"DashBoardControl.Info.DashBoardSettingInfo",
         "VisitTime":"2",
         "Browser":"IE"
      },
      {
         "__type":"DashBoardControl.Info.DashBoardSettingInfo",
         "VisitTime":"1",
         "Browser":"Opera"
      }
   ]
}

我尝试像这样在二维数组中创建这些数据。但我失败了。

                        var BrowserWiseVisit = [];
                        if (msg.d.length > 0) {
                            var arrVisit = [];
                            $.each(msg.d, function (index, item) {

                                arrVisit = [];
                                arrVisit.push(item.Browser);
                                arrVisit.push(item.VisitTime);
                                BrowserWiseVisit.push(arrVisit);

                            });


                        }

我想要一个像这种格式的数据

[['Firefox', 183], ['Chrome', 28], ['IE', 2], ['Opera', 1]];

使用此http://www.jqplot.com/此插件。 感谢。

3 个答案:

答案 0 :(得分:3)

看来你正在做http://www.jqplot.com/

你想要数据 [[' Firefox',183],[' Chrome',28],[' IE',2],[' Opera', 1]];

简单地在你的js中将visitTime数据解析为int。
以下是演示:http://jsfiddle.net/Simplybj/HCNL5/

var BrowserWiseVisit = [];
                        if (msg.d.length > 0) {
                            var arrVisit = [];
                            $.each(msg.d, function (index, item) {

                                arrVisit = [];
                                arrVisit.push(item.Browser);
                                arrVisit.push(parseInt(item.VisitTime));
                                BrowserWiseVisit.push(arrVisit);

                            });
                    }

答案 1 :(得分:0)

试试这个:

var BrowserWiseVisit = [];
if (msg.d.length > 0) {
   var arrVisit = [];
   $.each(msg.d, function (index, item) {
      arrVisit = [item.Browser,item.VisitTime];
      BrowserWiseVisit.push(arrVisit);
   });
}

答案 2 :(得分:-2)

这应该做:

var sampleData = [], results = d;
for (var i = 0, len = results.length; i < len; i++) {
    var result = results[i];
    sampleData.push({ browser: result.Browser, visitTime: result.VisitTime});
}