2016-10-27 2 views
-1

ich eine Liste der für dieses Objekt haben:Linq Transformation Hilfe benötigt

public class WebinarAttendeesList { 
    public int wa_id { get; set; } 
    public int PID { get; set; } 
    public string FullName { get; set; } 
    public string Email { get; set; } 
    public string JoinTime { get; set; } 
    public string TimeInSession { get; set; } 
    public string LeaveTime { get; set; } 
    public int FirstPollCount { get; set; } 
    public int SecondPollCount { get; set; } 
    public int AttendedWebinar { get; set; } 
    public int Makeup { get; set; } 
    public string Comments { get; set; } 
    public string RegistrantKey { get; set; } 
    public string webinarKey { get; set; } 
} 

Ich verwende diesen Code Linq:

var webinars = new 
{ 
    Webinars = r.GroupBy(x => new { x.PID, x.FullName }). 
    Select(y => new 
    { 
     PID = y.Key.PID, 
     FullName = y.Key.FullName, 
     AffReceived = 0, 
     Attendances = y.Select(z => new 
     { 
      WebinarKey = z.webinarKey, 
      RegistrantKey = z.RegistrantKey, 
      TimeInSession = z.TimeInSession, 
      FirstPollCount = z.FirstPollCount, 
      SecondPollCount = z.SecondPollCount, 
      AttendedWebinar = z.AttendedWebinar 
     }) 
    }) 
}; 
string json = JsonConvert.SerializeObject(webinars); 

die Liste zu JSON zu transformieren. Aber ich denke, ich würde lieber haben es in eine List<FinalWebinarAttendeesList> umgewandelt (siehe unten Definition). Der JSON, den er erzeugt, sieht korrekt aus und übergibt Lint.

Wenn ich versuche, dies zu tun:

List<FinalWebinarAttendeesList> fl = JsonConvert.DeserializeObject<List<FinalWebinarAttendeesList>>(json); 

Ich erhalte eine Fehlermeldung:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 
'System.Collections.Generic.List`1[WebinarProject.Models.FinalWebinarAttendeesList]' 
because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. 
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or 
change the deserialized type so that it is a normal .NET type (e.g. not a 
primitive type like integer, not a collection type like an array or List<T>) 
that can be deserialized from a JSON object. JsonObjectAttribute can also be 
added to the type to force it to deserialize from a JSON object. Path 
'Webinars', line 1, position 12. 

Wie kann ich dies entweder direkt in das Objekt verwandelt ich will, oder in der Lage sein, es zu deserialisieren in das Objekt, das ich will?

FinalWebinarAttendeesList Definition:

public class FinalWebinarAttendeesList 
{ 
    public FinalWebinar Webinars {get; set; } 
} 
public class FinalWebinar 
{ 
    public int PtID { get; set; } 
    public string FullName { get; set; } 
    public int AffReceived { get; set; } 
    public List<FinalWebinarAttendee> Attendances { get; set; } 

} 
public class FinalWebinarAttendee 
{ 
    public string RegistrantKey { get; set; } 
    public string TimeInSession { get; set; } 
    public int FirstPollCount { get; set; } 
    public int SecondPollCount { get; set; } 
    public int AttendedWebinar { get; set; } 

} 
+0

ich das Problem nicht reproduzieren kann. Ist dieser Code vollständig? –

Antwort

0

Sie erstellt nur eine Webinar Instanz, aber Sie versuchen, eine Liste von Webinar s deserialisieren. Versuchen:

FinalWebinarAttendeesList fl = JsonConvert.DeserializeObject<FinalWebinarAttendeesList>(json); 
+0

'FinalWebinarAttendeesList fl = JsonConvert.DeserializeObject (json);' erzeugt denselben Fehler. – MB34

+0

@ MB34 Können Sie den JSON angeben, den Sie deserialisieren möchten? – Nico

+0

Das Webinars-Mitglied FinalWebinarAttendeesList wurde in eine Liste geändert. 'öffentliche Liste Webinars {bekommen; einstellen; } ' – MB34

1

Ich habe eine json Beispielausgabe Zufallsdaten und und ich fand, dass Sie versuchen, die json mit dem falschen Objekt deserialisieren. Json Ausgabe:

{"Webinars":[{"PID":453,"FullName":"jdis","Attendances":[{"WebinarKey":"kdnsaod","RegistrantKey":"udhaso","TimeInSession":"hdija","FirstPollCount":45,"SecondPollCount":45,"AttendedWebinar":0}]}]} 

Das Objekt hat folgende Struktur de:

public class Attendance 
{ 
    public string WebinarKey { get; set; } 
    public string RegistrantKey { get; set; } 
    public string TimeInSession { get; set; } 
    public int FirstPollCount { get; set; } 
    public int SecondPollCount { get; set; } 
    public int AttendedWebinar { get; set; } 
} 

public class Webinar 
{ 
    public int PID { get; set; } 
    public string FullName { get; set; } 
    public List<Attendance> Attendances { get; set; } 
} 

public class RootObject 
{ 
    public List<Webinar> Webinars { get; set; } 
} 

Und für Deserialisieren das Objekt:

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