我想遍历并展平一个大json文件,该文件具有以下显示产品层次结构的结构(可以将其视为在线商店中的导航):
productGroups: [
{
"key": "child 1"
...
"childrenProductGroups": [
{
"key": "child 1.1",
...,
"childrenProductGroups": []
},
{
"key": "child 1.2"
...
"childrenProductGroups": [
{
"key": "child 1.2.1",
...,
"childrenProductGroups": [
{
"key": "child 1.2.1.1",
...,
childrenProductGroups": [
...
]
}
]
},
{
"key": "child 1.2.2",
...,
"childrenProductGroups": []
}
]
},
{
"key": "child 1.3",
...,
"childrenProductGroups": [
...
]
}
]
},
{
"key": "child 2",
...,
"childrenProductGroups": [
...
]
},
{
"key": "child 3",
...,
"childrenProductGroups": [
...
]
}
]
我想用这样的格式把它们展平:
{
"hierarchieSet": [
{
"Nodeid": "00000001", # Number in this json
"Nodename": "child 1",
"Tlevel": "01", # First child of product group
"Parentid": "00000000", # Parent is null
"Childid": "00000002", # Child node number
"Nextid": "00000008" # Node number on the same level (child 2)
},
{
"Nodeid": "00000002",
"Nodename": "child 1.1",
"Tlevel": "02",
"Parentid": "00000001",
"Childid": "00000003",
"Nextid": "00000003"
},
{
"Nodeid": "00000003",
"Nodename": "child 1.2",
"Tlevel": "02",
"Parentid": "00000002",
"Childid": "00000005",
"Nextid": "00000007"
},
{
"Nodeid": "00000004",
"Nodename": "child 1.2.1",
"Tlevel": "03",
"Parentid": "00000003",
"Childid": "0000005",
"Nextid": "00000006"
}
,
{
"Nodeid": "00000005",
"Nodename": "child 1.2.1.1",
"Tlevel": "04",
"Parentid": "00000004",
"Childid": "0000000", #No more children
"Nextid": "00000000"
},
{
"Nodeid": "00000006",
"Nodename": "child 1.2.2",
"Tlevel": "03",
"Parentid": "00000003",
"Childid": "0000000",
"Nextid": "00000000"
},
{
"Nodeid": "00000007",
"Nodename": "child 1.3",
"Tlevel": "02",
"Parentid": "00000001",
"Childid": "0000000",
"Nextid": "00000000"
},
{
"Nodeid": "00000008",
"Nodename": "child 2",
"Tlevel": "01",
"Parentid": "00000000",
"Childid": "0000009", # 00000009 not shown
"Nextid": "00000014" #
},
...
{
"Nodeid": "000000014",
"Nodename": "child 3",
"Tlevel": "01",
"Parentid": "00000000",
"Childid": "00000015",
"Nextid": "00000000" # 00000010 does not exist
}
]
}
因此,我确定了一些主要问题:
树结构的递归
变换元素
整平结构
跟踪父母、兄弟姐妹和孩子
跟踪递归级别
格式化数字
我试图用两种不同的方法来解决这个问题:
使用dataweave转换所有元素
使用java遍历结构
由于我对函数式编程相当陌生,所以我更关注java实现,但遇到了许多问题。
java方法
读取json>init tree var并为顶级数组调用中的每个元素分配java instance> traverse(data, level)
在 Tree.java
.
树.java:
import org.json.JSONObject;
public class Tree {
private int id = 0;
private List<Node> nodes = new ArrayList<Node>();
public Tree() {
nodes.add(new Node("01", "00000001", "HOME", "01", "00000000", "00000002", "00000000"));
}
public void traverse(String data, int level) {
System.out.println(data);
// TODO parse json
}
private void visit(JSONObject parent, JSONObject node, int level) {
id++;
nodes.add(new Node("01", String.valueOf(id), node.getString("key"), String.valueOf(level), "", "", ""));
}
public List<Node> getNodes() {
return nodes;
}
private static class Node {
private String zshop, nodename, parentid, childid, nextid, nodeid, tlevel;
public Node(String zshop, String nodeid, String nodename, String tlevel, String parentid, String childid, String nextid) {
this.zshop = zshop;
this.nodeid = nodeid;
this.nodename = nodename;
this.tlevel = tlevel;
this.parentid = parentid;
this.childid = childid;
this.nextid = nextid;
}
}
}
调用invoke操作时,我使用以下负载:
%dw 2.0
output application/java
---
{
data: vars.rootMessage.payload as String,
level: 1
}
但这会产生以下错误:
无法强制对象{编码:utf-8,媒体类型:text/json;charset=utf-8,mimetype:text/json,原始:org.mule.weave.v2.el。seekablecursorstream@50ecee52 }(org.mule.weave.v2.el。muletypedvalue@511ba9cc)到字符串
5 |数据:vars.rootmessage.payload as string,^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^跟踪:在main(行:5,列:7)“计算表达式:”%dw 2.0 output application/java---{数据:vars.rootmessage.payload as string,级别:1}“。
我尝试了很多方法:
把它扔到一个 ProductGroup
我用java写的对象
尝试将检索到的对象强制转换为 org.json.JSONObject
尝试缓冲和读取 vars.rootMessage.payload (Binary)
但这些问题我都解决不了。
dataweave接近我的.dw脚本
%dw 2.0
fun append
(item: Object, acc: Object = {
}) = acc ++ item
fun mapper(item: Object) =
{
Zshop: "01",
Nodeid: "00000000",
Nodename: item.key as Number as String {format: ""},
Tlevel: "02",
Parentid: "00000000",
Childid: "00000000",
Nextid: "00000000"
}
fun traverse(a: Array, level: Number) =
a map $ flatMap(value, index) -> value
output application/json
---
{
test: payload.productGroups reduce (item, acc) -> append(mapper(item), acc)
}
我试图解决一些问题。 mapper(item)
应该创建可以附加到最终输出的json对象 appender(item, acc)
. 递归已经被勾勒出来了,但还不是我主要关心的问题。
结果如下:
(original payload),
"Zshop": "01",
"Nodeid": "00000000",
"Nodename": "800",
"Tlevel": "02",
"Parentid": "00000000",
"Childid": "00000000",
"Nextid": "00000000",
"Zshop": "01",
"Nodeid": "00000000",
"Nodename": "110",
"Tlevel": "02",
"Parentid": "00000000",
"Childid": "00000000",
"Nextid": "00000000",
"Zshop": "01",
"Nodeid": "00000000",
"Nodename": "720",
"Tlevel": "02",
"Parentid": "00000000",
"Childid": "00000000",
"Nextid": "00000000",
"Zshop": "01",
"Nodeid": "00000000",
"Nodename": "710",
"Tlevel": "02",
"Parentid": "00000000",
"Childid": "00000000",
"Nextid": "00000000",
...
我想知道为什么我得到一个没有任何对象结构的平面结果。
我的问题:
java :为什么我不能铸造字符串或它是如何正确地完成
dataweave:有没有一个简单的解决方案我看不出来?
为什么它是一个平淡的结果而不是一个对象?
是这个词的用法吗 reduce
以及 flatMap
功能是否正确?
欢迎任何帮助和/或反馈。
2条答案
按热度按时间rdrgkggo1#
基于我得到的帮助,我可以设计出这个解决方案:
以及流配置:
oxiaedzo2#
java :为什么我不能铸造字符串或它是如何正确地完成
json不是字符串。使用write(有效负载,'application/json')生成字符串。
dataweave:有没有一个简单的解决方案我看不出来?
只需传递对象,它就是java中的map。因为它是树-每个分支都是这个图中的另一个图。
为什么它是一个平淡的结果而不是一个对象?
它总是客观存在的。java世界里没有其他东西。
reduce和flatmap函数的用法是否正确?
不,mapobject和递归应该是很好的方法。