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": []
}

I want to access the value of json object. Field name of json object will be different each time I am getting the json object so I am sending the field name in xAxis and yAxis as separate.

When I am trying to access the jsonData.data[0].data[0].xAxis , it is giving me undefined instead of value. I can't access like this as field name will be different each time so I am trying to access it through variable jsonData.data[0].data[0].Part .

5 Answers 5

2

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

http://jsfiddle.net/vsZkR/

Sign up to request clarification or add additional context in comments.

Comments

1

you should use json like this

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

Comments

0

for xAxis, just this should work:

console.log( jsondata.xAxis );

See here: jsFiddle

Comments

0

Try this :

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);

Comments

0

I would unwrap it from the redundant "data" object.

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

It looks cleaner afterward, and works just fine. Your JSON object also failed JSLint. Always run your code through JSLint or a similar product before posting here.

log("jsonData.data[0][jsonData.yAxis]: " + jsonData.data[0][jsonData.yAxis]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.