简体   繁体   中英

How to contruct nested JSON using retrofit2

I am trying to construct a bit complex nested JSON for a POST request in my Android application, I am using Retrofit2 and GSON converter.

After constructing, I used GSON to print it out in order to be sure of what will be sent to the API endpoint. Below is the output I got

{
"agent_id":"testy",
"answer":[
    {
        "answer":"Yes",
        "question_id":"1"
    },
    {
        "answer":"Yes",
        "question_id":"5"
    },
    {
        "answer":"No",
        "question_id":"6"
    },
    {
        "answer":"No",
        "question_id":"7"
    },
    {
        "sub_question":[
            {"sub_question_id":"2"},
            {"sub_question_id":"2"},
            {"sub_question_id":"2"},
            {
                "sub_answer":[
                    {"coord":"6.4378537,3.4289744000000155","text":""},
                    {"coord":"6.4378537,3.4289744000000155","text":""},
                    {"coord":"6.4378537,3.4289744000000155","text":""}
                    ]
            }
            ]
    }
    ],
            "street_id":3,
            "token":"afadfadfadfdfHFGD_JSDHD"
  }

However, the actual format I need is as seen below

{
"agent_id":"testy",
"answer":[
    {
        "answer":"Yes",
        "question_id":"1",
        "sub_question":[
            {
                "sub_question_id":"2",
                "sub_answer":[
                    {"coord":"6.4378537,3.4289744000000155","text":""}
                    ]
            }
        ]
    },
    {
        "answer":"No",
        "question_id":"5",
        "sub_question":[
            {
                "sub_question_id":"2",
                "sub_answer":[
                    {"coord":"6.4378537,3.4289744000000155","text":""}
                    ]
            }
        ]

    },
    {
        "answer":"Yes",
        "question_id":"6",
        "sub_question":[
            {
                "sub_question_id":"2",
                "sub_answer":[
                    {"coord":"6.4378537,3.4289744000000155","text":""}
                    ]
            }
        ]

    },
    {
        "answer":"No",
        "question_id":"7",
        "sub_question":[
            {
                "sub_question_id":"2",
                "sub_answer":[
                    {"coord":"6.4378537,3.4289744000000155","text":""}
                    ]
            }
        ]

    }
],
"street_id":3,
"token":"asdfasdfasdf3453adfafdaADN"
}

The code that does the construction is below

private void submitAnswers() {

    List<Answer> answerList = new ArrayList<>();
    List<SubQuestion> subQuestionList = new ArrayList<>();
    List<SubAnswer> subQuestionAnswerList = new ArrayList<>();
    //Adding QuestionID and QuestionAnswer to the Answer array
    for (int k = 0; k < questSize.size(); k++) {
        Answer answer1 = new Answer();
        answer1.setQuestion_id(mainQuestAnsID.get(k));
        answer1.setAnswer(mainQuestAns.get(k));
        answerList.add(answer1);
    }

    for (int j = 0; j < subQuestID.size(); j++) {
        SubQuestion subQuest = new SubQuestion();
        subQuest.setSub_question_id(subQuestID.get(j));
        subQuestionList.add(subQuest);
    }

    for (int h = 0; h < subQuestAns.size(); h++) {
        SubAnswer subQuestionAnswer = new SubAnswer();
        subQuestionAnswer.setText(subQuestAns.get(h));
        subQuestionAnswer.setCoord("6.4378537,3.4289744000000155");
        subQuestionAnswerList.add(subQuestionAnswer);
    }

    Answer answer = new Answer();
    answer.setSub_question(subQuestionList);
    answerList.add(answer);
    SubQuestion subQuest = new SubQuestion();
    subQuest.setSub_answer(subQuestionAnswerList);
    subQuestionList.add(subQuest);

    AnswerRequest answerRequest = new AnswerRequest();
    answerRequest.setAgent_id(agentID);
    answerRequest.setToken(token);
    answerRequest.setStreet_id(streetID);
    answerRequest.setAnswer(answerList);

    //Gson for printing out the JSON
    Gson gson = new Gson();
    Type type = new TypeToken<AnswerRequest>() {
    }.getType();
    String json = gson.toJson(answerRequest, type);
    System.out.println(json);

}

Can anyone tell what is wrong that makes me not to get the desired output?

I guess the main reason is that you are building your lists separately, while you should do it once for every answer. For what I understand from your expected output, you have:

  • 1 main object that has some parameters and a list of answer
  • each answer has some parameters and a list of subquestion
  • each subquestion has some parameters and a list of subanswers

So you should build your object in the same way. Eg for each new answer, construct its list of subquestion, for each subquestion construct its list of subanswer

Code speaking it would be something like this:

for (int k = 0; k < questSize.size(); k++) {
    Answer answer1 = new Answer();
    answer1.setQuestion_id(mainQuestAnsID.get(k));
    answer1.setAnswer(mainQuestAns.get(k));

    // now construct the subquestion list of that new answer
    List<SubQuestion> subQuestions = new ArrayList<>();
    for (int j = 0; j < subQuestID.size(); j++) { // change the loop to get only what you need for the current answer
        SubQuestion subQuest = new SubQuestion();
        // add all the subquestions related to the answer here
        ...
        // Construct the subanswer of that new subquestion
        List<SubAnswer> subAnswers = new ArrayList<>();
        for (int h = 0; h < subQuestAns.size(); h++) { // adapt the loop to get the subanswers related to the current subquestion
            SubAnswer subQuestionAnswer = new SubAnswer();
            // get the infos needed for this subanswer
            ...
            // add the new subanswer to the list
            subAnswers.add(subQuestionAnswer);
        }
        // add the subanswer list to the subquestion
        subQuest.setSub_answer(
        // add the subquestion to the list
        subQuestions.add(subQuest);
    }
    // then add the list to the answer
    answer1.setSub_question(subQuestions);
    // finally add the answer to the list
    answerList.add(answer1);
}
// now you can create the AnswerRequest object like before

And you should have a result that looks more like what you need now :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM