在java中将结果集转换为嵌套的json

时间:2016-03-03 14:23:48

标签: java json resultset

我正在尝试将结果集转换为Java中的JSON字符串。数据格式为

+---+----------+-------------+---------------+
| id| job_type | question    | response_type | 
+---+----------+-------------+---------------+
| 1 | quote    | question1   | text1         |   
| 2 | quote    | question2   | number2       |   
+---+----------+-------------+---------------+
| 3 | standard | question1   | text2         |   
| 4 | standard | question2   | number2       |   
+---+----------+-------------+---------------+

我希望以

的形式获得JSON
{
   "JobType": “Quote",
   "Questions": [{
      "question": “question1",
      "response_type": “ text1",
   }, {
      "question": “question2",
      "response_type": “ number2",
   }],
   “JobType”:”Standard”,
   "Questions": [{
       "question": “question1",
       "response_type": “ number2",
   }, {
       "question": “question2",
       "response_type": “ number2",
   }]
}

这是我到目前为止的地方

JSONObject jobType = new JSONObject();
List<JSONObject> jobTypeQuestionListIndividual = new ArrayList<JSONObject>();

int i = 0;
if (!jobTemplate.next()) {    
        System.out.println("No records found");
} else {
    do {
        if (i % 2 == 0) {
            jobType.put("JobType",jobTemplate.getString("JobType") );

            JSONObject firstQuestion = new JSONObject();
            firstQuestion.put("question",  jobTemplate.getString("question"));
            firstQuestion.put("response_type",  jobTemplate.getString("response_type"));

            jobTypeQuestionListIndividual.add(firstQuestion);

            jobType.put("Questions",jobTypeQuestionListIndividual);

        } else {
            JSONObject question = new JSONObject();

            question.put("question",  jobTemplate.getString("question"));
            question.put("response_type",  jobTemplate.getString("response_type"));

            jobTypeQuestionListIndividual.add(question);
        }

        i = i+1;

    } while (jobTemplate.next());
}

System.out.println( jobType);

但这会导致

{
   "JobType": “Quote",
   "Questions": [{
      "question": “question1",
      "response_type": “ text1",
   }, {
      "question": “question2",
      "response_type": “ number2",
   },{
      "question": “question1",
      "response_type": “ number2",
   }, {
       "question": “question2",
       "response_type": “ number2",
   }]
}

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我设法通过首先创建hashmaps和hashsets来弄清楚如何构建嵌套的json。这受到以下答案的启发: cppreference.comHow to get an array of two values from jdbc result set

我的代码没有提供我最初需要的格式的JSON,但它适用于我。我得到了

{“Quote”:[{“question”:“question1”,“response_type”:“text1”,},{“question”:“question2”,“response_type”:“number2”,}],“标准“:[{”question“:”question1“,”}]}

我的更新代码是

String out = "";
try {
    String jobType = "";
    ResultSet jobTemplate = jobs.getAllJobDetailsTemplate(Integer.parseInt(customerId));
    Map<String, Set<HashMap<String,String>>> jobTypes = new HashMap<String, Set<HashMap<String,String>>>();

    while (jobTemplate.next()) {
        jobType = jobTemplate.getString("JobType");
        Set<HashMap<String,String>> questions = jobTypes.containsKey(jobType) ? jobTypes.get(jobType) : new HashSet<HashMap<String,String>>();
           HashMap<String,String> individualQuestion = new HashMap<String,String>();
             individualQuestion.put("question",  jobTemplate.getString("question"));
             individualQuestion.put("response_type", jobTemplate.getString("response_type"));
           questions.add(individualQuestion);
        jobTypes.put(jobType, questions); 
    }

    out = new ObjectMapper().writeValueAsString(jobTypes);
}