10

我从网络服务中得到以下响应:

{
  "data":{
    "foo.hugo.info": {
      "path": "logon.cgi",
      "minVersion": 1,
      "maxVersion": 2
    },
    "foo.Fritz.Task": {
      "path": "Fritz/process.cgi",
      "minVersion": 1,
      "maxVersion": 1
    }
  },
  "success": true
}

json-object 必须如何反序列化?

还是有另一种方法来获取属性的值?

4

4 回答 4

7

有了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"",
      ""minVersion"": 1,
      ""maxVersion"": 1
    }
  },
  ""success"": true
}";
Root root = JsonConvert.DeserializeObject<Root>(json);

在此示例中,我使用了一个Dictionary<string, Data>对象来对 2 个复杂键 (foo.hugo.infofoo.Fritz.Task) 建模,因为它们包含不能在 .NET 成员中使用的名称。

于 2013-08-29T15:10:39.583 回答
5

如果您使用的是 VS2012 或更高版本,您可以执行以下操作:

Edit > Paste Special > Paste JSON As Classes

以您的示例为例,这将导致:

public class Rootobject
{
    public Data data { get; set; }
    public bool success { get; set; }
}

public class Data
{
    public FooHugoInfo foohugoinfo { get; set; }
    public FooFritzTask fooFritzTask { get; set; }
}

public class FooHugoInfo
{
    public string path { get; set; }
    public int minVersion { get; set; }
    public int maxVersion { get; set; }
}

public class FooFritzTask
{
    public string path { get; set; }
    public int minVersion { get; set; }
    public int maxVersion { get; set; }
}
于 2013-08-29T15:12:11.167 回答
1

看看这个网站:http: //json2csharp.com/

粘贴 json 字符串,它将为您生成类。我通常将它与 JSON.NET 一起使用来反序列化根对象的实例。

于 2013-08-29T15:04:56.650 回答
1

您可以使用DataContractJsonSerializer

    [DataContract]
    public class DetailedData
    {
        [DataMember(Name="path")]
        public string Path { get; set; }
        [DataMember(Name = "minVersion")]
        public int MinVersion { get; set; }
        [DataMember(Name = "maxVersion")]
        public int MaxVersion { get; set; }
    }

    [DataContract]
    public class Data
    {
        [DataMember(Name = "foo.hugo.info")]
        public DetailedData Info { get; set; }
        [DataMember(Name = "foo.Fritz.Task")]
        public DetailedData Task { get; set; }
    }

    [DataContract]
    public class RootObject
    {
        [DataMember(Name = "data")]
        public Data Data { get; set; }
        [DataMember(Name = "success")]
        public bool Success { get; set; }
    }

    static void Main(string[] args)
    {
        string json = "...";
        DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(RootObject));

        RootObject obj = (RootObject)js.ReadObject(new MemoryStream(Encoding.Unicode.GetBytes(json)));
        Console.WriteLine(obj.Data.Task.MaxVersion); 
    }

编辑:InfoTask

于 2013-08-29T15:29:06.997 回答