2

I have a data in json format getting from PHP script the data is coming in following format as follows:

[{
"type":"checkbox",
"grid-name":"Sports",
"values":["Cricket","Football"],
 "input":[{"Cricket":2},{"Football":1}]
},
{"type":"checkbox",
"grid-name":"Hobbies",
"values":["Playing Chess","Swimming"],
"input":[{"Playing Chess":1},{"Swimming":2}]
},
{"type":"radiobutton",
"grid-name":"Gender",
 "values":["Male","Female"],
"input":[{"Male":3},{"Female":0}]
},
{"type":"radiobutton",
"grid-name":"Citizen",
"values":["Indian","NRI"],
"input":[{"Indian":3},{"NRI":0}]
},
{"type":"number",
"grid-name":"Age",
"input":["24","23","23"]
},
 {"type":"select",
"grid-name":"City",
"values":["Satara","New york","Korea"],
"input":[{"Satara":1},{"New york":1},{"Korea":1}]
}]

i want to capture the values & input array. How to access through nested array?

cn007b
  • 16,596
  • 7
  • 59
  • 74
Shaggie
  • 1,799
  • 7
  • 34
  • 63
  • If you request the das with jQuery, and the server sends the correct information, then the JSON formatted String will be automatically parsed into a JavaScript Object. The Resulting array can be iterated using a normal `for` loop, or the `forEach` function provided by an Array. – t.niese Apr 16 '15 at 06:23
  • Where do you need to capture it? There are many functions like `each()`/`map()` available. – Shaunak D Apr 16 '15 at 06:25
  • probably i need to capture value & input data where value data is simple an array at the place of value whereas input is array inside array at the place of value – Shaggie Apr 16 '15 at 06:34

2 Answers2

1

jQuery:

$.each(yourObject, function( index, value ) {
    console.log(value.values);
    console.log(value.input);
});

Native js (but better don't use it, accordingly to this):

for (index in yourObject) {
    console.log(yourObject[index].values);
    console.log(yourObject[index].input);
}

Native js, another example:

for (var i = 0; i < yourObject.length; i++) {
    console.log(yourObject[i].values);
    console.log(yourObject[i].input);
}
Community
  • 1
  • 1
cn007b
  • 16,596
  • 7
  • 59
  • 74
  • 1
    The structure shown by the OP is an Array, and the OP also asks about Array, so you should not suggest a `for-in` loop or at least make clear that this should only be used for Objects ([Why is using “for…in” with array iteration such a bad idea?](http://stackoverflow.com/questions/500504)) – t.niese Apr 16 '15 at 06:33
  • @t.niese agreed.. `Foreach` preffered. – Tushar Gupta Apr 16 '15 at 06:41
  • inside your each you should write one more... `$.each(yourObject, function( index, value ) { $.each(value.input, function( i, v ) { /* Here code ... */ }); });` – cn007b Apr 16 '15 at 06:59
  • Added native js another example. I know, that it isn't actual already, but just for example... – cn007b Apr 16 '15 at 15:16
0

You're looking for $.each. This will allow you to loop through object-arrays.

https://api.jquery.com/jquery.each/

If you're also asking how to capture the values and you have the raw text, you're also looking for $.parseJSON

https://api.jquery.com/jquery.parsejson/

Josh
  • 3,258
  • 2
  • 19
  • 31