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"
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"
Share Improve this question edited Dec 3, 2014 at 10:46 Ranjith Venkatesh asked Dec 3, 2014 at 10:22 Ranjith VenkateshRanjith Venkatesh 1,3623 gold badges22 silver badges59 bronze badges 3-
var json = JSON.parse('@Model.JsonData')
– Ehsan Sajjad Commented Dec 3, 2014 at 10:24 - The above ment does not work because the @Model.JsonTreeData encodes automatically and throws "SCRIPT1015: Unterminated string constant" – Ranjith Venkatesh Commented Dec 3, 2014 at 10:47
- possible duplicate of How to remove " from my Json in javascript? – Chaitanya Gadkari Commented Apr 23, 2015 at 10:19
2 Answers
Reset to default 4Replace "
with "
var data = JSON.parse("[{"id": 123,"text": "Consumer","parent": "#"}]".replace(/"/g,'"'));
console.log(data);
In your controller Pass Model.JsonData this way
Model.JsonData = new HtmlString("Your String or Json");
Add using Microsoft.AspNetCore.Html; if HtmlString is not accessible.