将json对象转换为数组

时间:2015-08-26 18:09:31

标签: javascript json

我有一个像这样的JSON对象作为服务器响应返回。

{"names":["Kreisler","Kreisler","Kreisler"]}

我需要将此对象作为数组。

任何帮助?

4 个答案:

答案 0 :(得分:4)

使用JSON.parse()

var json = '{"names":["Kreisler","Kreisler","Kreisler"]}';
data = JSON.parse(json);
console.log(data['names']); // ["Kreisler", "Kreisler", "Kreisler"]

答案 1 :(得分:0)

考虑到响应数据位于response变量中,请使用:

var names = response["names"];

答案 2 :(得分:0)

var data = JSON.parse('{"names":["Kreisler","Kreisler","Kreisler"]}', function(k, v) {
  console.log(k); // log the current property name, the last is "".
  return v;       // return the unchanged property value.
});
console.log(data.names[0]);

答案 3 :(得分:0)

如果您在数据类型中使用fec1 <- c(9.53408021, 9.48993631, 9.44261613, 9.38585482, 9.31611858, 9.24743801, 9.19483663, 9.11690414, 9.05586032, 8.96781034, 8.85437896, 8.73956021, 8.62587246, 8.49758284, 8.41877774, 8.29737132, 8.02294327, 7.62604834, 7.32936532, 7.10006394, 6.87039464, 6.56332757, 6.27195705, 5.97089310, 5.69537808, 5.46074967, 5.19061116, 4.90749261, 4.61822473, 4.39635507, 3.96883450, 3.71230296, 3.43604354, 3.15150833, 2.82864081, 2.51857560, 2.27264470, 2.06785600, 1.86116599, 1.61452534, 1.41214847, 1.26673153, 1.15033592, 1.00646045, 0.83493055, 0.65399085, 0.47450499, 0.30408840, 0.18216540, 0.09006937, 0.00000000) 参数,并且您的回调参数称为响应,那么:

json