我从webservice获得以下响应:
{ "data":{ "foo.hugo.info": { "path": "logon.cgi","minVersion": 1,"maxVersion": 2 },"foo.Fritz.Task": { "path": "Fritz/process.cgi","maxVersion": 1 } },"success": true }
json对象如何反序列化呢?
解决方法
与
JSON.NET
图书馆相当微不足道:
public class Root { public Dictionary<string,Data> Data { get; set; } public bool Success { get; set; } } public class Data { public string Path { get; set; } public int MinVersion { get; set; } public int MaxVersion { get; set; } }
接着:
string json = @"{ ""data"":{ ""foo.hugo.info"": { ""path"": ""logon.cgi"",""minVersion"": 1,""maxVersion"": 2 },""foo.Fritz.Task"": { ""path"": ""Fritz/process.cgi"",""maxVersion"": 1 } },""success"": true }"; Root root = JsonConvert.DeserializeObject<Root>(json);
在这个例子中,我使用了一个字典< string,Data>对象来建模2个复杂的键(foo.hugo.info和foo.Fritz.Task),因为它们包含不能在.NET成员中使用的名称.