我有一個模型,其中包含和不同對象類型的數組。父對象反序列化正常,但嵌套數組不斷失敗。JSONConvert使用嵌套數組的DeserializeObject
這裏是我的對象:
public class VenueRoomViewModel
{
public string Name { get; set; }
public decimal HourCost { get; set; }
public decimal DayCost { get; set; }
public int MaxDelegate { get; set; }
public string SpecialNotes { get; set; }
public string Status { get; set; }
[JsonProperty("AssociatedFacilities")]
public List<FacilityViewModel> AssociatedFacilities { get; set; }
}
public class FacilityViewModel
{
public string FacilityName { get; set; }
public int FacilityNo { get; set; }
public string FacilityStatus { get; set; }
}
我嘗試和解析這樣的JSON:
VenueRoomViewModel myVM = sonConvert.DeserializeObject<VenueRoomViewModel>(jsonModel);
這裏是它試圖解析JSON的副本:
"{\"State\":1,\"No\":\"0\",\"Name\":\"test\",\"HourCost\":\"12\",\"DayCost\":\"12\",\"MaxDelegate\":\"12\",\"SpecialNotes\":\"blah blah blah\",\"Status\":\"A\",\"AssociatedFacilities\":\"[{\\\"FacilityStatus\\\":1,\\\"FacilityNo\\\":\\\"1\\\",\\\"FacilityName\\\":\\\"Overhead Projector\\\"},{\\\"FacilityStatus\\\":1,\\\"FacilityNo\\\":\\\"5\\\",\\\"FacilityName\\\":\\\"New Facility\\\"}]\",\"VenueNo\":\"2\"}"
如果我單獨解析對象,那麼它工作正常,但我似乎無法讓它工作只是解析條ng
var converter = new ExpandoObjectConverter();
dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(jsonModel, converter);
List<FacilityViewModel> facilities = JsonConvert.DeserializeObject<List<FacilityViewModel>>(obj.AssociatedFacilities);
這解析了嵌套數組,所以我認爲json是可以的。
下面是它的工作https://dotnetfiddle.net/znMYDZ
'FacilityStatus'是數據中的'int',但模型中的'string'? 'FacilityNo'與此相反。認爲這些類型需要交換。 – Lloyd