" is JSON string

非 Y 不嫁゛ 提交于 2019-12-11 11:28:39

问题


I have a JSON string which looks like this when displayed in an ASP.NET MVC page using @Model.JsonData

[
  {
    "id": 123,
    "text": "Consumer",
    "parent": "#";
  }
]

When I use the same @Model.JsonData in the JavaScript code it is encoded as:

[
  {
    "id": 123,
    "text": "Consumer",
    "parent": "#"
  }
]

Why does the JavaScript segment encode the double quotes?

When the double quotes are encoded the jstree plugin expecting JSON data does not work.

<script>
    $(function () {
        $('#jstree').jstree({
            'core': {
                'data': function ()
                {
                    var jsonTreeData = @Model.JsonTreeData;
                    return jsonTreeData;
                }
            }
        });
    });
</script>

Error message: "SCRIPT1015: Unterminated string constant"


回答1:


Replace &quot; with "

var data = JSON.parse("[{&quot;id&quot;: 123,&quot;text&quot;: &quot;Consumer&quot;,&quot;parent&quot;: &quot;#&quot;}]".replace(/&quot;/g,'"'));

console.log(data);


来源:https://stackoverflow.com/questions/27269135/quot-is-json-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!