我正在創建一個Web服務來回報一個嵌套的JSON這樣的創建與Java嵌套JSON:從ResultSet中
{
"questionaire": {
"idSection": 1,
"sectionName": "Section Test 1",
"questions": {
"text": {
"idQuestion": 1,
"statement": "Question statement",
"kindQuestion": "boolean",
"availableAnswers": {
"idAnswer": 1,
"stringAnswer": "answer 1"
}
}
}
},
"idSection": 1,
"sectionName": "Section Test 1",
"questions": {
"text": {
"idQuestion": 1,
"statement": "Question statement",
"kindQuestion": "boolean",
"availableAnswers": {
"idAnswer": 1,
"stringAnswer": "answer 1"
}
}
}
}
我不是100%肯定,如果我寫的結構是正確的,想法是:我得到部分,其中包含問題和每個問題的答案是從一種(布爾,數字,選擇),並有她自己的答案。所有這些數據我都在數據庫中,現在我必須得到部分和所有相關信息,如:部分>問題>鍵入答案>可用答案。
這就是我想要做的,但它不恰當地工作,我不知道相對於節中的問題和內部問題的答案,以及如何巢下去。
// ques is a JSONArray
sections = resultset from database;
// While loop for every section
while(sections.next()){
// Here I start to create the json
jsonD.put("sectionID", sections.getInt("id"));
jsonD.put("sectionName", sections.getString("sectionName"));
questions = resultset from database, contains the questions for every section
// Recupera preguntes vinculades a la seccio
while(questions.next()){
ques.put("idQuestion", id);
ques.put("statement", statement);
...
}
}
在這一點上我的代碼不會創建嵌套的JSON適當
在我看來,這將是更好地創建一個Java對象結構層次結構的和使用db結果集準備該對象結構,然後使用jackson或GSON將該對象轉換爲json。 –