gpt4 book ai didi

java - 使用 Jackson 生成 JSON 时标记对象

转载 作者:行者123 更新时间:2023-12-01 11:23:58 25 4
gpt4 key购买 nike

我想生成一个 JSON 字符串来表示包含节点和边的图:

这是我的代码:

private String sigmaDragNodeGen(int n, int e) throws JsonGenerationException, JsonMappingException, IOException
{
Collection<Node> nodes = new ArrayList<Node>();
Collection<Edge> edges = new ArrayList<Edge>();

for (int i = 0; i<n; i++)
{
nodes.add(new Node(i));
}

for (int i=0; i<e; i++)
{

edges.add(new Edge(i, randInt(0, n), randInt(0,n) ));
}


ArrayList<Object> objs = new ArrayList<Object>();
objs.add(nodes);
objs.add(edges);

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(objs);


return json;
}

这是正确的 JSON 格式的示例(三个节点,三个边)

{"nodes":[{"id":"n0","label":"Node 0","x":0.86,"y":0.94,"size":0.84,"color":"#666"},{"id":"n1","label":"Node 1","x":0.82,"y":0.36,"size":0.32,"color":"#666"},{"id":"n2","label":"Node 2","x":0.16,"y":0.04,"size":0.31,"color":"#666"}],"edges":[{"id":"e0","source":"n2","target":"n2","size":0.59,"color":"#ccc"},{"id":"e1","source":"n1","target":"n0","size":0.03,"color":"#ccc"},{"id":"e2","source":"n0","target":"n1","size":0.95,"color":"#ccc"}]} 

以下是代码的输出:

[ [ {
"id" : "n0",
"size" : 0.8699021194884783,
"label" : "node 0",
"x" : 0.88077279794336,
"y" : 0.5359460674201729,
"color" : "#666"
}, {
"id" : "n1",
"size" : 0.33569239055407896,
"label" : "node 1",
"x" : 0.038203905272055416,
"y" : 0.33471657148239087,
"color" : "#666"
}, {
"id" : "n2",
"size" : 0.9716255994055384,
"label" : "node 2",
"x" : 0.8276469936703907,
"y" : 0.5837967558194771,
"color" : "#666"
} ], [ {
"id" : "e0",
"size" : 0.9003567677144881,
"color" : "#ccc",
"source" : "n3",
"target" : "n1"
}, {
"id" : "e1",
"size" : 0.30469509354678037,
"color" : "#ccc",
"source" : "n2",
"target" : "n3"
}, {
"id" : "e2",
"size" : 0.8801283716618974,
"color" : "#ccc",
"source" : "n2",
"target" : "n2"
} ] ]

问题是它没有生成“节点”和“边”标签。

此外 - 将其放入数组中不起作用。格式必须为 {[{ 而不是 [[{

我怎样才能做到这一点?

最佳答案

创建一个 Graph 对象来保存节点和边,并将其转换为 JSON。

public class Graph {


private Collection<Edge> edges;
private Collection<Node> nodes;
public Collection<Edge> getEdges() {
return edges;
}
public void setEdges(Collection<Edge> edges) {
this.edges = edges;
}
public Collection<Node> getNodes() {
return nodes;
}
public void setNodes(Collection<Node> nodes) {
this.nodes = nodes;
}
public Graph(Collection<Edge> edges, Collection<Node> nodes) {
super();
this.edges = edges;
this.nodes = nodes;
}



}

Graph g = new Graph(edges, nodes);

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(g);

return json;

输出:

{
"edges" : [ {
"id" : "e0",
"size" : 0.3186257261429163,
"color" : "#ccc",
"source" : "n2",
"target" : "n0"
}, {
"id" : "e1",
"size" : 0.3053632041981851,
"color" : "#ccc",
"source" : "n3",
"target" : "n2"
}, {
"id" : "e2",
"size" : 0.6057810501318774,
"color" : "#ccc",
"source" : "n3",
"target" : "n3"
} ],
"nodes" : [ {
"id" : "n0",
"size" : 0.8532639054164607,
"label" : "node 0",
"x" : 0.9745052833914616,
"y" : 0.5453935240318681,
"color" : "#666"
}, {
"id" : "n1",
"size" : 0.38109723464675194,
"label" : "node 1",
"x" : 0.8087916300487502,
"y" : 0.9290942762280863,
"color" : "#666"
}, {
"id" : "n2",
"size" : 0.10385603038824787,
"label" : "node 2",
"x" : 0.5557991632545263,
"y" : 0.6368544717623684,
"color" : "#666"
} ]
}

关于java - 使用 Jackson 生成 JSON 时标记对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30970305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com