c# - DataContractJsonSerializer throws exception Expecting state 'Element'.. Encountered 'Text' with name '', namespace '' -


i need serialize piece of json.

i response rest service, service returning json. after want map request class. i'm using datacontractjsonserializer, can't work.

when data serialize following exception thrown:

"expecting state 'element'.. encountered 'text'  name '', namespace ''. " 

here 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 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 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 appreciated!

edit:

i'm complementing service code:

list<myclass> myclass = validationmanager.getxmlasalistofeducationinformationobject();  javascriptserializerserializer = new javascriptserializer();  string jsondata = serializer.serialize(myclass);  return jsondata; 

i had same issue.
sending json object client(browser-using jquery) server application(iis-asp.net using c#).
json includes inner json objects , there problem exists.
below list json object (in minimal version example case), c# classes must constructed , code deserializes json c# data classes.

json object:

"{"conference_id":"9","submissiontype":{"value":"1"},"textpublishtype":[{"value":"12"},{"value":"9"}],"title":"atitle"}"   

be carefull: submissiontype property includes inner json value , textpublishtype property includes array of json object value.

c# class must constructed(depends on 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 different class cause includes inner json object.
textpublishtype defined list of different class cause includes inner array json object.

public class submissiontypeclass {     public int value { get; set; } }  public class textpublishtypeclass {     public int value { get; set; } } 

this 2 classes include 1 property. because on json object inner json include 1 property(value). simple example answer case. if want more properties on json object have define them with same key name on 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 think create more complex class structure.
hope help.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -