Hi ,
Following is the Json String produced by calling method toJson on my model :
currentString =
{ \"class\": \"go.TreeModel\",\n \"nodeDataArray\": [ \n{\"key\":0, \"text\":\"Mind Map\", \"loc\":\"0 0\"},\n{\"text\":\"idea\", \"parent\":0, \"key\":-2, \"loc\":\"78.22705078125 0\"},\n{\"text\":\"idea\", \"parent\":-2, \"key\":-3, \"loc\":\"128.22705078125 0\"}\n ]}:
Now when i try to load data from this string using :
myDiagram.model = go.Model.fromJson(currentString);
Nothing happens at all whereas when I try to load a normal model , it works.
Please help, this is quite critical as my dissertation is dependent on this very functionality.
simon
August 8, 2016, 2:21am
2
In the code you provided, currentString is not a string, it is a JavaScript object (that doesn’t seem completely formed)
Make sure it is a string.
I am straightaway using the string saved from toJson without any changes:
{ “class”: “go.TreeModel”,\n “nodeDataArray”: [ \n{“key”:0, “text”:“Mind Map”, “loc”:“0 0”},\n{“text”:“idea”, “parent”:0, “key”:-2, “loc”:“78.22705078125 0”},\n{“text”:“idea”, “parent”:-2, “key”:-3, “loc”:“128.22705078125 0”}\n ]}
The string was created by:
var modelAsText = myDiagram.model.toJson();
and then saved to Firebase.
simon
August 8, 2016, 3:03am
4
That’s not a string as-is, though. Try wrapping it in single-quotes to make it a string
currentString = '{ \"class\": \"go.TreeModel\",\n \"nodeDataArray\": [ \n{\"key\":0, \"text\":\"Mind Map\", \"loc\":\"0 0\"},\n{\"text\":\"idea\", \"parent\":0, \"key\":-2, \"loc\":\"78.22705078125 0\"},\n{\"text\":\"idea\", \"parent\":-2, \"key\":-3, \"loc\":\"128.22705078125 0\"}\n ]}';
Not working Simon.
Alright , so I converted the object received from toJson to a string by calling
var mmodelAsText = myDiagram.model.toJson();
var modelAsText=JSON.stringify(mmodelAsText);
which produces :
“”{ \“class\”: \“go.TreeModel\”,\n \“nodeDataArray\”: [ \n{\“key\”:0, \“text\”:\“Mind Map\”, \“loc\”:\“0 0\”},\n{\“text\”:\“idea\”, \“parent\”:0, \“key\”:-2, \“loc\”:\“78.22705078125 0\”}\n ]}""
and after removing all \,\n and the “” at the end as
{ “class”: “go.TreeModel”,“nodeDataArray”: [ {“key”:0, “text”:“Mind Map”, “loc”:“0”},{“text”:“idea”, “parent”:0, “key”:-2, “loc”:“78.22705078125 0”} ]}
it worked… But the documentation says that toJSON produces a string but it is not.
Well, but how can i do this conversion programatically?
simon
August 8, 2016, 3:41am
6
Yes it is. You can test it out yourself by feeding the results right back in:
myDiagram.model = go.Model.fromJSON(myDiagram.model.toJSON());
What you are getting from your server may or may not be a string, but what GoJS produces is definitely a string.
My bad (inexperienced programmer), it has to be the server then or maybe android because I am sending the data from js in webview to android then to server.
So I will try to make it work by editing the String, using replace or replaceall for \ and \n.
Thank You Simon.