Json Deserialization Is Not Working With Flurl
I try to consume an json APIwith Flurl from a website and I'm getting parse errors or conversion errors. I'm not sure what is my problem since is the first time I deal with json fi
Solution 1:
Let's look at the error message:
Path '', line 1, position 4814377. ---> System.ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.List`1[JsonTest.SeatTest].
This is telling you that somewhere in the JSON response (at the 4,814,377th character to be exact), there's a string where in array is expected. It appears that you're expecting both "Group"
and "Area"
properties to be arrays in the JSON, but somewhere deep down in the response at least one of them is a string.
One way to work around this is to parse the response string to a JArray
and build your strongly-typed list one by one, handling parsing errors along the way:
var results = newList<SeatTest>();
var arr = JArray.Parse(response);
foreach (var obj in arr) {
try {
var seat = obj.ToObject<SeatTest>();
results.Add(seat);
}
catch (Exception ex) {
// parsing error, inspect ex and obj.ToString(). log? ignore?
}
}
Post a Comment for "Json Deserialization Is Not Working With Flurl"