2013-08-07 30 views
1

我有一個返回JSON的函數。這裏是我的JSON問題的格式。通過標記將JSON數組循環到android sql lite數據庫中?

[{"ChapterNum":"1","CourseID":"37","ChapterID":"30"}, 
    {"ChapterNum":"2","CourseID":"37","ChapterID":"31"},  
    {"ChapterNum":"3","CourseID":"37","ChapterID":"32"}] 

我基本上想把它的值「ChapterNum」,並將其存儲到我的android sqlite數據庫。

我只是困惑如何通過它循環,並把它放到我的DB ...

了先前的DB項做到這一點。

public long addStudentInfoToDb(String stuID,String stuFName, String stuLName) { 
    ContentValues student_info = new ContentValues(); 

    student_info.put(STU_ID, stuID); 
    student_info.put(STU_FNAME, stuFName); 
    student_info.put(STU_LNAME, stuLName); 
    return mDb.insert("Student", null, student_info); 

} 

所以我想喜歡

public long addChaptersToDb() { 
    ContentValues chapter_info = new ContentValues(); 



    ///loop through jArray 
      ///read array into contentvalues 





    return mDb.insert("Chapter", null, chapter_info); 
} 

任何想法?

回答

1
public long addChaptersToDb() { 
    JSONArray jsonArray = new JSONArray(response); 
    for(int i=0; i<jsonArray.length(); i++){ 
     JSONObject jsonData = jsonArray.getJSONObject(i); 
     String chapterNum = jsonData.getString("ChapterNum"); 
     String courseID = jsonData.getString("CourseID"); 
     String chapterID = jsonData.getString("ChapterID"); 

     ContentValues chapter_info = new ContentValues(); 
     chapter_info.put(ChapterNum, chapterNum); 
     chapter_info.put(CourseID, courseID); 
     chapter_info.put(ChapterID, chapterID); 

     mDb.insert("Chapter", null, chapter_info); 
    } 
}