I am trying to deserialize a JSON object according to my model using the code below:
LoadData<MyModel>(Data.Stats, null);
public void LoadData<TModel>(string data, JsonSerializerSettings jsonSettings) where TModel : class
{
var mockData = JsonConvert.DeserializeObject<Collection<TModel>>(data, jsonSettings); // ERROR HERE
Context.SaveChanges();
}
However I am getting an error that reads
Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: {. Path '[0].Statistics', line 7, position 19.'
My JSON object is:
[
{
"Id": 3033,
"Grade": 3,
"Statistics": { //ERROR OCCURS ON THIS PROPERTY
"Avatar.Add": 1,
"TotalPlays": 36,
"Game.TotalPlays.Spell_Mem_Words": 27,
"Book.TotalReads.Count": 23,
"Game.TotalPlays.Count": 39,
"Character.TotalPlays.L": 23,
"Character.TotalPlays.E": 3,
"TotalPlays.Pick_Vocab": 16,
"Character.TotalPlays.R": 22
}
}
]
The Object Model is:
public class MyModel
{
public int Id { get; set; }
public int Grade { get; set; }
public string Statistics { get; set; }
}
Things I Have Tried
(1) Using json lint I have ensured that the json string is valid.
(2) In javascript serializing the object with back ticks surrounding it works. Backticks don't work in C# JS Fiddle
(3) Tried making the Statistics property in object model to use class called stats instead of string like
public class Stats
{
public string Label { get; set;}
public int Value { get; set; }
}
(4) Tried nearly all the answers on this SO post
Unfortunately I still have not solved this issue. Any ideas?