作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 json.net 将 json 字符串反序列化为 C# 上的新列表。
当我进行直接反序列化时,我得到的一些属性为 null,因为我的列表中有不同的对象。
所以我想为该任务创建一个“翻译器”,构建通用对象并设置我的对象的属性。
这是我的进步..
CitasProfesorWeb.JavaService.AgendaWSService service =
new JavaService.AgendaWSService();
JsonTextReader reader;
private void cargaDatos()
{
String lista = service.obtenerCitasNuevas(2);
reader = new JsonTextReader(new StringReader(lista));
while (reader.Read())
{
//here i want to read the attributes or objects
}
}
我已尝试使用 JsonConvert.PopulateObject(reader,cita) 但我收到一条错误消息,指出我的参数无效。
--编辑--
这是我收到的字符串:
[{"idCita":6,"fechaSolicitud":"2012/4/20","fechaCita":"2012/4/19","horario":{"idHorario":1,"fechaInicio":"2012/1/16","fechaHoy":1337281297199,"fechaFin":"2012/5/30","nombre":"Enero-Mayo 2012","profesor":{"idProfesor":2, "nombre":"Guillermo", "apellido":"Salazar", "nomUsuario":"g.salazar", "email":"g.salazar@itson.mx", "ubicacion":"LV323", "descripcion":"Profesor Interino"}},"alumno":{"idAlumno":1, "nombre":"Jhonatan", "apellido":"Romero", "nomUsuario":" jromero", "email":"jhonatanromgggggh", "carrera":"LSIA" },"asunto":"asesorias","status":"0","horaCita":"10:0"}, {"idCita":7,"fechaSolicitud":"2012/4/20","fechaCita":"2012/4/27","horario":{"idHorario":1,"fechaInicio":"2012/1/16","fechaHoy":1337281297199,"fechaFin":"2012/5/30","nombre":"Enero-Mayo 2012","profesor":{"idProfesor":2, "nombre":"Guillermo", "apellido":"Salazar", "nomUsuario":"g.salazar", "email":"g.salazar@itson.mx", "ubicacion":"LV323", "descripcion":"Profesor Interino"}},"alumno":{"idAlumno":1, "nombre":"Jhonatan", "apellido":"Romero", "nomUsuario":" jromero", "email":"jhonatanromgggggh", "carrera":"LSIA" },"asunto":"asesorias","status":"0","horaCita":"11:0"}, {"idCita":11,"fechaSolicitud":"2012/4/20","fechaCita":"2012/4/20","horario":{"idHorario":1,"fechaInicio":"2012/1/16","fechaHoy":1337281297199,"fechaFin":"2012/5/30","nombre":"Enero-Mayo 2012","profesor":{"idProfesor":2, "nombre":"Guillermo", "apellido":"Salazar", "nomUsuario":"g.salazar", "email":"g.salazar@itson.mx", "ubicacion":"LV323", "descripcion":"Profesor Interino"}},"alumno":{"idAlumno":1, "nombre":"Jhonatan", "apellido":"Romero", "nomUsuario":" jromero", "email":"jhonatanromgggggh", "carrera":"LSIA" },"asunto":"asesorias","status":"0","horaCita":"17:0"}, {"idCita":12,"fechaSolicitud":"2012/4/27","fechaCita":"2012/5/3","horario":{"idHorario":1,"fechaInicio":"2012/1/16","fechaHoy":1337281297199,"fechaFin":"2012/5/30","nombre":"Enero-Mayo 2012","profesor":{"idProfesor":2, "nombre":"Guillermo", "apellido":"Salazar", "nomUsuario":"g.salazar", "email":"g.salazar@itson.mx", "ubicacion":"LV323", "descripcion":"Profesor Interino"}},"alumno":{"idAlumno":1, "nombre":"Jhonatan", "apellido":"Romero", "nomUsuario":" jromero", "email":"jhonatanromgggggh", "carrera":"LSIA" },"asunto":"tesis","status":"0","horaCita":"12:0"}, {"idCita":15,"fechaSolicitud":"2012/5/11","fechaCita":"2012/4/20","horario":{"idHorario":1,"fechaInicio":"2012/1/16","fechaHoy":1337281297200,"fechaFin":"2012/5/30","nombre":"Enero-Mayo 2012","profesor":{"idProfesor":2, "nombre":"Guillermo", "apellido":"Salazar", "nomUsuario":"g.salazar", "email":"g.salazar@itson.mx", "ubicacion":"LV323", "descripcion":"Profesor Interino"}},"alumno":{"idAlumno":1, "nombre":"Jhonatan", "apellido":"Romero", "nomUsuario":" jromero", "email":"jhonatanromgggggh", "carrera":"LSIA" },"asunto":"Tesis","status":"0","horaCita":"10:0"}]
这是我的课:
public class Cita
{
Profesor profesor;
public Profesor Profesor
{
get { return profesor; }
set { profesor = value; }
}
Alumno alumno;
public Alumno Alumno
{
get { return alumno; }
set { alumno = value; }
}
DateTime inicioCita;
public DateTime InicioCita
{
get { return inicioCita; }
set { inicioCita = value; }
}
String asunto;
public String Asunto
{
get { return asunto; }
set { asunto = value; }
}
String lugar;
public String Lugar
{
get { return lugar; }
set { lugar = value; }
}
int status;
public int Status
{
get { return status; }
set { status = value; }
}
DateTime fechaSolicitud;
public DateTime FechaSolicitud
{
get { return fechaSolicitud; }
set { fechaSolicitud = value; }
}
}
最佳答案
我会使用 dynamic
来解析你的 json 字符串,如下所示(无需声明 cita
,horario
,profesor
,校友
类(class))
dynamic dynObj = JsonConvert.DeserializeObject(json);
foreach (var cita in dynObj)
{
Console.WriteLine("{0} {1} {2}",
cita.horario.profesor.apellido,
cita.alumno.nombre,
cita.fechaSolicitud
);
}
关于c# - 如何将 Json 数组反序列化为不同的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10641748/
出于好奇,我尝试了一些原型(prototype)制作,但似乎只允许在第一个位置使用子例程的原型(prototype) &。 当我写作时 sub test (&$$) { do_somethin
我需要开发一个类似于 Android Play 商店应用程序或类似 this app 的应用程序.我阅读了很多教程,发现几乎每个教程都有与 this one 类似的例子。 . 我已经开始使用我的应用程
考虑一个表示“事件之间的时间”的列: (5, 40, 3, 6, 0, 9, 0, 4, 5, 18, 2, 4, 3, 2) 我想将这些分组到 30 个桶中,但桶会重置。期望的结果: (0, 1,
我是一名优秀的程序员,十分优秀!