从JSON创建数组

时间:2015-09-30 21:45:49

标签: javascript jquery arrays json google-visualization

我有这个JSON代码:

{"data":[{"vreme":"2015-09-29 19:07:23","cena":1100,"utovar":"2015-09-30","istovar":"2015-10-01","poruka":""},{"vreme":"2015-09-29 18:41:06","cena":3453,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":"sve ukljuceno u cenu"},{"vreme":"2015-09-29 16:32:57","cena":1200,"utovar":"","istovar":"","poruka":"+magacin"},{"vreme":"2015-09-29 17:43:58","cena":3400,"utovar":"2015-09-29","istovar":"2015-09-30","poruka":"+skladiste + niza cena + tacan da tum utovara"},{"vreme":"2015-09-29 19:07:31","cena":1100,"utovar":"2015-09-30","istovar":"2015-10-01","poruka":""},{"vreme":"2015-09-29 19:44:12","cena":1099,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":"niza ponuda"},{"vreme":"2015-09-29 20:10:02","cena":520,"utovar":"2015-09-29","istovar":"2015-09-30","poruka":"+magacin"},{"vreme":"2015-09-29 21:05:22","cena":500,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":""},{"vreme":"2015-09-29 21:09:21","cena":499,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":"All Invlusive"},{"vreme":"2015-09-29 21:10:42","cena":450,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":"Ukljucen rucak za 2 osobe"},{"vreme":"2015-09-29 21:12:40","cena":440,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":"I razvoz po gradu sa manjim kombijem"},{"vreme":"2015-09-29 21:14:08","cena":7500,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":"Prevoz u pozlacenim posudama od srebra"},{"vreme":"2015-09-29 21:16:36","cena":999,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"Electro Truck Shipment - Clima Cargo"},{"vreme":"2015-09-29 21:18:42","cena":439,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"10 u pola sa lukom i 2 piva"},{"vreme":"2015-09-29 21:19:46","cena":438,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"Mica shiping&pushing"},{"vreme":"2015-09-29 21:21:16","cena":430,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"Celo selo šmr\u010de belo - TRANS"},{"vreme":"2015-09-29 21:21:47","cena":437,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":""},{"vreme":"2015-09-29 21:21:57","cena":400,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":""},{"vreme":"2015-09-29 21:22:59","cena":425,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"https:\/\/www.youtube.com\/watch?v=X3YIzdTGbX8"},{"vreme":"2015-09-29 21:25:55","cena":399,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"Free of charge two immigrants in blind box under load space"},{"vreme":"2015-09-29 21:26:23","cena":380,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":"moze na odlozeno"},{"vreme":"2015-09-29 21:28:47","cena":350,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"+ dozen immigrants with free food & beer with comfort seater "},{"vreme":"2015-09-29 21:30:24","cena":330,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"Shipment with Space X rocket directly to Mars + back"},{"vreme":"2015-09-29 22:06:01","cena":320,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"Loading and shiping"},{"vreme":"2015-09-29 22:09:26","cena":0,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":"a taman sam hteo da ponudim besplatno"},{"vreme":"2015-09-29 22:13:42","cena":-100,"utovar":"2015-09-29","istovar":"2015-10-30","poruka":""},{"vreme":"2015-09-29 22:26:13","cena":-9999,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"Truck Track rules"},{"vreme":"2015-09-30 00:02:06","cena":-10000,"utovar":"2015-09-30","istovar":"2015-09-30","poruka":""},{"vreme":"2015-09-30 00:07:53","cena":-10001,"utovar":"2015-09-30","istovar":"2015-09-30","poruka":"cvrc:D"},{"vreme":"2015-09-30 08:34:53","cena":1000,"utovar":"2015-09-30","istovar":"2015-09-30","poruka":""}]}

此外,我有这个谷歌可视化API代码: https://jsfiddle.net/Lus7z2q6/

所以我如何将我的JSON文件转换为如下所示:

 data.addRows([
        [0, 0],   [1, 100],  [2, 23],  [3, 17],  [4, 18],  [5, 9],
        [6, 11],  [7, 27],  [8, 33],  [9, 40],  [10, 32], [11, 35],
        [12, 30], [13, 40], [14, 42], [15, 47], [16, 44], [17, 48],
        [18, 52], [19, 54], [20, 42], [21, 55], [22, 56], [23, 57],
        [24, 60], [25, 50], [26, 52], [27, 51], [28, 49], [29, 53],
        [30, 55], [31, 60], [32, 61], [33, 59], [34, 62], [35, 65],
        [36, 62], [37, 58], [38, 55], [39, 61], [40, 64], [41, 65],
        [42, 63], [43, 66], [44, 67], [45, 69], [46, 69], [47, 70],
        [48, 72], [49, 68], [50, 66], [51, 65], [52, 67], [53, 70],
        [54, 71], [55, 72], [56, 73], [57, 75], [58, 70], [59, 68],
        [60, 64], [61, 60], [62, 65], [63, 67], [64, 68], [65, 69],
        [66, 70], [67, 72], [68, 75], [69, 80]
      ]);

其中数组为[data.vreme[0],data.cena[0]] ,[data.vreme[1],data.cena[1]]...

如何将我的JSON转换为这个数组,或者如何以这种格式使用JSON for google visualization api?

4 个答案:

答案 0 :(得分:0)

这样的东西? jsfiddle demo

var newArray = [], length = JSONstring.data.length;
for (i = 0; i < length; i++) { 
  newArray.push([i, JSONstring.data[i].cena]);
}

我相信&#34; vreme&#34;是日期,但是示例中小数组中的第一个数字总是递增1,因此它不是&#34; vreme&#34;你想要的,但是数组的索引号。

答案 1 :(得分:-1)

使用jquery,你可以遍历你的json对象并创建一个新的数组数组,如下所示:

var arr = [];
$.each(d.data, function(k, v){
    arr.push([v.vreme, v.cena]);
});

然后你只需添加你的数组而不是谷歌中的烘焙数据:

data.addRows(arr);

当然,如果您正在处理JSON 字符串,您需要先解析它:

var jsonStr = `{".. pretend this is the object in the example .."}`,
    d = JSON.parse(jsonStr);

答案 2 :(得分:-1)

您的数据:

{"data":[
        {"vreme":"2015-09-29 19:07:23","cena":1100, ... },
        {"vreme":"2015-09-29 18:41:06","cena":3453, ... },
        ...
    ]
}

分解你的json数据(不是代码),你有一个数组的对象:

"data" : [ 
    {myObject1}, {myObject2}, ... 
]

你的例子是

[data.vreme[0], data.cena[0]], 
[data.vreme[1], data.cena[1]], ...

这很接近,但你需要的是

[data[0].vreme, data[0].cena], 
[data[1].vreme, data[1].cena], ...

最终你需要一个数组数组:

data.addRows([
    [0, 0],   [1, 100],  [2, 23],  [3, 17],  [4, 18],  [5, 9],
    ...
    [66, 70], [67, 72], [68, 75], [69, 80]
  ]);

data.addRows([
    [data[0].vreme, data[0].cena],   [data[1].vreme, data[1].cena],
    ...
    [data[68].vreme, data[68].cena]
  ]);

所以你必须将每个对象{}的数据推送到一个数组中,并将每个小数组推送到一个容器数组中。

答案 3 :(得分:-1)

在此我插入了“data.vreme [0]”,以Date.prototype.valueOf()

为单位转换

var JSONstring = {"data":[{"vreme":"2015-09-29 09:07:23","cena":1100,"utovar":"2015-09-30","istovar":"2015-10-01","poruka":""},{"vreme":"2015-09-29 12:41:06","cena":3453,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":"sve ukljuceno u cenu"},{"vreme":"2015-09-29 16:32:57","cena":1200,"utovar":"","istovar":"","poruka":"+magacin"},{"vreme":"2015-09-29 17:43:58","cena":3400,"utovar":"2015-09-29","istovar":"2015-09-30","poruka":"+skladiste + niza cena + tacan da tum utovara"},{"vreme":"2015-09-29 19:07:31","cena":1100,"utovar":"2015-09-30","istovar":"2015-10-01","poruka":""},{"vreme":"2015-09-29 19:44:12","cena":1099,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":"niza ponuda"},{"vreme":"2015-09-29 20:10:02","cena":520,"utovar":"2015-09-29","istovar":"2015-09-30","poruka":"+magacin"},{"vreme":"2015-09-29 21:05:22","cena":500,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":""},{"vreme":"2015-09-29 21:09:21","cena":499,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":"All Invlusive"},{"vreme":"2015-09-29 21:10:42","cena":450,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":"Ukljucen rucak za 2 osobe"},{"vreme":"2015-09-29 21:12:40","cena":440,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":"I razvoz po gradu sa manjim kombijem"},{"vreme":"2015-09-29 21:14:08","cena":7500,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":"Prevoz u pozlacenim posudama od srebra"},{"vreme":"2015-09-29 21:16:36","cena":999,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"Electro Truck Shipment - Clima Cargo"},{"vreme":"2015-09-29 21:18:42","cena":439,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"10 u pola sa lukom i 2 piva"},{"vreme":"2015-09-29 21:19:46","cena":438,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"Mica shiping&amp;pushing"},{"vreme":"2015-09-29 21:21:16","cena":430,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"Celo selo &scaron;mr\u010de belo - TRANS"},{"vreme":"2015-09-29 21:21:47","cena":437,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":""},{"vreme":"2015-09-29 21:21:57","cena":400,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":""},{"vreme":"2015-09-29 21:22:59","cena":425,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"https:\/\/www.youtube.com\/watch?v=X3YIzdTGbX8"},{"vreme":"2015-09-29 21:25:55","cena":399,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"Free of charge two immigrants in blind box under load space"},{"vreme":"2015-09-29 21:26:23","cena":380,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":"moze na odlozeno"},{"vreme":"2015-09-29 21:28:47","cena":350,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"+ dozen immigrants with free food &amp; beer with comfort seater "},{"vreme":"2015-09-29 21:30:24","cena":330,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"Shipment with Space X rocket directly to Mars + back"},{"vreme":"2015-09-29 22:06:01","cena":320,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"Loading and shiping"},{"vreme":"2015-09-29 22:09:26","cena":0,"utovar":"2015-09-29","istovar":"2015-09-29","poruka":"a taman sam hteo da ponudim besplatno"},{"vreme":"2015-09-29 22:13:42","cena":-100,"utovar":"2015-09-29","istovar":"2015-10-30","poruka":""},{"vreme":"2015-09-29 22:26:13","cena":-9999,"utovar":"2015-09-15","istovar":"2015-09-30","poruka":"Truck Track rules"},{"vreme":"2015-09-30 00:02:06","cena":-10000,"utovar":"2015-09-30","istovar":"2015-09-30","poruka":""},{"vreme":"2015-09-30 00:07:53","cena":-10001,"utovar":"2015-09-30","istovar":"2015-09-30","poruka":"cvrc:D"},{"vreme":"2015-09-30 08:34:53","cena":1000,"utovar":"2015-09-30","istovar":"2015-09-30","poruka":""}]}

var newArray = [];
for (i = 0; i < JSONstring.data.length; i++) { 
    var dt = new Date(JSONstring.data[i].vreme).valueOf(); //console.log(dt);
    newArray[i] = [dt, JSONstring.data[i].cena];
}
google.load('visualization', '1', {packages: ['corechart', 'line']});
google.setOnLoadCallback(drawBackgroundColor);

function drawBackgroundColor() {
      var data = new google.visualization.DataTable();
      data.addColumn('number', 'X');
      data.addColumn('number', 'Dogs');

      data.addRows(newArray);

      var options = {
        hAxis: {
          title: 'vreme'
        },
        vAxis: {
          title: 'cena'
        },
        backgroundColor: '#f1f8e9'
      };

      var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
      chart.draw(data, options);
    }
  <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  <div id="chart_div"></div>