Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to name c# class fields to be able deserialize json field names with invalid characters

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.

like image 233
bercik Avatar asked Oct 29 '13 21:10

bercik


People also ask

What is the most popular name starting with C?

The most popular names starting with C in the U.S. are currently Charlotte, Camila, Chloe, Claire, Caroline, Carter, Charles, Caleb, Christopher, and Cameron.


2 Answers

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"
}
like image 104
Joe Enos Avatar answered Sep 26 '22 02:09

Joe Enos


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

like image 26
Michael Murphy Avatar answered Sep 26 '22 02:09

Michael Murphy