私は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"}
]
私が必要なのは、このオブジェクトから2つのドロップダウンを作成することです。
回答番号1->真/偽
回答番号2->正誤問題
ドロップダウン部分私は自分でやります..私はこのオブジェクトを反復する方法について混乱しています。 any1でいくつかの例を教えてもらえますか?
jsonオブジェクトjsonObject
は配列に格納されます。行う :
$.each(jsonArray, function(index,jsonObject){
$.each(jsonObject, function(key,val){
console.log("key : "+key+" ; value : "+val);
});
});
それはあなたに与えます
key : DisplayName ; value : Answer Number 1
key : Value ; value : Answer 1
key : Option ; value : true
とにかく、アンソニーは正しいです。あなたの構造は操作するのが難しいでしょう
JSONが無効です。これはどうですか :
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);
});
jQuery.each() を使用します
$.each( yourArrayOfObjects, function( index, object ){
// do your magic here
});
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"
}];
繰り返す
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]
}
}