0

I'm working to create a web service to return one nested json like this:

{
    "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"
            }
        }
    }
}

I'm not 100% sure if the structure I wrote is correct, the idea is: I got sections, which contains questions and every answer related to question is from one kind (boolean, numeric, select) and has her own answers. All this data I have it on a database, now I have to get the sections and all the relative information like: section > questions > type answer > available answers.

This is what I'm trying to do but it doesn't work appropriately I don't know how to nest the questions relative to the section and the answers inside the questions and go on.

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

              }

On this point my code does not create the nested json appropriately

Amr Ashraf
  • 315
  • 4
  • 14
jcobo1
  • 1,065
  • 1
  • 18
  • 34
  • In my opinion it would be better to create a java object structure and prepare that object structure with db resultset and then use either jackson or GSON to convert that object into json. – Rahul Kumar Dec 30 '16 at 19:50

3 Answers3

0

You have to create a hierarchical data structure and then convert it into Json. Represent tree hierarchy in java

Try using the jackson API to generate JSON https://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/

Community
  • 1
  • 1
Jaydip Rakholiya
  • 792
  • 10
  • 20
0

Take a look at JsonObjectBuilder, JsonArrayBuilder, etc. in javax.json. They will do the trick for you.

bichito
  • 1,406
  • 2
  • 19
  • 23
0

Google gson are much more advanced when it comes to build multi dimentional json structures. First create an data heirarchy something like the below code. Then based on the data you have, set the the values to each of the fields in the Class. public class 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;
}

.......

And constuct your heirarchy using

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));
letmesolve
  • 264
  • 3
  • 8