0

I am working with a vendor who has a Restful API infrastructure that allows for "custom fields" on certain entities. When you create a custom field on one of their entities through their interface, you give the custom entity a name. However, in their API, you try to use that custom field, it is actually specified by an int (which represents a unique ID for that field). For example, the first custom field you create on an entity will have an ID of 1, and that is how you access or read that field from the API.

So in the below Json response sample, you'll see that the custom fields for enrollment are returned in the "enrollment fields" collection. 4 is the ID of the custom field, and ABC123 is the value populated in that custom field.

"enrollment_fields":{  
               "4":"ABC123"
            },

I'm not quite sure how I would handle this in my Object Class. Obviously I can't give an object member a number as a name. How would I go about deserializing this type of member into my object so that I can read the value ABC123?

  • A similar problem here: https://stackoverflow.com/questions/43351824/deserialize-json-with-numbers-as-property-names – Praneet Nadkar Oct 01 '18 at 05:47
  • You could try Dynamic / Expando objects. Haven't tried with an integer as property name though, but they might have a handling for it. – M22an Oct 01 '18 at 05:47
  • https://www.c-sharpcorner.com/article/json-serialization-and-deserialization-in-c-sharp/ –  Oct 01 '18 at 05:53
  • `enrollment_fields` I would create an obj `List enrollment_fields` with `EnrollementField { int Id; string Value;}`. And deserialise this part of the Json to a `Dictionary`. Looping over it with a foreach I will initialise the `List`. – Drag and Drop Oct 01 '18 at 06:15

2 Answers2

2

Since Object in C# can't have property as number, there is two way you can do

If your key is not going to change:

[JsonProperty(PropertyName = "4")]
public String Four { get; set; }

Else, you can use a Dictionary<string, string>.

Daniel Tran
  • 6,083
  • 12
  • 25
0

Here I create a sample console app for your demonstration purpose.

class Program
{
    static void Main(string[] args)
    {
        //This is your sample json that comes from Rest API
        string json = @"
                        {'enrollment_fields':{  
                                             '4':'ABC123',
                                             '5': 'XYZ123',
                                             '6': 'PQR123'
                                             }
                        }";


        //Deserialize your json into Dictionary of dictionary.
        var obj = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(json);

        //Get your desired key from outer dictionary
        var enrollment_fields = obj["enrollment_fields"];

        //Loop on inner dictionary key to get values
        foreach (string key in enrollment_fields.Keys)
            //Here you get your dynamic key. e.g. 4,5,6
            Console.WriteLine(key + ": " + enrollment_fields[key]);

        Console.ReadLine();
    }
}

Note: Download Newtonsoft.json form Nuget Package Manager and in your program use namespace like using Newtonsoft.Json;.

Output:

enter image description here

er-sho
  • 9,581
  • 2
  • 13
  • 26