DataContractJsonSerializer throws an exception. Waiting for state "Element" .. A "Text" with the name "namespace" was detected

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; 
+6
source share
2 answers

I had the same problem.

I sent the JSON object from the client (using jQuery using a browser) to my server application (IIS-ASP.NET using C #).

JSON includes internal JSON objects and there is a problem.

Below I list a JSON object (in the minimal version for example), C # classes to be constructed, and code that deserializes JSON for C # data classes.

JSON object:

 "{"Conference_id":"9","SubmissionType":{"Value":"1"},"TextPublishType":[{"Value":"12"},{"Value":"9"}],"Title":"aTitle"}" 

Be careful: the SubmissionType property includes the internal JSON as its value, and the TextPublishType property includes an array of the JSON object as the value.

C # class to be built (depends on JSON structure):

 public class Abstract { public int Conference_id { get; set; } public SubmissionTypeClass SubmissionType { get; set; } public List<TextPublishTypeClass> TextPublishType{ get; set; } } 

SubmissionType, defined as another class, causes the inclusion of an internal JSON object.
TextPublishType, defined as a list of another class, causes the inclusion of an internal JSON object of the array.

 public class SubmissionTypeClass { public int Value { get; set; } } public class TextPublishTypeClass { public int Value { get; set; } } 

These two classes include only one property. This is due to the fact that on a JSON object, internal JSON includes only one property (value). This is a simple example for the answer case. If you need more properties on a JSON object, you must define them with the same key name in the class definition.

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 create a more complex class structure.
I hope this helps.

+6
source

This service response works when I deserialize it, which makes me suspect that your extra dance with MemoryStream is causing a zero thread somewhere. Just simplify it and pass the response stream directly to the serializer:

 HttpWebResponse response = webRequest.GetResponse(); var serializer = new DataContractJsonSerializer(typeof(List<MyClass>)); var list = (List<MyClass>)serializer.ReadObject(response.GetResponseStream()); 
0
source

Source: https://habr.com/ru/post/910187/


All Articles