简体   繁体   English

如何从json创建一个c#对象

[英]How to make an c# object from json

I get the following response from a webservice: 我从Web服务得到以下响应:

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

How must the json-object look like to deserialize this? json-object必须如何反序列化?

Or is there another way to get the values of the properties? 或者是否有其他方法来获取属性的值?

With the JSON.NET library it's pretty trivial: 使用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; }
}

and then: 然后:

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);

In this example I have used a Dictionary<string, Data> object to model the 2 complex keys ( foo.hugo.info and foo.Fritz.Task ) because they contain names that cannot be used in a .NET member. 在这个例子中,我使用了Dictionary<string, Data>对象来建模2个复合键( foo.hugo.infofoo.Fritz.Task ),因为它们包含不能在.NET成员中使用的名称。

If you're using VS2012 or above you can do the following: 如果您使用的是VS2012或更高版本,则可以执行以下操作:

Edit > Paste Special > Paste JSON As Classes

With your example, this results in: 在您的示例中,这会导致:

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; }
}

Check out this site: http://json2csharp.com/ 看看这个网站: http//json2csharp.com/

Paste in the json string and it will generate classes for you. 粘贴在json字符串中,它将为您生成类。 I usually use this in hand with JSON.NET to deserialize an instance of the Root Object. 我通常在JSON.NET中使用它来反序列化根对象的实例。

You can use DataContractJsonSerializer 您可以使用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); 
    }

Edit: same class for Info and Task 编辑: InfoTask相同类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM