使用Jackson生成JSON时标记对象

时间:2015-06-21 23:24:20

标签: java json jackson

我想生成一个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"
} ] ]

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

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

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

创建一个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"
  } ]
}