I need help to serialize a json fragment.
I get a response from the recreation service, the service returns json. After that, I want to map the request to the class. I am using DataContractJsonSerializer, but I cannot get it to work.
When data is serialized, the following exception is thrown:
"Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''. "
Here is the code:
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); Stream responseStreamm = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStreamm); string streamAsString = reader.ReadToEnd(); MemoryStream memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(streamAsString)) {Position = 0}; DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<MyClass>)); List<MyClass> myClass = (List<MyClass>)serializer.ReadObject(memoryStream);
And here is MyClass:
[DataContract] public class MyClass { [DataMember] public string RawData { get; set; } [DataMember] public string StudentIdentity { get; set; } [DataMember] public string FirstName { get; set; } [DataMember] public string LastName { get; set; } [DataMember] public string SchoolName { get; set; } [DataMember] public string SchoolCode { get; set; } [DataMember] public string TypeOfEducation { get; set; } [DataMember] public string EducationCode { get; set; } [DataMember] public string NationalProgram { get; set; } [DataMember] public string Objective { get; set; } [DataMember] public string IssuingDate { get; set; } [DataMember] public string GradeType { get; set; } [DataMember] public string ProgramRange { get; set; } [DataMember] public string HourTotal { get; set; } [DataMember] public string BasicEligibility { get; set; } [DataMember] public string OccupationCompetence { get; set; } [DataMember] public string CourseOfStudyFromSchool { get; set; } [DataMember] public string Software { get; set; } [DataMember] public string SoftwareProvider { get; set; } [DataMember] public string ProgramType { get; set; } [DataMember] public string Note { get; set; } }
Answer from the service:
"[{\"RawData\":\"\",\"StudentIdentity\":\"450101\",\"FirstName\":\"Kalle\",\"LastName\":\"Karlsson\",\"SchoolName\":\"\",\"SchoolCode\":\"SKL123\",\"TypeOfEducation\":\"\",\"EducationCode\":\"Code\",\"NationalProgram\":\"\",\"Objective\":\"Obj\",\"IssuingDate\":\"2012-01-28\",\"GradeType\":\"GradeType\",\"ProgramRange\":\"1\",\"HourTotal\":\"2000\",\"BasicEligibility\":\"BE\",\"OccupationCompetence\":\"OC\",\"CourseOfStudyFromSchool\":\"Y\",\"Software\":\"HAL213\",\"SoftwareProvider\":\"SchoolSoft\",\"ProgramType\":\"C\",\"Note\":\"Notering\",\"CourseInformation\":[{\"CourseCode\":\"ABC555\",\"Grade\":\"VG\",\"GradeDate\":\"2012-01-28\",\"Points\":\"50\",\"Comment1\":\"Kommentar1\",\"Comment2\":\"\",\"Comment3\":\"\",\"AddtionalInformation\":\"Info\",\"Exceptions\":null},{\"CourseCode\":\"DFG333\",\"Grade\":\"G\",\"GradeDate\":\"2012-01-28\",\"Points\":\"60\",\"Comment1\":\"\",\"Comment2\":\"\",\"Comment3\":\"\",\"AddtionalInformation\":\"\",\"Exceptions\":null}],\"Exceptions\":[]}]"
Help is much appreciated!
Edit:
I am completing the service code:
List<MyClass> myClass = validationManager.GetXmlAsAListOfEducationInformationObject(); JavaScriptSerializerserializer = new JavaScriptSerializer(); string jsonData = serializer.Serialize(myClass); return jsonData;