JMD JMD - 1 year ago 138
C# Question

Unexpected token in JSON

I have checked JSON string response of at and it says that JSON string is valid.

Following is the function which I have used to serialize data to JSON string:

private string getJSONData()
obj_userSession = new UserSession();
DataTable dtRender = null;
DataView dvRender = null;

obj_userSession = new UserSession();
if (obj_userSession.LoginData != null && obj_userSession.EmailsDetails != null)
dvRender = new DataView(obj_userSession.EmailsDetails);
dtRender = dvRender.ToTable("EmailsDetails", false, "MessageDate", "SentFrom", "MessageBody", "SentTo", "MLSNumber");
return JsonConvert.SerializeObject(dtRender);
return "";

And here is the response from above function as JSON string:

"MessageDate": "2016-04-04T05:42:38.273",
"SentFrom": "Site Team",
"MessageBody": "<html>\r\n<head>\r\n\t<style type=\"text/css\">\r\n\t\t.c0 { font-family:'Arial';font-size:10.5pt; }\r\n\t\t.c1 { margin-left:0pt;margin-top:0pt;margin-right:0pt;margin-bottom:7.5pt; }\r\n\t</style>\r\n</head>\r\n<body class=\"c0\">\r\n<p class=\"c1\">Hi Joe, </p>\r\n<p class=\"c1\">Testing Site</p>\r\n<p class=\"c1\">--James</p>\r\n<p class=\"c1\"></p>\r\n</body>\r\n</html>\r\n",
"SentTo": "James",
"Number": ""

We does not getting any error in code but result not displayed in browser. And gets above mention error in developer tools in browser.

If I removed
from function
to avoid it from serializing and remove binding code from design page for
then it works fine.

What character I have to escape from
and how to do it?

This is AngularJS controller function which I have used to get data from:

$scope.browseListing = function (strURL) {
.then(function (response) {
$scope.Data =;
if ($scope.IsMap)
if ($scope.IsDetails)

}, function (response) {

And this is the html binding:

<tr ng-repeat="listing in Data">
<td colspan="6">
<table class="tblListingOuter">
<th style="width:20%;">
<th style="width:80%;">
<td><b>Date: </b>{{ listing.MessageDate }}</td>
<td><b>Message: </b></td>
<td><b>Sent By: </b>{{ listing.SentFrom }}</td>
<td rowspan="3">
<pre contenteditable="true" ng-bind-html="listing.MessageBody | unsafe"></pre>

<td><b>Sent To: </b>{{ listing.SentTo }}</td>
<td><b>MLSNO: </b>{{ listing.MLSNumber }}</td>

Answer Source

In Newtonsoft JsonSerializerSettings you have property StringEscapeHandling which specifies how strings are escaped when writing json. code below may work for you

var settings = new JsonSerializerSettings();
settings.StringEscapeHandling = StringEscapeHandling.EscapeHtml;
return JsonConvert.SerializeObject(dtRender, settings)

acceptable values for this property listed in documentation here:

you may try other flags to get it working

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download