0

以下是在我的模型上调用 toJson 方法生成的 Json 字符串:

currentString= { \"class\": \"go.TreeModel\",\n \"nodeDataArray\": [ \n{\"key\":0, \"text\":\"思维导图\", \"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 ]}

现在,当我尝试使用以下方法从该字符串加载数据时: myDiagram.model = go.Model.fromJson(currentString);

什么都没有发生,而当我尝试加载普通模型时,它可以工作。

请帮忙。

4

2 回答 2

1

String 很可能在服务器端发生了变化,Go js 没有将其读取为有效的 String ,因此我通过以下代码删除了无效字符以使其成为有效的 Json String 。此外,开头和结尾还有一些额外的引号。在此之后,它完美地工作。

    String trimCurrentString=currentString.replaceAll("\\\\n", " ");//removes /n from the string.
                String doubletrimCurrentString=trimCurrentString.replaceAll("\\\\","");//removes \from the  String.
                StringBuilder stringBuilder=new StringBuilder(doubletrimCurrentString);
                stringBuilder.deleteCharAt(0);//removes the quote from beginning
                stringBuilder.deleteCharAt((stringBuilder.length())-1);//removes the quote from the end
于 2016-08-10T15:10:57.170 回答
0

首先,currentString需要是一个字符串,而不是 JavaScript 对象。

其次,您需要在每个双引号之前停止使用反斜杠。

于 2016-08-09T02:35:07.993 回答