将JSON数据导出并导入Cytoscape.js [英] Exporting and importing JSON data to Cytoscape.js

查看:121
本文介绍了将JSON数据导出并导入Cytoscape.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于此问题和答案,我已经制作这个 JSFiddle

Based on this question and answer, I've made this JSFiddle.

我在尝试什么要做的是找到一种方法将JSON数据正确导出/导入到cytoscape.js。

What I'm trying to do is to find a way to properly export / import JSON data to cytoscape.js.

我正在使用 JSON.stringify(cy。 json())从元素中获取JSON数据,另一方面我正在清理 cy 区域并使用 cy.add(文本输入)添加元素。

I'm using JSON.stringify(cy.json()) to get JSON data from elements, and on the other hand I'm cleaning the cy area and using cy.add(text-input) to add the elements back.

即:您可以添加节点,复制它生成的JSON数据,然后您可以刷新浏览器并直接从节点粘贴JSON数据,尝试将其添加到 CY

I.e.: you can add a node, copy it's JSON data generated, then you can refresh the browser and paste the JSON data from node directly, tryng to add it to cy.

但我无法解决这个问题,我无法弄明白我错在哪里(可能使用 cy。添加功能)。始终确定两个错误:

But I couldn't get this to work, and I can't really figure out where I'm wrong (probably using the cy.add function). Always geting both errors:

元素必须是'nodes'或'edges'类型;你指定'undefined'

未捕获的TypeError:无法读取属性'single'的未定义

任何想法?

提前致谢。

推荐答案

如果你从源代码构建(或者在发布时使用2.1),你可以使用 eles.jsons(),给出一个元素JSON数组。你正在调用 cy.json(),它给出了整个图形初始化选项JSON - 你无法传递给 cy.add( )或类似。

If you build from the source (or use 2.1 when released), you can use eles.jsons(), which gives an array of element JSONs. You're calling cy.json(), which gives the entire graph init options JSON -- which you can't pass to cy.add() or similar.

除了 eles.jsons(),你可以使用已经存在的 ele.json()并通过遍历元素来自己建立一个数组。

Alternatively to eles.jsons(), you can use the already existing ele.json() and build up an array yourself by iterating over the elements.

你也是需要将对象传递给 cy.add()等。您无法传递JSON字符串。

You also need to pass the objects to cy.add() etc. You can't pass a JSON string.

例如

cy.add( JSON.parse( jsonString ) )

这篇关于将JSON数据导出并导入Cytoscape.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆