2016-12-30 4 views
0

からのjavaでネストされたJSONを作成します。私はこのような1つのネストされたJSONを返すようにWebサービスを作成するために働いていた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%わからないんだけど、私はセクションを持っています。セクションには質問が含まれています。質問に関連するすべての答えは、1つの種類(ブール値、数値、選択値)からあり、自分の答えを持っています。これらのデータはすべてデータベース上にありますが、ここでセクションとすべての関連情報を取得する必要があります:section> questions> type answer> available answers。

これは私がやろうとしていることですが、適切に機能しませんセクション内の質問と質問内の回答を入れ子にする方法がわかりません。

// 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); 
        ... 
       } 

       } 

この時点で私のコードは、適切に

+0

を使用して、階層構造をconstuctし、そのオブジェクト構造をdb resultsetで準備し、次にjacksonまたはGSONを使用してそのオブジェクトをjsonに変換します。 –

答えて

0

を生成するために、ジャクソンAPIを使用してみてください。彼らはあなたのためにトリックを行います。

0

Google gsonは、多次元のjson構造を構築するためにはるかに高度です。 まず、下のコードのようなデータ階層を作成します。次に、データに基づいて、クラスの各フィールドに値を設定します。 パブリッククラスCreateGoalRequest {

@SerializedName("name") 
private String goalName; 

@SerializedName("recommendable_modules_alt") 
private RecommendationModuleAlt recommendationList; 

@SerializedName("target_modules") 
private List<TargetModule> targetModules; 

@SerializedName("max_recommendation_size") 
private int recommendationSize; 

@SerializedName("start_date") 
private String startDate; 

@SerializedName("metrics_enabled") 
private Boolean metricsEnabled; 

public List<TargetModule> getTargetModules() { 
    return targetModules; 
} 

.......

そして、Javaのオブジェクト構造を作成する方が良いだろう、私の意見では

import com.google.gson.Gson; 
      CreateGoalRequest createGoalRequest = new CreateGoalRequest(); 
      createGoalRequest.setGoalName(goalName); 
      createGoalRequest.setMetricsEnabled(Boolean.TRUE); 
      createGoalRequest.setTargetModules(targetModules); 
      createGoalRequest.setRecommendationList(recommendableList); 
      createGoalRequest.setRecommendationSize(recommendableItemCount); 
      createGoalRequest.setStartDate(startDate); 

      try { 
       String entity = new String(gson.toJson(createGoalRequest)); 
関連する問題