带有嵌套数组的JSONConvert DeserializeObject

时间:2016-11-10 10:17:32

标签: c# .net arrays json json.net

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

以下是我的目标:

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

2 个答案:

答案 0 :(得分:0)

问题是,您的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);

答案 1 :(得分:0)

你的json不同于https://dotnetfiddle.net/znMYDZ的那个 你的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}";