GreenEyedAndy
GreenEyedAndy

Reputation: 1525

How to make an c# object from json

I get the following response from a webservice:

{
  "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?

Or is there another way to get the values of the properties?

Upvotes: 10

Views: 294

Answers (4)

Ahmed KRAIEM
Ahmed KRAIEM

Reputation: 10427

You can use 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

Upvotes: 1

dav_i
dav_i

Reputation: 28107

If you're using VS2012 or above you can do the following:

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

Upvotes: 5

Darin Dimitrov
Darin Dimitrov

Reputation: 1038820

With the JSON.NET library it's pretty trivial:

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.

Upvotes: 7

stevepkr84
stevepkr84

Reputation: 1657

Check out this site: http://json2csharp.com/

Paste in the json string and it will generate classes for you. I usually use this in hand with JSON.NET to deserialize an instance of the Root Object.

Upvotes: 1

Related Questions