问题
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 "
with "
var data = JSON.parse("[{"id": 123,"text": "Consumer","parent": "#"}]".replace(/"/g,'"'));
console.log(data);
来源:https://stackoverflow.com/questions/27269135/quot-is-json-string