I'm not an expert with json, actually I started last week working with it and I found myself having trouble trying to get map the following Json into a class.
S far I have copy the data from the browser into different Json files (isolate data to see the problem) and I realize that the property Config mess the deserialization process. If I (1) grab the entire data and (2) put it into a file and (3) add the extension .json and then later (4) just erase the Config property, everything works like a charm. But I want to be able to deserialize the whole thing.
For reading the url I'm using Flurl to generate a string from the response as I mentioned in a previous post using the GetStringAsync()
and for generating the Classes I just paste the response from Flurl or Postman into a Json2Csharp converter. Now for Deserialization I have try the following using Json.Net.
//Test 1
string cleanseStr = Regex.Unescape(FlurlResponse);
var myObj = JsonConvert.DeserializeObject<MyModel>(cleanseStr );
//Test 2
FlurlResponse= FlurlResponse.Replace("\\\\\\", "\\\\");
FlurlResponse= FlurlResponse.Replace("\\\\", "\\");
var myObj = JsonConvert.DeserializeObject<MyModel>(FlurlResponse);```
//Test 3
FlurlResponse= FlurlResponse.Replace("\\\\\\", "\\\\");
string cleanseStr = Regex.Unescape(FlurlResponse);
var myObj = JsonConvert.DeserializeObject<MyModel>(cleanseStr );
So far, I had no luck. I get errors at the beginning of the json or at the end saying that "cannot convert string into [MyModel]". I also took the response value from Test1 and Test2 (if I'm not istaken) and add it to this unserializer using "Json" to "Unserialized print_r" as my settings and compare them to my deserialization by hand (before using JsonConvert.DeserializeObject
) and I got the same result.
At the moment I'm stuck thinking what I'm missing in order to correctly deserialize the HTML string within the Json from the Config property. Probably someone that had face the same problem in the past can help me deserialize this or give any advice.
Error: Newtonsoft.Json.JsonSerializationException: 'Error converting value blah and 4millions characters later... "value":"<span fontWeight=\" to type TestingAPI.MyModel'. Path '', line 1, position 250 (for Test2) or 1950 (for Test1 and 3, 1950 means the end of the file).
"{\"Config\":[{\"id\":1,\"description\":\"Title\",\"value\":\"blah, blah\"},{\"id\":2,\"description\":\"Dislcaimer\",\"value\":\"<span fontWeight=\\\"bold\\\"> blah- </span>\\r\\n<br/><br/>blah 101 bl.ah. <span fontWeight=\\\"bold\\\"> blah (blah) blah 101 blah\\r\\n blah blah (blah)</span> blah, blah,\\r\\n blah. blah.\\r\\n blah, blah. blah, blah\\r\\n blah.\\r\\n<br/><br/>blah, blah, blah, blah blah\\r\\n blah blah-ah blah. blah (bl-ah blah, bl-ah blah, bl-ah blah, and >blah)\\r\\n blah.\\r\\n<br/><br/>blah, blah\\r\\n blah. \\r\\n<br/><br/>blah:<a
href=\\\"http://blah.blah.blah/blah/blah/blah.htm#blah\\\">http://blah.blah.blah/blah/blah/blah.htm#blah</a>\"}]}"
I solved my problem in a not very pleasant way, manual deserialization:
string cleanseStr= Regex.Unescape(FlurlResponseString);
cleanseStr= cleanseStr.Replace("\r\n", "");
cleanseStr= cleanseStr.Replace("\\\\\\", "\\\\");
cleanseStr= cleanseStr.Replace("=\"", "=\\\"");
cleanseStr= cleanseStr.Remove(0, 1);
cleanseStr= cleanseStr.Remove(scapedJson.Length - 1, 1);
MyModel _myModel= new MyModel();
JsonConvert.PopulateObject(cleanseStr, _myModel);
The correct way is proposed by @BrianRogers, thank you @BrianRogers.
I think the real problem here is that the original JSON is double-serialized. That is, it was serialized from an object to JSON and then that JSON string was serialized again, resulting in extra backslashes and quotes in the string.
So, to deserialize it, you need to do the reverse: deserialize the downloaded double-serialized JSON to a normal JSON string, then deserialize that string to an object.
I was able to do this successfully using the following code (Flurl.Http + Json.Net):
string json = await "https://gis.cdc.gov/grasp/fluView6/GetFlu6AllDataP".GetStringAsync();
json = JsonConvert.DeserializeObject<string>(json);
MyModel model = JsonConvert.DeserializeObject<MyModel>(json);
With the following model classes:
public class MyModel
{
public List<Group> Group { get; set; }
public List<Season> Season { get; set; }
public List<Age> Age { get; set; }
public List<VirusType> VirusType { get; set; }
public List<VirusData> VirusData { get; set; }
public List<Config> Config { get; set; }
public List<LastWeek> LastWeek { get; set; }
}
public class Group
{
public string Name { get; set; }
public int ID { get; set; }
}
public class Season
{
public int SeasonID { get; set; }
public string Description { get; set; }
public int StartWeek { get; set; }
public int EndWeek { get; set; }
public bool Enabled { get; set; }
public string Label { get; set; }
public bool ShowLabType { get; set; }
}
public class Age
{
public int AgeID { get; set; }
public string Label { get; set; }
public string Color_HexValue { get; set; }
public bool Enabled { get; set; }
}
public class VirusType
{
public int VirusID { get; set; }
public string Description { get; set; }
public string Label { get; set; }
public int StartMMWRID { get; set; }
public int EndMMWRID { get; set; }
public int DisplayOrder { get; set; }
public string ColorName { get; set; }
public string Color_HexValue { get; set; }
public int LabTypeID { get; set; }
public int SortID { get; set; }
}
public class VirusData
{
public int VisrusID { get; set; }
public int AgeID { get; set; }
public int Count { get; set; }
public int MMWRID { get; set; }
public int Seasonid { get; set; }
public int PublishYearWeekID { get; set; }
public string LoadDateTime { get; set; }
}
public class Config
{
public int ID { get; set; }
public string Description { get; set; }
public string Value { get; set; }
}
public class LastWeek
{
public int MMWRID { get; set; }
public DateTime WeekEnd { get; set; }
public int WeekNumber { get; set; }
public DateTime WeekStart { get; set; }
public int Year { get; set; }
public string YearWeek { get; set; }
public int SeasonID { get; set; }
}