Create JSON with its own structure

1

I'm new to creating JSON, I'm trying to create a JSON in C # which I can do it in a basic way, that is, with the basic structure.

I need to obtain the following structure as a result:

{
    "Encabezado ": {
        "InstitucionId": "072",
        "FolioCliente": "A123"
    },
    "Datos": {
        "OCR": "3374999950186",
        "CIC": "",
        "ApellidoPaterno": "FLORES",
        "ApellidoMaterno": "CARLOS",
        "Nombre": "DE LA CRUZ",
        "AnioRegistro": "2008",
        "Emision": "2013",
        "ClaveElector": "LPVLJN90022109H700",
        "CURP": "LOVJ900221HDFPLN03",
        "Consentimiento": "1"
    },
    "Ubicación": {
        "PosicionSatelital ": {
            "Latitud": "13.800499",
            "Longitud": "-124.160198"
        }
    },
    "Minucias": [{
            "Tipo ": "ANSI ",
            "Nombre": "HUELLA2",
            "Valor": "145454544"
        },
        {
            "Tipo ": "ANSI ",
            "Nombre": "HUELLA7",
            "Valor": "145454544"
        }
    ]
}

I have my code for the JSON:

public class Validacion
    {
        public long InstitucionId { get; set; }
        public string FolioCliente { get; set; }

        public long OCR { get; set; }
        public long CIC { get; set; }
        public string ApellidoPaterno { get; set; }
        public string ApellidoMaterno { get; set; }
        public string Nombre { get; set; }
        public long AnioRegistro { get; set; }
        public long Emision { get; set; }
        public string ClaveElector { get; set; }
        public string CURP { get; set; }
        public long Consentimiento { get; set; }

        public double Latitud { get; set; }
        public double Longitud { get; set; }

        public string Tipo { get; set; }
        public string NombreMinucias { get; set; }
        public string Valor { get; set; }
        public string Tipo2 { get; set; }
        public string NombreMinucias2 { get; set; }
        public string Valor2 { get; set; }
    }

What do I have to add in my code to be able to structure it as I need it?

    
asked by Alejandro Reyes 20.10.2017 в 17:39
source

2 answers

3

The JSON you provide has different "levels" to group the information, so it is necessary to replicate that level structure:

The "rough" and quick way to do it is through anonymous types :

public class Validacion
{
    public object Encabezado { get; set; }
    public object Datos { get; set; }
    public object Ubicación { get; set; }
    public object[] Minucias { get; set; }
}

To generate it like this:

Validacion jsonObject = new Validacion()
{
    Encabezado = new { InstitucionId = "072", FolioCliente = "A123" },
    Datos = new {
        OCR = "3374999950186",
        CIC = "",
        ApellidoPaterno = "FLORES",
        ApellidoMaterno = "CARLOS",
        Nombre = "DE LA CRUZ",
        AnioRegistro = 2008,
        Emision = 2013,
        ClaveElector = "LPVLJN90022109H700",
        CURP = "LOVJ900221HDFPLN03",
        Consentimiento = "1"
    },
    Ubicación = new {
        PosiciónSatelital = new {
            Latitud = 13.800499,
            Longitud = -124.160198
        }
    },
    Minucias = new object[] {
        new {
            Tipo = "ANSI ",
            Nombre = "HUELLA2",
            Valor = "145454544"
        },
        new {
            Tipo = "ANSI ",
            Nombre = "HUELLA7",
            Valor = "145454544"
        }
    }
}

The correct way to do this is to create your classes (or structures). Here I put them with the suffix Model :
public class EncabezadoModel
{
    public string InstitucionId { get; set; }
    public string FolioCliente { get; set; }
}

public class DatosModel
{
    public string OCR { get; set; }
    public string CIC { get; set; }
    public string ApellidoPaterno { get; set; }
    public string ApellidoMaterno { get; set; }
    public string Nombre { get; set; }
    public int AnioRegistro { get; set; }
    public int Emision { get; set; }
    public string ClaveElector { get; set; }
    public string CURP { get; set; }
    public string Consentimiento { get; set; }
}

public class PosicionSatelitalModel
{
    public double Latitud { get; set; }
    public double Longitud { get; set; }
}

public class UbicaciónModel
{
    public PosicionSatelitalModel PosiciónSatelital { get; set; }
}

public class MinuciasModel
{
    public string Tipo { get; set; }
    public string Nombre { get; set; }
    public string Valor { get; set; }
}

And finally the Validación model would look like this:

public class ValicacionModel
{
    public EncabezadoModel Encabezado { get; set; }
    public DatosModel OCR { get; set; }
    public UbicaciónModel Ubicación { get; set; }
    public List<MinuciasModel> Minucias { get; set; }
}

to invoke it like this:

ValidacionModel jsonObject = new ValidacionModel();
jsonObject.Encabezado = new EncabezadoModel()
{
    InstitucionId = "072",
    FolioCliente = "A123"
};
jsonObject.Datos = new DatosModel()
{
    OCR = "3374999950186",
    CIC = "",
    ApellidoPaterno = "FLORES",
    ApellidoMaterno = "CARLOS",
    Nombre = "DE LA CRUZ",
    AnioRegistro = 2008,
    Emision = 2013,
    ClaveElector = "LPVLJN90022109H700",
    CURP = "LOVJ900221HDFPLN03",
    Consentimiento = "1"
};
jsonObject.Ubicación = new UbicaciónModel()
{
    PosiciónSatelital = new PosicionSatelitalModel() {
        Latitud = 13.800499,
        Longitud = -124.160198
    }
};
jsonObject.Minucias = new List<MinuciasModel>();
jsonObject.Minucias.Add(new MinuciasModel()
    {
        Tipo = "ANSI ",
        Nombre = "HUELLA2",
        Valor = "145454544"
    });
jsonObject.Minucias.Add(new MinuciasModel()
    {
        Tipo = "ANSI ",
        Nombre = "HUELLA7",
        Valor = "145454544"
    });
    
answered by 20.10.2017 / 18:10
source
0

I recommend using the Newtonsoft.Json library that you can install through NuGet.

More or less it would be like this.

Validacion oValidacion = new Validacion();
oValidacion.InstitucionId = "1";
oValidacion.FolioCliente = "123";
//Colocas los demás campos
string output = JsonConvert.SerializeObject(product);

Do not forget to import

using Newtonsoft.Json;
    
answered by 20.10.2017 в 18:11