15

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?

2
  • have you missed a comma after the second element? Commented 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. Commented Jun 19, 2013 at 8:32

4 Answers 4

32

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

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

1 Comment

Thx alot this one helped
4

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

Fiddle

Comments

2

Use jQuery.each()

$.each( yourArrayOfObjects, function( index, object ){
  // do your magic here
});

Comments

0
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]
        }
    }

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.