DeSerialize JSON from Web Service into Array of C# Objects?

Sep 14, 2011 at 12:06 PM

Hi,

I have a web service that is returning an array of objects in json form. I would like to convert this array to a list of c# objects.

 

Here is my C# Object declaration

public class  

WifiNet

{

 

public string mode { get; set ; }

 

public string frequency { get; set ; }

 

public string channel { get; set ; }

 

public string quality { get; set ; }

 

public string signal_level { get; set ; }

 

public string noise_level { get; set ; }

 

public string encryption { get; set ; }

 

public string essid { get; set ; }

 

public string pairwise_ciphers { get; set ; }

 

public string group_cipher { get; set ; }

 

public string auth_suites { get; set ; }

 

public string htcap { get; set ; }

 

public string mtik_name { get; set ; }

}

Here is a sample json array:

[        {
                "mac" : "12:24:99:71:96:82",
                "mode" : "Master",
                "frequency" : "2.422",
                "channel" : "3",
                "quality" : "24/94",
                "signal_level" : "-72",
                "noise_level" : "-91",
                "encryption" : "wpa2",
                "essid" : "IMDGuest",
                "pairwise_ciphers" : "CCMP TKIP"
                "group_cipher" : "TKIP",
                "auth_suites" : "PSK",
                "htcap" : 2,
                "mtik_name" : ""
        },
        {
                "mac" : "12:24:99:71:96:81",
                "mode" : "Master",
                "frequency" : "2.422",
                "channel" : "3",
                "quality" : "25/94",
                "signal_level" : "-71",
                "noise_level" : "-91",
                "encryption" : "wpa2",
                "essid" : "IMDFamily",
                "pairwise_ciphers" : "CCMP",
                "group_cipher" : "CCMP",
                "auth_suites" : "802.1x",
                "htcap" : 2,
                "mtik_name" : ""
        },
        {
                "mac" : "12:24:99:71:96:83",
                "mode" : "Master",
                "frequency" : "2.422",
                "channel" : "3",
                "quality" : "22/94",
                "signal_level" : "-74",
                "noise_level" : "-91",
                "encryption" : "wpa2",
                "essid" : "IMDN",
                "pairwise_ciphers" : "CCMP",
                "group_cipher" : "CCMP",
                "auth_suites" : "802.1x",
                "htcap" : 2,
                "mtik_name" : ""
        }

]

Can anyone offer  some code snippets that will help me understand how to use the Json.net library in c# to accomplish this.

TIA

 

public string mac { get; set ; }  

Oct 11, 2011 at 1:03 PM
Edited Oct 11, 2011 at 1:03 PM

Of course, this is fairly easy:

Newtonsoft.Json.JsonConvert.DeserializeObject<List<WifiNet>>( yourString );