I am using JSON.NET to deserialize some JSON responses that I have. I have been successful up to now. In order for JSON.NET to properly deserialize the objects the field name in the class needs to be called exactly as in JSON. The problem is I have some fields that have funky characters in their name that I can't use in C# like {"(.
Does anyone know how to rename the fields so they get mapped properly?
Here is a short example of what works.
JSON input:
{
"contact_id": "",
"status": "Partial",
"is_test_data": "1",
"datesubmitted": "2013-10-25 05:17:06"
}
Deserialized class:
class DeserializedObject
{
public string contact_id;
public string status;
public int is_test_data;
public DateTime datesubmitted;
}
Deserialization:
var deserialized = JsonConvert.DeserializeObject<DeserializedObject>(jsonInput);
This gets mapped properly. The problem starts when I try to process the following field:
{
"contact_id": "",
"status": "Partial",
"is_test_data": "1",
"datesubmitted": "2013-10-25 05:17:06",
"[variable("STANDARD_GEOCOUNTRY")]": "Germany"
}
Deserialized class:
class Output
{
public string contact_id;
public string status;
public int is_test_data;
public DateTime datesubmitted;
public string variable_standard_geocountry; // <--- what should be this name for it to work?
}
I would appreciate any help.
The most popular names starting with C in the U.S. are currently Charlotte, Camila, Chloe, Claire, Caroline, Carter, Charles, Caleb, Christopher, and Cameron.
With JSON.NET, you'd just need to put a JsonProperty
attribute on the property, like:
class Output
{
public string contact_id;
public string status;
public int is_test_data;
public DateTime datesubmitted;
[JsonProperty("[variable(\"STANDARD_GEOCOUNTRY\")]")]
public string variable_standard_geocountry; // <--- what should be this name for it to work?
}
This will now deserialize. This assumes that your JSON is properly formatted with those quotation marks, like:
{
"contact_id": "",
"status": "Partial",
"is_test_data": "1",
"datesubmitted": "2013-10-25 05:17:06",
"[variable(\"STANDARD_GEOCOUNTRY\")]": "Germany"
}
You could use the JsonProperty attribute and set the name like this ...
`
class Output
{
public string contact_id;
public string status;
public int is_test_data;
public DateTime datesubmitted;
[JsonProperty("geocountry")]
public string variable_standard_geocountry; // <--- what should be this name for it to work?
}
` Here's a link to the documentation as well which may have other info you might find helpful. http://james.newtonking.com/json/help/?topic=html/JsonPropertyName.htm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With