I'm a beginner using the Json.Net framework. I've looked at the samples, but can't seem to figure out how to do something very simple...
I have the following Ajax jQuery which retrieves a collection of ratings.
<div id="RatingContent">
</div>
@section scripts {
<script type="text/javascript">
$(function () {
$.ajax({
type: "POST",
url: "Ratings/Index",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// Insert the returned HTML into the <div>.
$('#RatingContent').html(msg.d);
}
});
})
</script>
}
My class object is as follows:
namespace AjaxCallModel
{
[Serializable, DataContract(IsReference = true)]
public partial class Rating
{
public Rating()
{
this.Restaurants = new HashSet<Restaurant>();
}
[DataMember]
[ScaffoldColumn(true)]
public int Id { get; set; }
[DataMember]
[Required, MaxLength(20, ErrorMessage = "UserName is required")]
public string UserName { get; set; }
[DataMember]
[Required, MaxLength(sizeof(short))]
[RegularExpression(@"\b[1-5]\b", ErrorMessage = "Only numbers are allowed from 1 to 5")]
public short RatingValue { get; set; }
[DataMember]
[Required]
[DataType(DataType.DateTime)]
public System.DateTime RatingDate { get; set; }
[DataMember]
[Required]
[DataType(DataType.MultilineText)]
public string Comments { get; set; }
[DataMember]
public virtual ICollection<Restaurant> Restaurants { get; set; }
}
}
The method in my Controller is as follows. I simply want to take the data (which is populated (3 records)) and place it in the HTML. The first part of the method successfully retrieves the data.
At this point, I am unsure of how to process the data and place it in the HTML.... When I run line 43, I get the following error. I think it's because I'm missing the "@" at the beginning of the collection.
Exception Details: Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: A. Path '', line 0, position 0.
Source Error:
Line 41: idtc.DateTimeFormat = "MM/dd/yyyy";
Line 42:
Line 43: List<Rating> ratings = await JsonConvert.DeserializeObjectAsync<List<Rating>>(rating.ToString());
Line 44:
Line 45: return ratings;
I would appreciate somebody "setting me straight" on what I'm doing wrong and also
1) the proper way to get data from the database (which I think I have correct - at the beginning of the method)) 2) The proper way to get data into the HTML object.
btw, I assume that serializing the data is taking the data from jQuery, for instance, and forming the json data in the client side object to update an object in the DB.
I also assume that de-serializing the data means basically what I'm trying to accomplish in this particular question.
I would appreciate a very simple example or link that can show me how to do both with an object.
public async Task<List<Rating>> Index()
{
IEnumerable<Rating> rating = await db.SelectRatingsAsync();
IsoDateTimeConverter idtc = new IsoDateTimeConverter();
idtc.DateTimeFormat = "MM/dd/yyyy";
List<Rating> ratings = await JsonConvert.DeserializeObjectAsync<List<Rating>>(rating.ToString());
return ratings;
}