c# - Deserializing from JSON (ScriptObject) to Managed Object -
i'm attempting deserialize json returned javascript via silverlight.
basically on client side returning json , in c# handler, getting via scriptobject...
i tried convertto method on scriptobject , still not anything.
how able convert scriptobject c# object list of objects?
somecallback(scriptobject result) { // convert managed object var objects = result.convertto<list<someobjectclass>>(); // can't property it.. // count correct... messagebox.show("count: " + objects.count); // shows correct count of items }
silverlight contains no api take scriptobject
, serialise json string.
silverlight supports json serialisation via system.runtime.serialization.json.datacontractjsonserializer
class found in system.servicemodel.web
dll.
you need javascript base json serialiser convert value trying pass scriptobject
pass json string parameter instead of scriptobject
. believe popular tool job jquery.
now looks expecting set (json "[x1,x2,,,xn]") x items of type someobjectclass
. can use little generic function deserialise such list :-
list<t> deserializejson<t>(string json) { byte[] array = encoding.utf8.getbytes(json); memorystream ms = new memorystream(array); datacontractjsonserializer dcs = new datacontractjsonserializer(typeof(list<t>)); return (list<t>)dcs.readobject(ms); }
you do:-
var objects = deserializejson<someobjectclass>(somejson);
Comments
Post a Comment