簡體   English   中英

如何使用Retrofit2構建嵌套的JSON

[英]How to contruct nested JSON using retrofit2

我正在嘗試在Android應用程序中為POST請求構造一個有點復雜的嵌套JSON,我正在使用Retrofit2和GSON轉換器。

構造之后,我使用GSON進行打印以確保將什么發送到API端點。 以下是我得到的輸出

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

但是,我需要的實際格式如下所示

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

構造代碼如下

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);

}

誰能說出是什么原因導致我得不到想要的輸出?

我想主要原因是您要分別構建列表,而每個答案都應該這樣做一次。 從您的預期輸出中我了解到,您有:

  • 1個具有一些參數和答案列表的主要對象
  • 每個答案都有一些參數和子問題列表
  • 每個子問題都有一些參數和子答案列表

因此,您應該以相同的方式構建對象。 例如,對於每個新答案,構建其子問題列表,對於每個子問題,構建其子問題列表

代碼講起來像這樣:

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

而且您的結果應該看起來更像現在需要的東西:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM