[英]How Can I Deserialize this JSON string?
I have been trying to deserilize a JSON object to C# class. 我一直在尝试将JSON对象deserilize到C#类。 My JSON Objetc (when I have read it from Request.InputStream) is the following: 我的JSON Objetc(当我从Request.InputStream中读取它时)如下:
{
"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 . 遗憾的是,如果我使用最后一个代码,字典每个(字符串键)月有一个项(listadoMeses的每次迭代),但Mes类的每个实例都具有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);
Your properties on Mes class are private. 您在Mes类上的属性是私有的。 Add the public access modifier to the properties. 将公共访问修饰符添加到属性。
Change it to this: 把它改成这个:
public class Mes
{
public string valor;
public string almacenado;
}
I don't like JavaScriptSerializer, I much prefer to use Json.net from newtonsoft 我不喜欢JavaScriptSerializer,我更喜欢使用newtonsoft的Json.net
This can be obtained via nu get: 这可以通过nu get获得:
> Install-Package Newtonsoft.Json -Version 9.0.1
Use Newtonsoft.Json . 使用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. 如何将json反序列化为对象。
RootObject result = JsonConvert.DeserializeObject<RootObject>(json);
Here Dotnet Fiddle 这里有Dotnet Fiddle
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.