I have a json object as
[
{"DisplayName":"Answer Number 1","Value":"Answer1","Option":"True"},
{"DisplayName":"Answer Number 1","Value":"Answer1","Option":"False"},
{"DisplayName":"Answer Number 2","Value":"Answer2","Option":"True"},
{"DisplayName":"Answer Number 2","Value":"Answer2","Option":"False"}
]
What I need is to create 2 drop downs from this object as
Answer Number 1 -> True/False
Answer Number 2 -> True/False
dropdown part I'll do my self.. I m just confused on how to iterate over this object. Can any1 please lead me to some example?
asked Jun 19, 2013 at 8:23
Gautam
1,7408 gold badges32 silver badges68 bronze badges
-
have you missed a comma after the second element?leonhart– leonhart2013年06月19日 08:27:42 +00:00Commented Jun 19, 2013 at 8:27
-
Are you unable to change the structure of the JSON? That seems like an awful way to represent the information you want.Anthony Grist– Anthony Grist2013年06月19日 08:32:25 +00:00Commented Jun 19, 2013 at 8:32
4 Answers 4
your json objects jsonObject are stored in an array. Do :
$.each(jsonArray, function(index,jsonObject){
$.each(jsonObject, function(key,val){
console.log("key : "+key+" ; value : "+val);
});
});
it will gives you
key : DisplayName ; value : Answer Number 1
key : Value ; value : Answer 1
key : Option ; value : true
Anyway, Anthony is right. Your structure will be difficult to manipulate
answered Jun 19, 2013 at 8:34
TCHdvlp
1,3341 gold badge9 silver badges15 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Vipertecpro
Thx alot this one helped
Your JSON isn't valid. What about this :
var json = '[
{"DisplayName":"Answer Number 1","Value":"Answer1","Option":"True"},
{"DisplayName":"Answer Number 1","Value":"Answer1","Option":"False"},
{"DisplayName":"Answer Number 2","Value":"Answer2","Option":"True"},
{"DisplayName":"Answer Number 2","Value":"Answer2","Option":"False"}
]';
var jsonObject = $.parseJSON(json); //Only if not already an object
$.each(jsonObject, function (i, obj) {
alert(obj.DisplayName);
});
answered Jun 19, 2013 at 8:44
LMeyer
2,6311 gold badge27 silver badges36 bronze badges
Comments
Use jQuery.each()
$.each( yourArrayOfObjects, function( index, object ){
// do your magic here
});
answered Jun 19, 2013 at 8:32
Petr Vostrel
2,33316 silver badges23 bronze badges
Comments
var json = [{
"DisplayName": "Answer Number 1",
"Value": "Answer1",
"Option": "True"
}, {
"DisplayName": "Answer Number 1",
"Value": "Answer1",
"Option": "False"
}, {
"DisplayName": "Answer Number 2",
"Value": "Answer2",
"Option": "True"
}, {
"DisplayName": "Answer Number 2",
"Value": "Answer2",
"Option": "False"
}];
To iterate
for (i in json) {
//json[i] is your current object inside the array, {"DisplayName":"Answer Number 1","Value":"Answer1","Option":"True"}
for (key in json[i]) {
// keys are DisplayName, Value, Option
alert(key);
alert(json[i][key]); // instead of writing object.key you can also write object[key]
}
}
answered Jun 19, 2013 at 8:34
Atif
10.9k22 gold badges68 silver badges97 bronze badges
Comments
lang-js