Reputation: 2883
i'm trying to deserialize a json string into a new list on C# using json.net.
When i do a direct deserialization i get some attributes as null because i have different objects on my list.
So i want to create a "translator" for that task, building generic objects and setting the attributes of my objects.
this is my progress..
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
}
}
i have tried using JsonConvert.PopulateObject(reader,cita) but i'm getting an error message saying that i have invalid parameters.
--Edit--
This is the string that i'm receiving :
[{"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":"[email protected]", "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":"[email protected]", "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":"[email protected]", "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":"[email protected]", "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":"[email protected]", "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"}]
This is my class:
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; }
}
}
Upvotes: 2
Views: 3170
Reputation: 13360
Here's a generic class for converting JSON to objects (be sure to include System.Web.Script.Serialization
)
public static T JsonToObject<T>(string JsonData)
{
// Deserialize the JSON into the object
JavaScriptSerializer jss = new JavaScriptSerializer();
T rf = (T)jss.Deserialize(JsonData, typeof(T));
return rf;
}
To convert an object back to JSON, use this general class
public static string ObjectToJson<T>(T rf)
{
// Serialize the object as JSON
StringBuilder sb = new StringBuilder();
JavaScriptSerializer jss = new JavaScriptSerializer();
jss.Serialize(rf, sb);
return sb.ToString();
}
As an example of usage, you would use
string json = @"[{""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"":""[email protected]"", ""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"":""[email protected]"", ""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"":""[email protected]"", ""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"":""[email protected]"", ""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"":""[email protected]"", ""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""}]";
List<Cita> p = JsonToObject<List<Cita>>(json);
EDIT: You specifically mentioned JSON.NET, so the above code may not help you if you want to keep with that. But, to parse all the attributes of a JSON string, you will need a class that matches the JSON response string. From there, you can set attributes of other objects as necessary.
Upvotes: 0
Reputation: 116178
I would use dynamic
to parse your json string like below (without the need to declare cita
,horario
,profesor
,alumno
classes)
dynamic dynObj = JsonConvert.DeserializeObject(json);
foreach (var cita in dynObj)
{
Console.WriteLine("{0} {1} {2}",
cita.horario.profesor.apellido,
cita.alumno.nombre,
cita.fechaSolicitud
);
}
Upvotes: 4