0
{
  "data": [{
    "data": [{
      "Part": "1.75 L ICON (Glass)",
      "ProductionCounts": 1012620
    }, {
      "Part": "1.75 L Marg Mix (PET)",
      "ProductionCounts": 1003278
    }, {
      "Part": "1.75 L Authentics (PET)",
      "ProductionCounts": 457615
    }, {
      "Part": "1.0 L Margarita Mix / PET",
      "ProductionCounts": 660982
    }, {
      "Part": "other",
      "ProductionCounts": 1571985
    }]
  }, ],
  "dateArray": ["2011-01-01", "2011-02-01", "2011-03-01", "2011-04-01", "2011-05-01", "2011-06-01", "2011-07-01", "2011-08-01", "2011-09-01", "2011-10-01", "2011-11-01"],
  "xAxis": "Part",
  "yAxis": "ProductionCounts",
  "interestingMoments": []
}

我想访问 json 对象的值。每次获取 json 对象时,json 对象的字段名称都会不同,因此我将 xAxis 和 yAxis 中的字段名称分开发送。

当我尝试访问 jsonData.data[0].data[0].xAxis 时,它给了我 undefined 而不是 value。我不能像这样访问,因为字段名称每次都会不同,所以我试图通过变量 jsonData.data[0].data[0].Part 访问它。

4

5 回答 5

2

jsonData.data[0].data[0][jsonData.xAxis]jsonData.data[0].data[0][jsonData.yAxis]

http://jsfiddle.net/vsZkR/

于 2013-02-11T06:24:03.853 回答
1

你应该像这样使用json

var a = {
  "data": [{
    "data": [{
      "Part": "1.75 L ICON (Glass)",
      "ProductionCounts": 1012620
    }, {
      "Part": "1.75 L Marg Mix (PET)",
      "ProductionCounts": 1003278
    }, {
      "Part": "1.75 L Authentics (PET)",
      "ProductionCounts": 457615
    }, {
      "Part": "1.0 L Margarita Mix / PET",
      "ProductionCounts": 660982
    }, {
      "Part": "other",
      "ProductionCounts": 1571985
    }]
  }, ],
  "dateArray": ["2011-01-01", "2011-02-01", "2011-03-01", "2011-04-01", "2011-05-01", "2011-06-01", "2011-07-01", "2011-08-01", "2011-09-01", "2011-10-01", "2011-11-01"],
  "xAxis": "Part",
  "yAxis": "ProductionCounts",
  "interestingMoments": []
};

a.data[0].data[0][a.xAxis] // for access part values
a.data[0].data[0][a.yAxis]  // for acsess ProductionCounts values
于 2013-02-11T06:24:54.073 回答
0

对于 xAxis,这应该可以工作:

console.log( jsondata.xAxis );

见这里:jsFiddle

于 2013-02-11T06:22:51.987 回答
0

尝试这个 :

var data={
  "data": [{
    "data": [{
      "Part": "1.75 L ICON (Glass)",
      "ProductionCounts": 1012620
    }, {
      "Part": "1.75 L Marg Mix (PET)",
      "ProductionCounts": 1003278
    }, {
      "Part": "1.75 L Authentics (PET)",
      "ProductionCounts": 457615
    }, {
      "Part": "1.0 L Margarita Mix / PET",
      "ProductionCounts": 660982
    }, {
      "Part": "other",
      "ProductionCounts": 1571985
    }]
  }, ],
  "dateArray": ["2011-01-01", "2011-02-01", "2011-03-01", "2011-04-01", "2011-05-01", "2011-06-01", "2011-07-01", "2011-08-01", "2011-09-01", "2011-10-01", "2011-11-01"],
  "xAxis": "Part",
  "yAxis": "ProductionCounts",
  "interestingMoments": []
};
console.log(data.xAxis);
console.log(data.yAxis);
于 2013-02-11T06:23:53.090 回答
0

我会从冗余的“数据”对象中解开它。

http://jsfiddle.net/turiyag/cQNPm/

之后它看起来更干净,并且工作正常。您的 JSON 对象也未能通过 JSLint。在此处发布之前,请始终通过 JSLint 或类似产品运行您的代码。

log("jsonData.data[0][jsonData.yAxis]: " + jsonData.data[0][jsonData.yAxis]);
于 2013-02-11T06:34:45.843 回答