[英]convert resultset to nested json in java
I am trying to convert a resultset into a JSON string in Java. 我正在尝试将结果集转换为Java中的JSON字符串。 The data is in the form
数据形式为
+---+----------+-------------+---------------+
| id| job_type | question | response_type |
+---+----------+-------------+---------------+
| 1 | quote | question1 | text1 |
| 2 | quote | question2 | number2 |
+---+----------+-------------+---------------+
| 3 | standard | question1 | text2 |
| 4 | standard | question2 | number2 |
+---+----------+-------------+---------------+
and I want to get JSON in the form 我想以以下形式获取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",
}]
}
This is where I have got so far 这是我到目前为止所到之处
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);
but this results in 但这导致
{
"JobType": “Quote",
"Questions": [{
"question": “question1",
"response_type": “ text1",
}, {
"question": “question2",
"response_type": “ number2",
},{
"question": “question1",
"response_type": “ number2",
}, {
"question": “question2",
"response_type": “ number2",
}]
}
Thanks in advance for your help. 在此先感谢您的帮助。
I managed to figure out how to build the nested json by first creating hashmaps and hashsets. 我设法通过首先创建哈希图和哈希集来弄清楚如何构建嵌套的json。 This was inspired by the following answers: How to get an array of two values from jdbc result set and Storing a hash map inside another hash map and improving performance
这是受以下答案的启发: 如何从jdbc结果集中获取两个值的数组,以及如何将哈希图存储在另一个哈希图中并提高性能
My code doesn't provide the JSON in exactly the format I originally needed, but it works for me. 我的代码没有提供与我最初所需的格式完全相同的JSON,但是对我有用。 I get
我懂了
{"Quote": [{"question": “question1","response_type": “ text1",},{ "question": “question2", "response_type": “ number2",}],"Standard":[{ "question": “question1","}]} {“ Quote”:[{“ question”:“ question1”,“ response_type”:“ text1”,},{“ question”:“ question2”,“ response_type”:“ number2”,}],“ Standard”:[ {“ question”:“ question1”,“}]}
My updated code is 我更新的代码是
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.