c# - How Can I Deserialize this JSON string? -


i have been trying deserilize json object c# class. json objetc (when have read request.inputstream) following:

{      "accion":"guardar",    "ejercicio":"2017",    "codigopieza":"13",    "solicitado":"61",    "solicitante":"46",    "listadomeses":{         "enero":{            "valor":"1",          "almacenado":"false"       },       "febrero":{            "valor":"",          "almacenado":"true"       },       "mayo":{            "valor":"2",          "almacenado":"true"       },       "agosto":{            "valor":"2",          "almacenado":"true"       },       "noviembre":{            "valor":"2",          "almacenado":"true"       }    } } 

this deserialize correctly if have class:

public class respuestausuario {         public string accion { get; set; }         public string ejercicio { get; set; }         public string codigopieza { get; set; }         public string solicitado { get; set; }         public string solicitante { get; set; }         public dictionary<string, dictionary<string, string>> listadomeses { get; set; }     } 

but want use classes:

public class respuestausuario {     public string accion { get; set; }     public string ejercicio { get; set; }     public string codigopieza { get; set; }     public string solicitado { get; set; }     public string solicitante { get; set; }     public dictionary<string, mes> listadomeses { get; set; } }  public class mes {     string valor;     string almacenado; } 

sadly, if use last code, dictionary have 1 item each (string key) month (each iteration of listadomeses), each instance mes class has both properties null.

how last classes?

the code use deserialize following (but information, suppose, useless, because on both cases same):

system.io.stream str = request.inputstream; str.position = 0; string datos = string.empty; using (streamreader reader = new streamreader(str, encoding.utf8)) {     datos = reader.readtoend(); }  javascriptserializer serializador = new javascriptserializer(); respuestausuario respuesta = (respuestausuario)serializador.deserialize<respuestausuario>(datos); 

your properties on mes class private. add public access modifier properties.

change this:

public class mes {     public string valor;     public string almacenado; } 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -