I am trying to create a JSON file to be injected through a WebService to a third party service, which will fill data in a dynamic form.
The issue is that the structure that I must respect, according to the provider's specifications, to give an example since the model has a greater number of values to transmit, is the following:
{ "f": "546",
"efs": { "1":"Ubicación",
"2":"Categoría",
"3":"Contacto",
"4":"Celular" } }
I've gone through several reference articles, but when creating the class I need I can't use numbers as labels/markers (if they are called that) and so I'm forced to use the following:
public class form_addsModel
{
public string f { get; set; }
public efsModel efs { get; set; }
}
public class efsModel
{
public string _1 { get; set; }
public string _2 { get; set; }
public string _3 { get; set; }
public string _4 { get; set; }
}
The expected result is not the right one, and the truth is that I don't know how feasible it is to build a JSON with those characteristics and restrictions set by the provider.
I am currently working in C# with Visual Studio 2012.
You can't name variables/properties as numbers, but you can use a data structure like a
IDictionary
:Within a
Dictionary
you can seamlessly recreate the structurekey/value
that reigns in JSON, and it gives you the flexibility to also use numbers to represent the "name" of any of the indexes it finds. In the example, nothing stops you from using a couple instead of aint - string
couplestring - string
and using it like this:If, as they commented, for these cases we would use a
Dictionary<int, string>
And to serialize it to a JSON string we use the https://www.newtonsoft.com/json library
Online example: https://dotnetfiddle.net/CpMStk