I need help to serialize a piece of json.
I get a response from a rest service, the service is returning json. After that I want to map the request to a class. I'm using the DataContractJsonSerializer, but I can't get it to work.
When the data is serialize 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 the 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; }
}
The response from the service is:
"[{\"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'm complementing with the service code:
List<MyClass> myClass = validationManager.GetXmlAsAListOfEducationInformationObject();
JavaScriptSerializerserializer = new JavaScriptSerializer();
string jsonData = serializer.Serialize(myClass);
return jsonData;
I had the same issue.
I was sending a JSON object from client(browser-using jQuery) to my server application (IIS-ASP.NET using c#).
The JSON includes inner JSON objects and there the problem exists.
Below I list the JSON object (in a minimal version for the example case), the c# classes that must be constructed and the code that deserializes the JSON to the C# data classes.
JSON object:
"{"Conference_id":"9","SubmissionType":{"Value":"1"},"TextPublishType":[{"Value":"12"},{"Value":"9"}],"Title":"aTitle"}"
Be carefull: The SubmissionType property includes an inner JSON as its value and the TextPublishType property includes an array of JSON object as its value.
C# class that must be constructed(depends on the JSON structure):
public class Abstract
{
public int Conference_id { get; set; }
public SubmissionTypeClass SubmissionType { get; set; }
public List<TextPublishTypeClass> TextPublishType{ get; set; }
}
The SubmissionType defined as a different class cause it includes an inner JSON object.
The TextPublishType defined as a list of a different class cause it includes an inner array JSON object.
public class SubmissionTypeClass
{
public int Value { get; set; }
}
public class TextPublishTypeClass
{
public int Value { get; set; }
}
This two classes include only one property. This is because on the JSON object the inner JSON include only one property(Value). This is a simple example just for the answer case. If you want more properties on the JSON object then you have to define them with the same key name on the class definition.
The deserialization code:
Abstract theAbstract = Activator.CreateInstance<Abstract>();
MemoryStream memoryStream = new MemoryStream(Encoding.Unicode.GetBytes("the above JSON text"));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(theAbstract.GetType());
theAbstract = (Abstract)serializer.ReadObject(memoryStream);
memoryStream.Dispose();
For more complex JSON objects the only thing to do is to create a more complex class structure.
I hope that it will help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With