JorgeAM
JorgeAM

Reputation: 91

How Can I Deserialize this JSON string?

I have been trying to deserilize a JSON object to C# class. My JSON Objetc (when I have read it from Request.InputStream) is the 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 I have this 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 I want to use this 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 I use the last code, the dictionary have one item for each (string key) month (each iteration of listadoMeses), but each instance for Mes class has both properties NULL.

How could I do with the last classes?

The code I use to Deserialize is the following (but this information, I suppose, is useless, because on both cases are the 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);

Upvotes: 4

Views: 108

Answers (3)

mybirthname
mybirthname

Reputation: 18127

Use Newtonsoft.Json. Here how your class should look like !

public class Enero
{
    public string valor { get; set; }
    public string almacenado { get; set; }
}

public class Febrero
{
    public string valor { get; set; }
    public string almacenado { get; set; }
}

public class Mayo
{
    public string valor { get; set; }
    public string almacenado { get; set; }
}

public class Agosto
{
    public string valor { get; set; }
    public string almacenado { get; set; }
}

public class Noviembre
{
    public string valor { get; set; }
    public string almacenado { get; set; }
}

public class ListadoMeses
{
    public Enero Enero { get; set; }
    public Febrero Febrero { get; set; }
    public Mayo Mayo { get; set; }
    public Agosto Agosto { get; set; }
    public Noviembre Noviembre { get; set; }
}

public class RootObject
{
    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 ListadoMeses listadoMeses { get; set; }
}

How you deserialize the json to object.

RootObject result = JsonConvert.DeserializeObject<RootObject>(json);

Here Dotnet Fiddle

Upvotes: 1

thepirat000
thepirat000

Reputation: 13114

Your properties on Mes class are private. Add the public access modifier to the properties.

Change it to this:

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

Upvotes: 3

Alec.
Alec.

Reputation: 5525

I don't like JavaScriptSerializer, I much prefer to use Json.net from newtonsoft

This can be obtained via nu get:

> Install-Package Newtonsoft.Json -Version 9.0.1

Upvotes: 2

Related Questions