serialization - Deserializing list of JSON objects in WebApi method -


i'm trying work out how extract list of objects json request in webapi call. within list of objects dictionary object, list object , list> object.

my request mvc controller, looks bit this:

//gets list of various objects list<object> values = new list<object>();  values.add(getitem1()); //returns dictionary<string, string> object values.add(getitem2()); //returns list<string> object values.add(getitem3()); //list<keyvaluepair<string, string>>   var content = new stringcontent(jsonconvert.serializeobject(values), encoding.utf8, "application/json"); httpresponsemessage postresult = client.postasync(baseurl, content).result; 

in webapi object, have function receiving call mvc controller. have trouble, can't data out of list. first item in list, happens dictionary.

[httppost] public void logerror(list<object> myobjectlist) {     dictionary<string, string> items = (dictionary<string, string>)myobjectlist[0];     list<string> moreitems = (list<string>)myobjectlist[1];     list<keyvaluepair<string, string>> evenmoreitems = (list<keyvaluepair<string, string>>)myobjectlist[2]; } 

if try start using object, error:

cannot cast 'myobjectlist[0]' (which has actual type of 'newtonsoft.json.linq.jobject') 'system.collections.generic.dictionary<string,string>' 

unfortunately, i'm @ complete loss next. can't seem find way deserialize list these individual object types.

the reason why hitting cast exception because type information lost client , server.

one way scenario work following

  1. use objectcontent instead of stringcontent let formatter (i.e. jsonmediatypeformatter) deal serialization , deserialization
  2. turn on typenamehandling on both client , server type information send on wire

server (webapiconfig.cs):

 config.formatters.jsonformatter.serializersettings.typenamehandling = newtonsoft.json.typenamehandling.all; 

client:

        //gets list of various objects         list<object> values = new list<object>();         values.add(getitem1()); //returns dictionary<string, string> object         values.add(getitem2()); //returns list<string> object         values.add(getitem3()); //list<keyvaluepair<string, string>>           var formatter = new jsonmediatypeformatter();         formatter.serializersettings.typenamehandling = newtonsoft.json.typenamehandling.all;          var content = new objectcontent<list<object>>(values, formatter);         httpresponsemessage postresult = client.postasync(baseurl, content).result; 

sample request body:

{"$type":"system.collections.generic.list1[[system.object, mscorlib]], mscorlib","$values":[{"$type":"system.collections.generic.dictionary2[[system.string, mscorlib],[system.string, mscorlib]], mscorlib","dictionarykey1":"dictionaryvalue1"},{"$type":"system.collections.generic.list1[[system.string, mscorlib]], mscorlib","$values":["listitem1"]},{"$type":"system.collections.generic.list1[[system.collections.generic.keyvaluepair`2[[system.string, mscorlib],[system.string, mscorlib]], mscorlib]], mscorlib","$values":[{"key":"keyvaluepairkey","value":"keyvaluepairkeyvalue"}]}]}


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -