c# - JSONConvert DeserializeObject with nested array -
i have model contains , array of different object type. parent object deserializes fine nested array fails.
here objects:
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; } }
i try , parse json this:
venueroomviewmodel myvm = sonconvert.deserializeobject<venueroomviewmodel>(jsonmodel);
and here copy of json trying parse:
"{\"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\"}"
if parse objects individually works fine can't seem work parsing string
var converter = new expandoobjectconverter(); dynamic obj = jsonconvert.deserializeobject<expandoobject>(jsonmodel, converter); list<facilityviewmodel> facilities = jsonconvert.deserializeobject<list<facilityviewmodel>>(obj.associatedfacilities);
this parses nested array fine think json ok.
here's working https://dotnetfiddle.net/znmydz
the problem is, json encodes associatedfacilities
string. try:
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);
Comments
Post a Comment