ihatemash ihatemash - 3 months ago 16
JSON Question

C# class that would represent JSON data

what would the C# class definition look like to represent this JSON data?

{
"accountId": "101",
"website": "www.example.com",
"alternateWebsites": [
{
"website": "site2.example.com"
}
],
"email": "contact@mysite.com",
"alternateEmails": [
{
"email": "sales@example.com"
}
],
"address": {
"street": "234 Main Street",
"city": "San Diego",
"postalCode": "92101",
"state": "CA"
},
"rankingKeywords":
[{
"keyword": "Coffee",
"localArea": "Sacramento, CA"
}]
}

Answer

You can use a site like this http://jsonutils.com/

where you paste in your json and it constructs your classes for you. The result of your JSON produced...

public class AlternateWebsite
{
    public string website { get; set; }
}

public class AlternateEmail
{
    public string email { get; set; }
}

public class Address
{
    public string street { get; set; }
    public string city { get; set; }
    public string postalCode { get; set; }
    public string state { get; set; }
}

public class RankingKeyword
{
    public string keyword { get; set; }
    public string localArea { get; set; }
}

public class Root
{
    public string accountId { get; set; }
    public string website { get; set; }
    public IList<AlternateWebsite> alternateWebsites { get; set; }
    public string email { get; set; }
    public IList<AlternateEmail> alternateEmails { get; set; }
    public Address address { get; set; }
    public IList<RankingKeyword> rankingKeywords { get; set; }
}