How to convert a JSON request into a list of specific format n spring?

 

问题

https://stackoverflow.com/questions/70978408/how-to-convert-a-json-request-into-a-list-of-specific-format-n-spring

I have a request JSON list like this:

[

{

Id: 0,

values: [a, b, c, d]

},

{

Id: 1,

values: [1, 2, 3, 4]

},

.

.

.

]

How do I convert this to a list like this:

[

{

Name: a,

Count: 1

},

{

Name: b,

Count: 2

}

{

Name: c,

Count: 3

}

{

Name: d,

Count: 4

}

]

I have a dto class consisting of name and count attributes.

解答

需要将原JSON 中 2 个不同集合的成员,按位置组成新集合。用 Java 实现代码较长。

Java 下的开源包 SPL 很容易写,只要三行:


A

1

=create(Name,Count)

2

>transpose(json(file("data.json").read()).(#2)).(A1.record(~))

3

=json(A1)

SPL 提供了 JDBC 供 Java 调用,把上面的脚本存为convert.splx,在 Java 中以存储过程的方式调用脚本文件:

Class.forName("com.esproc.jdbc.InternalDriver");

con= DriverManager.getConnection("jdbc:esproc:local://");

st = con.prepareCall("call convert()");
st.execute();

SPL 源代码:https://github.com/SPLWare/esProc

问答搜集