JMD JMD - 1 month ago 9
C# Question

Unexpected token in JSON

I have checked JSON string response of at
https://jsonformatter.curiousconcept.com/ 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
MessageBody
from function
getJSONData
to avoid it from serializing and remove binding code from design page for
MessageBody
then it works fine.

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

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

$scope.browseListing = function (strURL) {
$scope.getURL(strURL);
$http.post($scope.URL)
.then(function (response) {
$scope.Data = response.data;
if ($scope.IsMap)
$scope.LoadMapData();
if ($scope.IsDetails)
$scope.buildReportURL($scope.Data[0].ListingID);

}, function (response) {
$log.info(response);
});
};


And this is the html binding:

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

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

Answer

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: http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_StringEscapeHandling.htm

you may try other flags to get it working

Comments