i getting error when using class.
error
expecting element 'root' namespace ''.. encountered 'none' name '', namespace
my class
[datacontract] public class entrydata { [datamember] public string entryid { get; set; } [datamember] public string emailaddress { get; set; } [datamember] public string statuscode { get; set; } [datamember] public string totalvotecount { get; set; } public static t deserialise<t>(string json) { var obj = activator.createinstance<t>(); using (var memorystream = new memorystream(encoding.unicode.getbytes(json))) { memorystream.position = 0; var serializer = new datacontractjsonserializer(obj.gettype()); obj = (t)serializer.readobject(memorystream); // getting exception here return obj; } } }
usage
string responsejson = new streamreader(httpcontext.current.request.inputstream).readtoend(); var results = entrydata.deserialise<entrydata>(response)
i have seen online has memorystream position
can see setting beginning.
please help.
json going handler
i don't set statuscode or totalvotecount when passing json in. don't think problem though.
{ "entryid":"43", "emailaddress":"test@email.com" }
answer
instead of using deserialize method in class using this.
//commented out code. string responsejson = new streamreader(httpcontext.current.request.inputstream).readtoend(); var results = entrydata.deserialise<entrydata>(response) // way go using javascriptserializer var serializer = new javascriptserializer(); var results = serializer.deserialize<entrydata>(response);
could caused json names not matching property names in c#?
my understanding that
{ "firstname" : "mark" }
would able deserialize into:
[datacontract] public class person { [datamember] public string firstname {get; set;} }
but wouldn't able serialize
{ "name" : "mark" }
unless changed c# class have explicit name datamember
[datacontract] public class person { [datamember(name="name")] public string firstname {get; set;} }
i'm not sure error cause though. don't have enough first hand experience.
Comments
Post a Comment