Reputation: 1
Greetings I have the following problem it turns out that I try to deserialize a json with the newtonsoft library with the following line
Tramite body = JsonConvert.DeserializeObject < Tramite> (JsonBody);
Json which is sent:
{
"body":[
{
"id":3772,
"processInstanceId":"1416",
"tipoTramite":{
"id":1,
"descripcion":"Solicitud Patente Comercial (Municipalidad)"
},
"canal":{
"id":0,
"nombre":""
},
"definicionTramite":{
"id":282,
"tipoTramite":null,
"idEstado":0,
"version":3,
"idDepartamento":0,
"idArea":0,
"nombre":"Solicitud Patente Comercial (Municipalidad)",
"codigo":null,
"descripcion":null,
"deploymentId":null,
"configuracion":null,
"processId":null
},
"institucion":{
"id":1045,
"idInstitucionBase":null,
"tipoInstitucion":null,
"nombre":"Municipalidad de La Granja",
"codigo":"LAGRANJA",
"direccion":null,
"logoWeb":null,
"logoPdf":null,
"telefonoFijo":null,
"email":null,
"propietario":null
},
"idDepartamento":1,
"idArea":3,
"estado":{
"id":294,
"definicionTramite":{
"id":282,
"tipoTramite":null,
"idEstado":0,
"version":0,
"idDepartamento":0,
"idArea":0,
"nombre":null,
"codigo":null,
"descripcion":null,
"deploymentId":null,
"configuracion":null,
"processId":null
},
"codigo":"03",
"nombre":"En Proceso"
},
"cve":"3df31748ec86df4280ecfaa853ead07a8918b345",
"numeroSolicitud":"555",
"tarifa":0,
"rut":{
"numero":13639987,
"dv":"K"
},
"rutEmpresa":{
"numero":76291578,
"dv":"2"
},
"nombre":"Solicitud Patente Comercial (Municipalidad)",
"flagPagado":false,
"fechaCreacion":1508792522683,
"fechaModificacion":1508792809726,
"datosTramite":[
{
"id":0,
"key":"inmueble_direccion_numero",
"tipo":"string",
"data":"3334"
}
],
"deploymenId":"cl.corfo.municipiogenerico:bpm-patentegenerica:1.4",
"processId":"patenteGenerica",
"solicitante":"FÉLIX EDUARDO CIFUENTES CID",
"contribuyente":"NombreMock de Fantasia",
"keyConfiguracion":"patente-comercial-ampliacion-giro"
}
]
}
This is the object: https://i.sstatic.net/MTT9s.png
Clase que serializo:
public int id { get; set; }
public string processInstanceId { get; set; }
Dictionary<string, string> tipoTramite { get; set; }
Dictionary<string, string> canal { get; set; }
public DefinicionTramite definicionTramite { get; set; }
public Institucion institucion { get; set; }
public int idDepartamento { get; set; }
public int idArea { get; set; }
public Estado estado { get; set; }
public string cve { get; set; }
public string numeroSolicitud { get; set; }
public double tarifa { get; set; }
Dictionary<string, string> rut { get; set; }
Dictionary<string, string> rutEmpresa { get; set; }
public string nombre { get; set; }
public bool flagPagado { get; set; }
public string fechaCreacion { get; set; }
public string fechaModificacion { get; set; }
public List<DatosTramite> datosTramite { get; set; }
public string deploymenId { get; set; }
public string processId { get; set; }
public string solicitante { get; set; }
public string contribuyente { get; set; }
public string keyConfiguracion { get; set; }
class Definicion Tramite
public int id { get; set; }
public string tipoTramite { get; set; }
public int idEstado { get; set; }
public int version { get; set; }
public int idDepartamento { get; set; }
public int idArea { get; set; }
public string nombre { get; set; }
public string codigo { get; set; }
public string descripcion { get; set; }
public string deploymentId { get; set; }
public string configuracion { get; set; }
public string processId { get; set; }
class Institucion
public int id { get; set; }
public string idInstitucionBase { get; set; }
public string tipoInstitucion { get; set; }
public string nombre { get; set; }
public string codigo { get; set; }
public string direccion { get; set; }
public string logoWeb { get; set; }
public string logoPdf { get; set; }
public string telefonoFijo { get; set; }
public string email { get; set; }
public string propietario { get; set; }
class Estado
public int id { get; set; }
public DefinicionTramite definicionTramite { get; set; }
public string codigo { get; set; }
public string nombre { get; set; }
class Datos Tramite
public int id { get; set; }
public string key { get; set; }
public string tipo { get; set; }
public string data { get; set; }
These are all the classes that are involved in the deserialization of the json that I sent, I tried to deserialize a dataSet but it also could not because of the classes it contained (according to what I read in a stackoverflow question) if someone knows that I am bad or if there is another way of doing the deserialization of a json in C# helps me
Upvotes: 0
Views: 160
Reputation: 629
First of all, your C# model is not entirely same as your JSON. For Example, tipoTramite in your main class and Json object are different. Secondly, the reason why you are getting everything null here is you are sending an array of object in the json but at the server level, you are deserializing the object. You could do one of the following:
1.Send JSON as object and not as array like below, and then your code should work
{
"body":{
"id":3772,
"processInstanceId":"1416",
...
...
...
}
2.If you don't want to change the json, then for deserializing, you will have to create a class that contains the array of Tramite type and then use that class to deserialize the json:
public class TramiteJsonModel{
public Tramite[] Body {get;set;}
}
Now use the code below to deserialize the content:
TramiteJsonModel body = JsonConvert.DeserializeObject <TramiteJsonModel> (JsonBody);
Upvotes: 0
Reputation: 813
This worked for me:
JsonConvert.DeserializeObject<Rootobject>(json)
Here are the classes that I created:
public class Rootobject
{
public Body[] body { get; set; }
}
public class Body
{
public int id { get; set; }
public string processInstanceId { get; set; }
public Tipotramite tipoTramite { get; set; }
public Canal canal { get; set; }
public Definiciontramite definicionTramite { get; set; }
public Institucion institucion { get; set; }
public int idDepartamento { get; set; }
public int idArea { get; set; }
public Estado estado { get; set; }
public string cve { get; set; }
public string numeroSolicitud { get; set; }
public int tarifa { get; set; }
public Rut rut { get; set; }
public Rutempresa rutEmpresa { get; set; }
public string nombre { get; set; }
public bool flagPagado { get; set; }
public long fechaCreacion { get; set; }
public long fechaModificacion { get; set; }
public Datostramite[] datosTramite { get; set; }
public string deploymenId { get; set; }
public string processId { get; set; }
public string solicitante { get; set; }
public string contribuyente { get; set; }
public string keyConfiguracion { get; set; }
}
public class Tipotramite
{
public int id { get; set; }
public string descripcion { get; set; }
}
public class Canal
{
public int id { get; set; }
public string nombre { get; set; }
}
public class Definiciontramite
{
public int id { get; set; }
public object tipoTramite { get; set; }
public int idEstado { get; set; }
public int version { get; set; }
public int idDepartamento { get; set; }
public int idArea { get; set; }
public string nombre { get; set; }
public object codigo { get; set; }
public object descripcion { get; set; }
public object deploymentId { get; set; }
public object configuracion { get; set; }
public object processId { get; set; }
}
public class Institucion
{
public int id { get; set; }
public object idInstitucionBase { get; set; }
public object tipoInstitucion { get; set; }
public string nombre { get; set; }
public string codigo { get; set; }
public object direccion { get; set; }
public object logoWeb { get; set; }
public object logoPdf { get; set; }
public object telefonoFijo { get; set; }
public object email { get; set; }
public object propietario { get; set; }
}
public class Estado
{
public int id { get; set; }
public Definiciontramite1 definicionTramite { get; set; }
public string codigo { get; set; }
public string nombre { get; set; }
}
public class Definiciontramite1
{
public int id { get; set; }
public object tipoTramite { get; set; }
public int idEstado { get; set; }
public int version { get; set; }
public int idDepartamento { get; set; }
public int idArea { get; set; }
public object nombre { get; set; }
public object codigo { get; set; }
public object descripcion { get; set; }
public object deploymentId { get; set; }
public object configuracion { get; set; }
public object processId { get; set; }
}
public class Rut
{
public int numero { get; set; }
public string dv { get; set; }
}
public class Rutempresa
{
public int numero { get; set; }
public string dv { get; set; }
}
public class Datostramite
{
public int id { get; set; }
public string key { get; set; }
public string tipo { get; set; }
public string data { get; set; }
}
You need to see your JSON and figure out that body is an array and a root. Hence there must be a root class with body as an array. Create that and then deserialize. That must work
Upvotes: 1