我有以下对象列表(例如,仅显示数组中的一个对象)。如何使用 Java 将用户列表拆分为具有相同原始对象的不同对象。我已经给出了从数据库获得的结果,并且在 Java 中处理后得到了预期的结果。
正如您在给定的预期 JSON 中所期望的结果中看到的,除了每个对象中的用户数组列表之外,所有对象都保持不变。我们必须根据 users 数组中所有对象的
"parent_user_id"
来拆分数组,因此具有相同 "parent_user_id"
的常见对象将作为一个整体包含在一个对象中。
从数据库得到的结果:
[
{
"_id" : "63a8808652f40e1d48a3d1d7",
"name" : "A",
"description" : null,
"users" : [
{
"id" : "63a8808c52f40e1d48a3d1da",
"owner" : "John Doe",
"purchase_date" : "2022-12-25",
"status" : "active",
"parent_user_id" : "63a8808c52f40e1d48a3d1da"
},
{
"id" : "63a880a552f40e1d48a3d1dc",
"owner" : "John Doe 1",
"purchase_date" : "2022-12-25",
"parent_user_id" : "63a8808c52f40e1d48a3d1da"
},
{
"id" : "63a880f752f40e1d48assddd"
"owner" : "John Doe 2",
"purchase_date" : "2022-12-25",
"parent_user_id" : "63a8808c52f40e1d48a3d1da"
},
{
"id" : "63a880f752f40e1d48a3d207"
"owner" : "John Doe 11",
"dt" : "2022-12-25",
"status" : "inactive",
"parent_user_id" : "63a880f752f40e1d48a3d207"
},
{
"id" : "63a880f752f40e1d48agfmmb"
"owner" : "John Doe 112",
"dt" : "2022-12-25",
"status" : "active",
"parent_user_id" : "63a880f752f40e1d48agfmmb"
}
{
"id" : "63a880f752f40e1d48agggg"
"owner" : "John SS",
"dt" : "2022-12-25",
"status" : "inactive",
"parent_user_id" : "63a880f752f40e1d48agggg"
}
{
"id" : "63a880f752f40e1d487777"
"owner" : "John SS",
"dt" : "2022-12-25",
"parent_user_id" : "63a880f752f40e1d48agggg"
}
]
}
]
用Java处理后我需要的结果:
[
{
"_id": "63a8808652f40e1d48a3d1d7",
"name": "A",
"description": null,
"users": [
{
"id": "63a8808c52f40e1d48a3d1da",
"owner": "John Doe",
"purchase_date": "2022-12-25",
"status": "active",
"parent_user_id": "63a8808c52f40e1d48a3d1da"
},
{
"id": "63a880a552f40e1d48a3d1dc",
"owner": "John Doe 1",
"purchase_date": "2022-12-25",
"parent_user_id": "63a8808c52f40e1d48a3d1da"
},
{
"id": "63a880f752f40e1d48assddd",
"owner": "John Doe 2",
"purchase_date": "2022-12-25",
"parent_user_id": "63a8808c52f40e1d48a3d1da"
}
]
},
{
"_id": "63a8808652f40e1d48a3d1d7",
"name": "A",
"description": null,
"users": [
{
"id": "63a880f752f40e1d48a3d207",
"owner": "John Doe 11",
"dt": "2022-12-25",
"status": "inactive",
"parent_user_id": "63a880f752f40e1d48a3d207"
}
]
},
{
"_id": "63a8808652f40e1d48a3d1d7",
"name": "A",
"description": null,
"users": [
{
"id": "63a880f752f40e1d48agfmmb",
"owner": "John Doe 112",
"dt": "2022-12-25",
"status": "active",
"parent_user_id": "63a880f752f40e1d48agfmmb"
}
]
},
{
"_id": "63a8808652f40e1d48a3d1d7",
"name": "A",
"description": null,
"users": [
{
"id": "63a880f752f40e1d48agggg",
"owner": "John SS",
"dt": "2022-12-25",
"status": "inactive",
"parent_user_id": "63a880f752f40e1d48agggg"
},
{
"id": "63a880f752f40e1d487777",
"owner": "John SS",
"dt": "2022-12-25",
"parent_user_id": "63a880f752f40e1d48agggg"
}
]
}
]
您可以使用 Josson 等 JSON 库来进行转换。
https://github.com/octomix/josson
反序列化
Josson josson = Josson.fromJsonString(
"[" +
" {" +
" \"_id\": \"63a8808652f40e1d48a3d1d7\"," +
" \"name\": \"A\"," +
" \"description\": null," +
" \"users\": [" +
" {" +
" \"id\": \"63a8808c52f40e1d48a3d1da\"," +
" \"owner\": \"John Doe\"," +
" \"purchase_date\": \"2022-12-25\"," +
" \"status\": \"active\"," +
" \"parent_user_id\": \"63a8808c52f40e1d48a3d1da\"" +
" }," +
" {" +
" \"id\": \"63a880a552f40e1d48a3d1dc\"," +
" \"owner\": \"John Doe 1\"," +
" \"purchase_date\": \"2022-12-25\"," +
" \"parent_user_id\": \"63a8808c52f40e1d48a3d1da\"" +
" }," +
" {" +
" \"id\": \"63a880f752f40e1d48assddd\"," +
" \"owner\": \"John Doe 2\"," +
" \"purchase_date\": \"2022-12-25\"," +
" \"parent_user_id\": \"63a8808c52f40e1d48a3d1da\"" +
" }," +
" {" +
" \"id\": \"63a880f752f40e1d48a3d207\"," +
" \"owner\": \"John Doe 11\"," +
" \"dt\": \"2022-12-25\"," +
" \"status\": \"inactive\"," +
" \"parent_user_id\": \"63a880f752f40e1d48a3d207\"" +
" }," +
" {" +
" \"id\": \"63a880f752f40e1d48agfmmb\"," +
" \"owner\": \"John Doe 112\"," +
" \"dt\": \"2022-12-25\"," +
" \"status\": \"active\"," +
" \"parent_user_id\": \"63a880f752f40e1d48agfmmb\"" +
" }," +
" {" +
" \"id\": \"63a880f752f40e1d48agggg\"," +
" \"owner\": \"John SS\"," +
" \"dt\": \"2022-12-25\"," +
" \"status\": \"inactive\"," +
" \"parent_user_id\": \"63a880f752f40e1d48agggg\"" +
" }," +
" {" +
" \"id\": \"63a880f752f40e1d487777\"," +
" \"owner\": \"John SS\"," +
" \"dt\": \"2022-12-25\"," +
" \"parent_user_id\": \"63a880f752f40e1d48agggg\"" +
" }" +
" ]" +
" }," +
" {" +
" \"_id\": \"111111111111111111111111\"," +
" \"name\": \"B\"," +
" \"users\": [" +
" {" +
" \"id\": \"22222222222222222222222\"," +
" \"owner\": \"John XX\"," +
" \"purchase_date\": \"2022-12-25\"," +
" \"status\": \"active\"," +
" \"parent_user_id\": \"22222222222222222222222\"" +
" }" +
" ]" +
" }" +
"]");
转型
JsonNode node = josson.getNode(
"[]@" + // 1
".users" + // 2
".group(parent_user_id)" + // 3
".map(..._id, ...name, ...description, users:elements)" + // 4
".@flatten()"); // 5
System.out.println(node.toPrettyString());
users
parent_user_id
_id/name/description
,并将elements
重命名为users
输出
[ {
"_id" : "63a8808652f40e1d48a3d1d7",
"name" : "A",
"description" : null,
"users" : [ {
"id" : "63a8808c52f40e1d48a3d1da",
"owner" : "John Doe",
"purchase_date" : "2022-12-25",
"status" : "active",
"parent_user_id" : "63a8808c52f40e1d48a3d1da"
}, {
"id" : "63a880a552f40e1d48a3d1dc",
"owner" : "John Doe 1",
"purchase_date" : "2022-12-25",
"parent_user_id" : "63a8808c52f40e1d48a3d1da"
}, {
"id" : "63a880f752f40e1d48assddd",
"owner" : "John Doe 2",
"purchase_date" : "2022-12-25",
"parent_user_id" : "63a8808c52f40e1d48a3d1da"
} ]
}, {
"_id" : "63a8808652f40e1d48a3d1d7",
"name" : "A",
"description" : null,
"users" : [ {
"id" : "63a880f752f40e1d48a3d207",
"owner" : "John Doe 11",
"dt" : "2022-12-25",
"status" : "inactive",
"parent_user_id" : "63a880f752f40e1d48a3d207"
} ]
}, {
"_id" : "63a8808652f40e1d48a3d1d7",
"name" : "A",
"description" : null,
"users" : [ {
"id" : "63a880f752f40e1d48agfmmb",
"owner" : "John Doe 112",
"dt" : "2022-12-25",
"status" : "active",
"parent_user_id" : "63a880f752f40e1d48agfmmb"
} ]
}, {
"_id" : "63a8808652f40e1d48a3d1d7",
"name" : "A",
"description" : null,
"users" : [ {
"id" : "63a880f752f40e1d48agggg",
"owner" : "John SS",
"dt" : "2022-12-25",
"status" : "inactive",
"parent_user_id" : "63a880f752f40e1d48agggg"
}, {
"id" : "63a880f752f40e1d487777",
"owner" : "John SS",
"dt" : "2022-12-25",
"parent_user_id" : "63a880f752f40e1d48agggg"
} ]
}, {
"_id" : "111111111111111111111111",
"name" : "B",
"users" : [ {
"id" : "22222222222222222222222",
"owner" : "John XX",
"purchase_date" : "2022-12-25",
"status" : "active",
"parent_user_id" : "22222222222222222222222"
} ]
} ]