正在拆分json对象



我有来自SQL语句的JSON输出,如下所示

{
"idno":6473853,
"user":"GCA_GB",
"operation":"U",
"timestamp":"2022-08-22T13:14:48",
"first_name":{
"old":"rak",
"new":"raki"
},
"fam_name":{
"old":"gow",
"new":"gowda"
}
}

有可能,除了名字和姓氏,我们还可以得到缩写和昵称

我想将上面的json拆分为下面的

{
"idno":6473853,
"user":"GCA_GB",
"operation":"U",
"timestamp":"2022-08-22T13:14:48",
"first_name":{
"old":"rak",
"new":"raki"
}
}
{
"idno":6473853,
"user":"GCA_GB",
"operation":"U",
"timestamp":"2022-08-22T13:14:48",
"fam_name":{
"old":"gow",
"new":"gowda"
}
}

有人能帮我拿这个吗

从您的JSON数据结构中,我猜有4个固定字段,分别命名为idnouseroperationtimestamp。所有其他节点都是各种对象节点,每个节点都有旧值和新值。并且您希望分离所有对象节点。下面的解决方案将把它们分离并放到一个数组节点中。

https://github.com/octomix/josson

反序列化

Josson josson = Josson.fromJsonString(
"{" +
"   "idno":6473853," +
"   "user":"GCA_GB"," +
"   "operation":"U"," +
"   "timestamp":"2022-08-22T13:14:48"," +
"   "first_name":{" +
"      "old":"rak"," +
"      "new":"raki"" +
"   }," +
"   "fam_name":{" +
"      "old":"gow"," +
"      "new":"gowda"" +
"   }" +
"}");

转换

JsonNode node = josson.getNode(
"map(idno, user, operation, timestamp," +
"    changes: entries().[value.isObject()]*.map(key::value))" +
".unwind(changes)");
System.out.println(node.toPrettyString());

输出

[ {
"idno" : 6473853,
"user" : "GCA_GB",
"operation" : "U",
"timestamp" : "2022-08-22T13:14:48",
"first_name" : {
"old" : "rak",
"new" : "raki"
}
}, {
"idno" : 6473853,
"user" : "GCA_GB",
"operation" : "U",
"timestamp" : "2022-08-22T13:14:48",
"fam_name" : {
"old" : "gow",
"new" : "gowda"
}
} ]
package org.example;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import lombok.Getter;
import lombok.Setter;

public class GsonTest {
@Getter
@Setter
public class oldNewClass{
private String old;
@SerializedName("new")
private String _new;
}
@Getter @Setter
public class SomeObject {
private String idno;
private String user;
private String operation;
private String timestamp;
private oldNewClass first_name;
private oldNewClass fam_name;
}
public static void main(String[] args) {
Gson gson = new Gson();
SomeObject s = gson.fromJson("{"idno":6473853,"user":"GCA_GB","operation":"U","timestamp":"2022-08-22T13:14:48","first_name":{"old":"rak","new":"raki"},"fam_name":{"old":"gow","new":"gowda"}}", SomeObject.class);
/* Access various properties */
System.out.println(s.getFirst_name().get_new());
/* Manipulate the object and then convert it back to json using */
String jsonString = gson.toJson(s);
}
}

请尝试上面的代码。以下是它的两个maven依赖项

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>```

同意Pshemo创建两个独立的JSON对象,这里有一个选项:

const statement = {"idno":6473853,"user":"GCA_GB","operation":"U","timestamp":"2022-08-22T13:14:48","first_name":{"old":"rak","new":"raki"},"fam_name":{"old":"gow","new":"gowda"}}
const noFamName = {...statement};
delete noFamName['fam_name'];
const noFirstName = {...statement}:
delete noFirstName['first_name']

最新更新