使用嵌套数组转换DeserializeObject

本文关键字:转换 DeserializeObject 数组 嵌套 | 更新日期: 2023-09-27 18:17:11

我有一个模型,它包含一个不同对象类型的数组。父对象反序列化得很好,但嵌套数组总是失败。

这些是我的对象:

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'"}"

如果我单独解析对象,它可以正常工作但我似乎不能让它只解析字符串

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

使用嵌套数组转换DeserializeObject

问题是,JSON将AssociatedFacilities编码为字符串。试一试:

var 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""
}";
JsonConvert.DeserializeObject<VenueRoomViewModel>(json);

您的json与https://dotnetfiddle.net/znMYDZ中的json不同Json中的模型应该是:

public class RootObject
{
    public int State { get; set; }
    public string No { get; set; }
    public string Name { get; set; }
    public string HourCost { get; set; }
    public string DayCost { get; set; }
    public string MaxDelegate { get; set; }
    public string SpecialNotes { get; set; }
    public string Status { get; set; }
    public string AssociatedFacilities { get; set; }
    public string VenueNo { get; set; }
}
或者如果你想使用你的模型Json应该像你的例子:
    string json = @"
        {""Name"":""Test Room"",
            ""HourCost"":1.72,
            ""DayCost"":12.55,
            ""MaxDelegate"":10,
            ""SpecialNotes"":""This room has been added in the unit tests"",
            ""Status"":""A"",
            ""AssociatedFacilities"":[{
                ""FacilityStatus"":1,
                ""FacilityNo"":""4"",
                ""FacilityName"":""Practical Area""},
                {""FacilityStatus"":1,
                ""FacilityNo"":""1"",
                ""FacilityName"":""Overhead Projector""}],
            ""No"":2352,
            ""EditCounter"":0,
            ""VenueNo"":2,
            ""State"":2}";