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?

3
  • 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. Commented Apr 16, 2015 at 6:23
  • Where do you need to capture it? There are many functions like each()/map() available. Commented Apr 16, 2015 at 6: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 Commented Apr 16, 2015 at 6:34

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

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?)
@t.niese agreed.. Foreach preffered.
inside your each you should write one more... $.each(yourObject, function( index, value ) { $.each(value.input, function( i, v ) { /* Here code ... */ }); });
Added native js another example. I know, that it isn't actual already, but just for example...
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/

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.