&安培; QUOT;是JSON字符串 [英] " is JSON string

查看:246
本文介绍了&安培; QUOT;是JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用@ Model.JsonData

 在ASP.NET MVC页面中显示时,我有一个这样的JSON字符串[
{
id:123,
text:Consumer,
parent:#;
}
]

当我使用相同的@ Model.JsonData JavaScript代码编码为:

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

为什么JavaScript段编码双引号? / p>

当双引号被编码时,jstree插件期待JSON数据不起作用。

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

错误消息:
SCRIPT1015:未终止的字符串常量

解决方案

& quot; 替换为



  var data = JSON.parse([{& quot; id& quot ;: 123,& quot; text& quot ;& quot;& quot;& quot; parent& quot ;:& quot;#& quot;}]替换(/& quot; / g,''));控制台.log(data);  


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:

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

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"

解决方案

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);

这篇关于&安培; QUOT;是JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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