如何从json多维数组访问数据

时间:2012-06-11 19:19:21

标签: jquery json

我有一个像这样的结果

[
       {"courseStarted" :[
               {"CategoryName":"CategoryName","courseName" :"Equipment Course", "seriesName": "Free Education", "courseDetails" :[
                       {"totalLessons" :"2", "NoLessonComplete":"1", "lastviewed":"date", "lessonDetails":[
                               {"lessonId":"342367","quiz":"quizReady"},
                               {"lessonId" :"345345", "quiz":"quizReady"},
                               {"lessonId":"435435","quiz":"quizReady"},
                               {"lessonId":"4234234", "quiz":"quizFailed"},
                               {"lessonId":"4234234","quiz":"quizPassed"}
                       ]}
               ]}
       ]},
       {"courseStarted" :[
               {"CategoryName":"CategoryName","courseName" :"Equipment Course1", "seriesName": "Free Education1", "courseDetails" :[
                       {"totalLessons" :"21", "NoLessonComplete":"11", "lastviewed":"date1", "lessonDetails":[
                               {"lessonId":"3423671","quiz":"quizReady"},
                               {"lessonId" :"3453451", "quiz":"quizReady"},
                               {"lessonId":"4354351","quiz":"quizReady"},
                               {"lessonId":"42342341", "quiz":"quizReady"},
                               {"lessonId":"42342341","quiz":"quizPassed"}
                       ]}
               ]}
       ]}

    ]
从这个json输出

如何使用$ .each函数访问lessonId的值。

1 个答案:

答案 0 :(得分:1)

$.each(data, function(){

    // Print course name
    console.log(this.courseStarted[0].courseName);

    $.each(this.courseStarted[0].courseDetails[0].lessonDetails, function(){
        // Print lesson id
        console.log(this.lessonId);
    });

});