I'm consuming a WCF service that returns JSON results wrapped inside the 'd' root element. The JSON response looks like this:
{"d":[
{
"__type":"DiskSpaceInfo:#Diagnostics.Common",
"AvailableSpace":38076567552,
"Drive":"C:",
"TotalSpace":134789197824
},
{
"__type":"DiskSpaceInfo:#Diagnostics.Common",
"AvailableSpace":166942183424,
"Drive":"D:",
"TotalSpace":185149157376
}
]}
I don't want to use dynamic typing, I have my class Diagnostics.Common.DiskSpaceInfo that I want to use when deserializing.
I am using Json.NET (Netwonsoft JSON).
The question is how to tell it to ignore the root element (that 'd' element) and parse what is inside.
The best solution I have so far is to use an anonymous type:
DiskSpaceInfo[] result = JsonConvert.DeserializeAnonymousType(json, new
{
d = new DiskSpaceInfo[0]
}).d;
this actually works but I don't like it very much. Is there another way? What I would like is something like:
DiskSpaceInfo[] result = JsonConvert.Deserialize(json, skipRoot: true);
or something like that...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…