如何使用Retrofit2构建嵌套的JSON

时间:2018-09-23 18:29:54

标签: java android

我正在尝试在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 个答案:

答案 0 :(得分:0)

我想主要原因是您要分别构建列表,而每个答案都应该这样做一次。 从您的预期输出中我了解到,您有:

  • 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

结果应该看起来更像现在需要的东西了:)