Problem With Json Deserialization Of Html String Using Json.net
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
Solution 1:
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:
publicclassMyModel
{
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; }
}
publicclassGroup
{
publicstring Name { get; set; }
publicint ID { get; set; }
}
publicclassSeason
{
publicint SeasonID { get; set; }
publicstring Description { get; set; }
publicint StartWeek { get; set; }
publicint EndWeek { get; set; }
publicbool Enabled { get; set; }
publicstring Label { get; set; }
publicbool ShowLabType { get; set; }
}
publicclassAge
{
publicint AgeID { get; set; }
publicstring Label { get; set; }
publicstring Color_HexValue { get; set; }
publicbool Enabled { get; set; }
}
publicclassVirusType
{
publicint VirusID { get; set; }
publicstring Description { get; set; }
publicstring Label { get; set; }
publicint StartMMWRID { get; set; }
publicint EndMMWRID { get; set; }
publicint DisplayOrder { get; set; }
publicstring ColorName { get; set; }
publicstring Color_HexValue { get; set; }
publicint LabTypeID { get; set; }
publicint SortID { get; set; }
}
publicclassVirusData
{
publicint VisrusID { get; set; }
publicint AgeID { get; set; }
publicint Count { get; set; }
publicint MMWRID { get; set; }
publicint Seasonid { get; set; }
publicint PublishYearWeekID { get; set; }
publicstring LoadDateTime { get; set; }
}
publicclassConfig
{
publicint ID { get; set; }
publicstring Description { get; set; }
publicstring Value { get; set; }
}
publicclassLastWeek
{
publicint MMWRID { get; set; }
public DateTime WeekEnd { get; set; }
publicint WeekNumber { get; set; }
public DateTime WeekStart { get; set; }
publicint Year { get; set; }
publicstring YearWeek { get; set; }
publicint SeasonID { get; set; }
}
Post a Comment for "Problem With Json Deserialization Of Html String Using Json.net"