Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

" is JSON string

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"

like image 798
Ranjith Venkatesh Avatar asked Mar 27 '26 13:03

Ranjith Venkatesh


1 Answers

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);
like image 192
Amy Avatar answered Mar 29 '26 03:03

Amy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!