jquery如何从json获取数组数组的值

时间:2014-04-17 13:00:03

标签: jquery json

我有这个json

{
  "default": [
    [
      1325876000000,
      0
    ],
    [
      1325876000000,
      0
    ],
    [
      1325876000000,
      0
    ],
    [
      1325876000000,
      0
    ]
  ],
  "direct": [
    [
      1328196800000,
      0
    ],
    [
      1328196800000,
      100
    ],
    [
      1328196800000,
      0
    ],
    [
      1328196800000,
      0
    ]
  ],
  "Sales": [
    [
      1330517600000,
      0
    ],
    [
      1330517600000,
      0
    ],
    [
      1330517600000,
      90
    ],
    [
      1330517600000,
      0
    ]
  ],
  "Support": [
    [
      1332838400000,
      0
    ],
    [
      1332838400000,
      0
    ],
    [
      1332838400000,
      0
    ],
    [
      1332838400000,
      0
    ]
  ]
}

我需要获取该json

中最后一个元素的第一个数组的值

在我的示例中,我需要获取1332838400000

我自己也做不到。遗憾。

2 个答案:

答案 0 :(得分:1)

获取每个元素的第一个元素中的最高元素:

var max = -Infinity;
for (var key in json) {
    var arr = json[key];
    if (arr[0][0] > max) {
        max = arr[0][0];
    }
}

答案 1 :(得分:-1)

var json = '{"default":[[1325876000000,0],[1325876000000,0],[1325876000000,0],[1325876000000,0]],"direct":[[1328196800000,0],[1328196800000,100],[1328196800000,0],[1328196800000,0]],"Sales":[[1330517600000,0],[1330517600000,0],[1330517600000,90],[1330517600000,0]],"Support":[[1332838400000,0],[1332838400000,0],[1332838400000,0],[1332838400000,0]]}';

var parser = $.parseJSON(json)

var key = keys(parser)
var len = key.length
var last = key[(len-1)]
alert(parser[last][0][0])