I have a string of JSON that I'm trying to convert into a list. There is an empty array value which is breaking the deserialization (have tried removing it manually or changing to a number and it works). Was thinking I could try to replace the [] in the string, but is there a better way to work around this?
public class Tax
{
public string Id;
public string Name;
public string PathOfTerm;
public string Children;
public string Level;
public string RawTerm;
}
var exString = "[{\"Id\":\"12345\",\"Name\":\"aName\",\"PathOfTerm\":\"aTerm\",\"Children\":[],\"Level\":0,\"RawTerm\":null}]";
JavaScriptSerializer ser = new JavaScriptSerializer();
var taxData = ser.Deserialize<List<Tax>>(exString);
The value is an array, but your field is a string. I suggest you make it an array (or list) of the appropriate type - we can't tell what that type would be from your JSON, but perhaps you want a string array?
I'd also suggest using properties instead of public fields.
(If you can move to Json.NET, I'd generally recommend that over JavaScriptSerializer
, too...)
See more on this question at Stackoverflow