我有一个获取JSON的函数。这是我的JSON问题的格式。
[{"ChapterNum":"1","CourseID":"37","ChapterID":"30"},
{"ChapterNum":"2","CourseID":"37","ChapterID":"31"},
{"ChapterNum":"3","CourseID":"37","ChapterID":"32"}]
我基本上想把“ChapterNum”的值带到我的android sqlite数据库中。
我很困惑如何遍历它并将其放入我的数据库......
用于执行此操作的先前数据库条目。
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);
}
有什么想法吗?
答案 0 :(得分: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);
}
}