I'm trying to work out how to extract a List of objects from a JSON request in a WEBAPI call. Within this list of objects is a Dictionary object, a List object and a List> object.
My request from my MVC controller, looks a bit like this:
//Gets a list of various objects
List<object> values = new List<object>();
values.add(GetItem1()); //Returns a Dictionary<string, string> object
values.add(GetItem2()); //Returns a 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 the WEBAPI object, I have this function that is receiving the call from the MVC controller. This is where I have my trouble, I can't get the data out of the List. The first item in the list, happens to be a 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 I try to start using the object, I get the error:
Cannot cast 'myObjectList[0]' (which has an actual type of 'Newtonsoft.Json.Linq.JObject') to 'System.Collections.Generic.Dictionary<string,string>'
Unfortunately, I'm at a complete loss about what to do next. I can't seem to find a way to deserialize the List into these individual object types.