【发布时间】:2018-12-11 12:27:03
【问题描述】:
我有一个示例 json 如下
[{"state":"Completed","mignum":146289,"projectleader":"Eric Lok","productiondate":"Jun 6, 2018","installationtiers":"Windows Server","targetplatform":"Production","apprelated":"UPS Pickup Point Web Application","appversion":"2.25"},
{"state":"Completed","mignum":146381,"projectleader":"James Rice","productiondate":"Jun 6, 2018","installationtiers":"Linux Web WL10","targetplatform":"Production","apprelated":"Content Only","appversion":""},
{"state":"Pending Migration to Production","mignum":146461,"projectleader":"Nishith Jani","productiondate":"Jun 6, 2018","installationtiers":"Linux BEA WL12","targetplatform":"Production","apprelated":"Tracking Comp","appversion":"1801.20"},
{"state":"Completed","mignum":146574,"projectleader":"Nishith Jani","productiondate":"Jun 6, 2018","installationtiers":"Linux BEA WL12","targetplatform":"Production","apprelated":"Tracking Comp","appversion":"01-00-07-17"}
{"state":Pending Approval by Development in Windward","mignum":146289,"projectleader":"Eric Lok","productiondate":"Jun 6, 2018","installationtiers":"Windows Server","targetplatform":"Production","apprelated":"UPS Pickup Point Web Application","appversion":"2.25"},
{"state":"Completed","mignum":146381,"projectleader":"James Rice","productiondate":"Jun 6, 2018","installationtiers":"Linux Web WL10","targetplatform":"Production","apprelated":"Content Only","appversion":""},
{"state":"Pending Approval by Development in Windward","mignum":146461,"projectleader":"Nishith Jani","productiondate":"Jun 6, 2018","installationtiers":"Linux BEA WL12","targetplatform":"Production","apprelated":"Tracking Comp","appversion":"1801.20"},
{"state":"Pending Migration to Production","mignum":146574,"projectleader":"Nishith Jani","productiondate":"Jun 6, 2018","installationtiers":"Linux BEA WL12","targetplatform":"Production","apprelated":"Tracking Comp","appversion":"01-00-07-17"}
]
我在 Spring 中使用 jdbc 连接从 db 获得
@GetMapping("/dashboard")
public String Data() {
Connection conn = null;
List<Map<String, Object>> listOfDates = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String countQuery = "SELECT migrations.mignum, migration_states.statemigrations.projectleader, migrations.productiondate, migrations.installationtiers, migrations.targetplatform, migrations.apprelated, migrations.appversion FROM migrations, migration_states WHERE migrations.productiondate='2018-07-07";
QueryRunner queryRunner = new QueryRunner();
listOfDates = queryRunner.query(conn, countQuery, new MapListHandler());
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
DbUtils.closeQuietly(conn);
}
return new Gson().toJson(listOfDates);
}
我想解析结果并将其保存在另一个json中,如下所示 1. 第一个结果
[
{
"state":"Completed"
"value": [
{"mignum":146289,"projectleader":"Eric Lok","productiondate":"Jun 6, 2018","installationtiers":"Windows Server","targetplatform":"Production","apprelated":"UPS Pickup Point Web Application","appversion":"2.25"};
{"mignum":146381,"projectleader":"James Rice","productiondate":"Jun 6, 2018","installationtiers":"Linux Web WL10","targetplatform":"Production","apprelated":"Content Only","appversion":""},
{"mignum":146381,"projectleader":"James Rice","productiondate":"Jun 6, 2018","installationtiers":"Linux Web WL10","targetplatform":"Production","apprelated":"Content Only","appversion":""}
]
}
{
"state":"Pending Migration to Production"
"value": [
{"mignum":146461,"projectleader":"Nishith Jani","productiondate":"Jun 6, 2018","installationtiers":"Linux BEA WL12","targetplatform":"Production","apprelated":"Tracking Comp","appversion":"1801.20"}.
{"mignum":146574,"projectleader":"Nishith Jani","productiondate":"Jun 6, 2018","installationtiers":"Linux BEA WL12","targetplatform":"Production","apprelated":"Trackin Comp","appversion":"01-00-07-17"}
]
}
{
"state":"Pending Approval by Development in Windward"
"value": [
{"mignum":146461,"projectleader":"Nishith Jani","productiondate":"Jun 6, 2018","installationtiers":"Linux BEA WL12","targetplatform":"Production","apprelated":"Tracking Comp","appversion":"1801.20"}.
{"mignum":146574,"projectleader":"Nishith Jani","productiondate":"Jun 6, 2018","installationtiers":"Linux BEA WL12","targetplatform":"Production","apprelated":"Trackin Comp","appversion":"01-00-07-17"}
]
}
]
2。第二个结果
[
{ "state":"Completed", "count": "2"},
{ "state":"Pending Migration to Production", "count": "2"},
{ "state":"Pending Approval by Development in Windward", "count": "2"}
]
我是 Java/Spring 方面这种 json 操作的新手,所以任何人都可以帮助我,因为我很长时间都被困在这一点上
【问题讨论】:
-
您可以使用 DTO 投影。例如具有属性状态和值映射的 StateDTO。
-
migrations和migration_states是什么关系? -
migration_states 来自 DB 的迁移表
-
您应该停止考虑 JSON 操作以及 Map
。相反,创建一个类来表示您从数据库中读取的内容。创建另一个表示您要生成的 JSON 结构的类,并使用 Jackson 注释映射到 JSON。然后从数据库中读取数据,创建第一类的实例;将这些对象转换为第二类的实例,并要求杰克逊将这些第二类的实例写为 JSON。 -
@JB Nizet 我理解你所说的整体,但不自信。你能添加一些类似的代码来理解你在说什么吗?
标签: java json spring spring-boot gson