将结果集转换为java中的嵌套json
问题描述:
我想将结果集转换为Java中的JSON字符串。该数据的格式为将结果集转换为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",
}]
}
在此先感谢您的帮助页。
答
我设法弄清楚如何通过首先创建hashmaps和hashsets来构建嵌套的json。这个灵感来自于如下回答: How to get an array of two values from jdbc result set和Storing a hash map inside another hash map and improving performance
我的代码不正是我最初所需要的格式提供JSON,但它为我工作。我得到
{“Quote”:[{“question”:“question1”,“response_type”:“text1”,},{“question”:“question2”,“response_type”:“number2”,}]] “标准”:[{ “问题”:“问题1" ,“}]}
我更新的代码是
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);
}
我从来没有使用JSON与Java,但我建议你应该创建一个QuestioResponse只有两个字符串的类(如果你想保持ID,也许是一个整数),以及一个包含QuestionResponse列表的类JobType(使用一个保存顺序的列表,如果它很重要)以及带jobtype名称的String。然后,一旦您将这些Beans中的所有结果集都转换成了,您就可以使用它们来创建一个Json对象(或者甚至使用一些可以为您做的API /库)。这应该更容易。 – Asoub