gpt4 book ai didi

c# - 如何将 Json 数组反序列化为不同的对象?

转载 作者:行者123 更新时间:2023-11-30 21:07:09 24 4
gpt4 key购买 nike

我正在尝试使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com