是否可以使用 JsonConvert.DeserializeObject 在列表中反序列化列表?
Object:
public class BusinessList
{
public long Number{ get; set; }
public string BusinessUnitCode { get; set; }
public List<string> Codes { get; set; }
}
Json:
{"Number":111121,"BusinessUnitCode":"ABC","Codes":["11111-2","14333-8"]}
Deserialization:
var dataList = (List<BusinessList>)JsonConvert.DeserializeObject(reciveObject, typeof(List<BusinessList>), jsonSerializerSettings);
错误:
"Expected a JsonObjectContract or JsonDictionaryContract for
type 'System.Collections.Generic.List`1
[PathResultsPortal.UI.Controllers.AnalyteDetails]',
got 'Newtonsoft.Json.Serialization.JsonArrayContract'."
如果您从业务列表对象中删除代码列表,它工作正常,它似乎无法处理列表中的列表,甚至可能吗?
public class BusinessList
{
[JsonProperty(PropertyName = "Number")]
public long Number{ get; set; }
[JsonProperty(PropertyName = "BusinessUnitCode ")]
public string BusinessUnitCode { get; set; }
[JsonProperty(PropertyName = "Codes ")]
public List<Codes> Codes { get; set; }
}
public class Codes
{
public string Code { get; set; }
}
试试这个