Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I need to generate JSON array in array data. I am able to get data from first array, however not able to get data from child array.

For example :-

[ { "id":"2012" "month": [ { "1": "Jan 2012"}, { "2": "Feb"} ] }, { "id":"2011" "month": [ { "1": "Jan 2011"}, { "2": "Feb"} ] } ]

I know how to call data from first array but stuck to get data from 2nd array, namely: month's data.

Please suggest me how I can get data from array in array

share|improve this question
4  
I think this belongs on Stack Overflow. Also, please show us what you have tried. – S.L. Barth Nov 21 '12 at 6:09

closed as off topic by Glenn Rogers, Jeff Mercado, Paul, sepp2k Nov 23 '12 at 15:09

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

First - your example is wrong. You should put comma after each id' value.

var jsonObject = {
  "id": "2012",
  "month": [
     {"1": "Jan 2012"},
     {"2": "Feb"}
  ]
}, {
  "id": "2011",
  "month": [
    {"1": "Jan 2011"},
    {"2": "Feb"}
  ]
}

And get data from json, like this:

var month = jsonObject[0].month[1];
var monthName = month["2"];

Another way - rename the keys in child array, for example {"m1": "Jan 2011"} and so on. And than you can get data form json object in a line ...

var monthName = json[0].month[1].m2;

Good luck.

share|improve this answer
you missed the square brackets for outer array, other than that answer is correct – almaz Nov 21 '12 at 11:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.