Export and import JSON data in Cytoscape.js

Based on this question and answer , I made it a JSFiddle .

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

I use JSON.stringify(cy.json())to get JSON data from the elements, and on the other hand, I clear the area cyand use cy.add(text-input)to add the elements back.

Ie: you can add node, copy its JSON data, then you can update your browser and paste JSON data from node directly, try adding it to cy.

But I couldn't get this to work, and I can't figure out where I am going wrong (possibly using a function cy.add). Always getting both errors:

An element must be of type 'nodes' or 'edges'; you specified 'undefined'

Uncaught TypeError: Cannot read property 'single' of undefined

Any ideas?

Thanks in advance.

+4
source share
1 answer

If you create a source (or use 2.1 at release), you can use eles.jsons()that gives an array of JSON elements. You call cy.json(), which gives all the init init options of the JSON graph, which you cannot pass to cy.add()or similar.

Alternatively, eles.jsons()you can use an existing one ele.json()and create an array yourself by iterating over the elements.

You also need to pass objects to cy.add(), etc. You cannot pass a JSON string.

eg.

cy.add( JSON.parse( jsonString ) )
+3
source

Source: https://habr.com/ru/post/1526091/


All Articles