我有如下的Json Array。
{
"leader_lessons":{
"lessons":{
"class_sec_data":{
"1":{
"leader_id":"1111",
"leader_school":null,
"leader_std":"Std2",
"leader_section":"B",
"leader_lesson_date":"2018-12-09",
"school_lesson_code":"Std2.2",
"leader_lessonName":"SUM - Mathematics"
},
"2":{
"leader_id":"1111",
"leader_school":null,
"leader_std":"Std2",
"leader_section":"D",
"leader_lesson_date":"2018-12-09",
"school_lesson_code":"Std2.2",
"leader_lessonName":"SUM - Mathematics"
},
"3":{
"leader_id":"1111",
"leader_school":null,
"leader_std":"UKG",
"leader_section":"C",
"leader_lesson_date":"2018-12-09",
"school_lesson_code":"UKG.31",
"leader_lessonName":"Rhymes English"
},
"4":{
"leader_id":"1111",
"leader_school":null,
"leader_std":"UKG",
"leader_section":"D",
"leader_lesson_date":"2018-12-09",
"school_lesson_code":"UKG.31",
"leader_lessonName":"Rhymes English"
},
"5":{
"leader_id":"1111",
"leader_school":null,
"leader_std":"UKG",
"leader_section":"H",
"leader_lesson_date":"2018-12-09",
"school_lesson_code":"UKG.31",
"leader_lessonName":"Rhymes English"
},
"6":{
"leader_id":"1111",
"leader_school":null,
"leader_std":"UKG",
"leader_section":"G",
"leader_lesson_date":"2018-12-09",
"school_lesson_code":"UKG.31",
"leader_lessonName":"Rhymes English"
}
}
},
"dates":[
"2018-12-09",
"2018-12-09"
]
}
}
我必须计算class_sec_data
的长度,并且在迭代i
中必须分配给HTML标记,如下所示。
for(var i = 0; i < school_leader_json.leader_lessons.lessons.class_sec_data.length; i++)
{
counter= counter+1;
$setInnerControlHTML("LD_Class_"+counter, school_leader_json.leader_lessons.lessons.class_sec_data[counter].leader_std);
$setInnerControlHTML("LD_Section_"+counter, school_leader_json.leader_lessons.lessons.class_sec_data[counter].leader_section);
$setInnerControlHTML("LD_Lesson_"+counter, school_leader_json.leader_lessons.lessons.class_sec_data[counter].leader_lessonName);
}
但是school_leader_json.leader_lessons.lessons.class_sec_data.length
是未定义的。
答案 0 :(得分:0)
您正在循环对象而不是数组。
一个数组看起来像这样:
let myArray = [];
一个对象看起来像这样:
let myObject = {};
将对象视为关联数组,即键->值对的列表。 这些键称为对象属性。在您的情况下,键为“ 1”,“ 2”等。
const school_leader_json = {"leader_lessons":{"lessons":{"class_sec_data":{"1":{"leader_id":"1111","leader_school":null,"leader_std":"Std2","leader_section":"B","leader_lesson_date":"2018-12-09","school_lesson_code":"Std2.2","leader_lessonName":"SUM - Mathematics"},"2":{"leader_id":"1111","leader_school":null,"leader_std":"Std2","leader_section":"D","leader_lesson_date":"2018-12-09","school_lesson_code":"Std2.2","leader_lessonName":"SUM - Mathematics"},"3":{"leader_id":"1111","leader_school":null,"leader_std":"UKG","leader_section":"C","leader_lesson_date":"2018-12-09","school_lesson_code":"UKG.31","leader_lessonName":"Rhymes English"},"4":{"leader_id":"1111","leader_school":null,"leader_std":"UKG","leader_section":"D","leader_lesson_date":"2018-12-09","school_lesson_code":"UKG.31","leader_lessonName":"Rhymes English"},"5":{"leader_id":"1111","leader_school":null,"leader_std":"UKG","leader_section":"H","leader_lesson_date":"2018-12-09","school_lesson_code":"UKG.31","leader_lessonName":"Rhymes English"},"6":{"leader_id":"1111","leader_school":null,"leader_std":"UKG","leader_section":"G","leader_lesson_date":"2018-12-09","school_lesson_code":"UKG.31","leader_lessonName":"Rhymes English"}}},"dates":["2018-12-09","2018-12-09"]}};
const class_sec_data = school_leader_json.leader_lessons.lessons.class_sec_data;
let counter = 0;
for(let key in class_sec_data){
console.log(class_sec_data[key]);
counter++;
// $setInnerControlHTML("LD_Class_"+counter, class_sec_data[key].leader_std);
// $setInnerControlHTML("LD_Section_"+counter, class_sec_data[key].leader_section);
// $setInnerControlHTML("LD_Lesson_"+counter, class_sec_data[key].leader_lessonName);
}
console.log('Total:', counter);
答案 1 :(得分:0)
如下所示。
const class_sec_data = school_leader_json.leader_lessons.lessons.class_sec_data;
let counter = 0;
for(let key in class_sec_data){
console.log(class_sec_data[key]);
counter++;
$setControlInnerHTML("LD_Class_"+key, school_leader_json.leader_lessons.lessons.class_sec_data[key].leader_std);
$setControlInnerHTML("LD_Section_"+key, school_leader_json.leader_lessons.lessons.class_sec_data[key].leader_section);
$setControlInnerHTML("LD_Lesson_"+key, school_leader_json.leader_lessons.lessons.class_sec_data[key].leader_lessonName);
}