I have a JSON return as below.
{
"id": 100,
"name": "employer 100",
"externalId": "100-100",
"networkId": 1000,
"address": {
"street-address": "1230 Main Street",
"locality": "Vancouver",
"postal-code": "V6B 5N2",
"region": "BC",
"country-name": "CA"
}
}
So I created class to deserialize the above json.
public class Employer
{
public int id { get; set; }
public string name { get; set; }
public string externalId { get; set; }
public int networkId { get; set; }
public Address address { get; set; }
}
public class Address
{
public string street_address { get; set; }
public string locality { get; set; }
public string postal_code { get; set; }
public string region { get; set; }
public string country_name { get; set; }
}
var response = _client.Execute(req);
return _jsonDeserializer.Deserialize <Employer> (response);
But I could not get street-address, postal-code and country-name from Json string. I think because of Json output keys contain ""-"" (As result of that I'm getting null).
So how could I resolve my issue ?
Use the DeserializeAs attribute on your properties:
[DeserializeAs(Name = "postal-code")]
public string postal_code { get; set; }
This allows you to set the same of the property in the json that maps to the property in your class, allowing the property to have a different name to the son.